我将显示一个自定义错误消息.
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' );
Run Code Online (Sandbox Code Playgroud)
我尝试了许多想法,但每次成功的消息来自文章发表的wordpress.我可以杀死成功消息并显示我自己的错误消息吗?
坦克寻求帮助......
您无法在wp_insert_post_data过滤器中打印错误,因为此后会立即重定向用户.最好的做法是挂钩重定向过滤器并将消息变量添加到查询字符串(这将覆盖任何现有的Wordpress消息).
因此,在wp_insert_post_data过滤器功能中添加重定向过滤器.
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;
}
Run Code Online (Sandbox Code Playgroud)
然后在重定向过滤器函数中添加消息变量.
function my_redirect_post_location_filter($location) {
remove_filter('redirect_post_location', __FUNCTION__, 99);
$location = add_query_arg('message', 99, $location);
return $location;
}
Run Code Online (Sandbox Code Playgroud)
最后挂进post_updated_messages过滤器并添加你的消息,以便Wordpress知道要打印什么.
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;
}
Run Code Online (Sandbox Code Playgroud)