WordPress:如何在 wp_insert_post_data() 中显示自定义错误消息
我将显示一条自定义错误消息。
function ccl($data, $postarr = '') {
if($data['post_status'] == "publish"){
$data['post_status'] = "draft";
echo '<div id="my-custom-error" class="error fade"><p>Publish not allowed</p></div>';
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'ccl' , '99' );
我尝试了很多想法,但每次文章发布的 wordpress 都会发出成功消息。我可以取消成功消息并显示我自己的错误消息吗?
坦克求救...
I'll show a custom - error message.
function ccl($data, $postarr = '') {
if($data['post_status'] == "publish"){
$data['post_status'] = "draft";
echo '<div id="my-custom-error" class="error fade"><p>Publish not allowed</p></div>';
}
return $data;
}
add_filter( 'wp_insert_post_data' , 'ccl' , '99' );
I've try many thinks but everytime a success message comes from wordpress that the article published. Can i kill the success message and show my own error message?
tanks for help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在
wp_insert_post_data
过滤器中打印错误,因为用户会立即重定向在此之后。最好的办法是挂钩重定向过滤器并将消息变量添加到查询字符串(这将覆盖任何现有的 Wordpress 消息)。因此,请在
wp_insert_post_data
过滤器函数中添加重定向过滤器。然后在重定向过滤器函数中添加消息变量。
最后连接到
post_updated_messages
过滤器并添加您的消息,以便 WordPress 知道要打印什么。You can't print an error in a
wp_insert_post_data
filter because the user is redirected immediately after this. The best thing to do is to hook into the redirect filter and add a message variable to the query string (this will overwrite any existing Wordpress message).So, add the redirect filter in your
wp_insert_post_data
filter function.Then add a message variable in the redirect filter function.
Finally hook into the
post_updated_messages
filter and add your message so Wordpress knows what to print.