Wordpress:如何在 wp_insert_post_data() 中显示自定义错误消息 [英] Wordpress: how displaying custom error message in wp_insert_post_data()
问题描述
我将显示自定义 - 错误消息.
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' );
我尝试了很多想法,但每次成功信息都来自文章发表的 wordpress.我可以取消成功消息并显示我自己的错误消息吗?
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?
坦克求助...
推荐答案
您无法在 wp_insert_post_data
过滤器,因为用户在此之后立即被重定向.最好的做法是挂钩重定向过滤器,并向查询字符串添加一个消息变量(这将覆盖任何现有的 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).
因此,在您的 wp_insert_post_data
过滤器函数中添加重定向过滤器.
So, add the redirect filter in your wp_insert_post_data
filter function.
add_filter('wp_insert_post_data', 'ccl', 99);
function ccl($data) {
if ($data['post_type'] !== 'revision' && $data['post_status'] == 'publish') {
$data['post_status'] = 'draft';
add_filter('redirect_post_location', 'my_redirect_post_location_filter', 99);
}
return $data;
}
然后在重定向过滤器函数中添加一个消息变量.
Then add a message variable in the redirect filter function.
function my_redirect_post_location_filter($location) {
remove_filter('redirect_post_location', __FUNCTION__, 99);
$location = add_query_arg('message', 99, $location);
return $location;
}
最后连接到 post_updated_messages
过滤器并添加您的消息,以便 Wordpress 知道要打印什么.
Finally hook into the post_updated_messages
filter and add your message so Wordpress knows what to print.
add_filter('post_updated_messages', 'my_post_updated_messages_filter');
function my_post_updated_messages_filter($messages) {
$messages['post'][99] = 'Publish not allowed';
return $messages;
}
这篇关于Wordpress:如何在 wp_insert_post_data() 中显示自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!