Wordpress:如何在wp_insert_post_data()中显示自定义错误消息

Wordpress:如何在wp_insert_post_data()中显示自定义错误消息,wordpress,Wordpress,我将显示一条自定义错误消息 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>'; } retur

我将显示一条自定义错误消息

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' );
函数ccl($data,$postarr=''){
如果($data['post_status']==“publish”){
$data['post_status']=“草稿”;
回显“不允许发布”

; } 返回$data; } 添加过滤器('wp_insert_post_data','ccl','99');
我尝试过很多想法,但每次都有一条成功的信息来自文章发表的wordpress。我可以取消成功消息并显示我自己的错误消息吗


如需帮助…

您不能在筛选器中打印错误,因为在此之后用户将立即重定向。最好的方法是挂接重定向过滤器,并将消息变量添加到查询字符串中(这将覆盖任何现有Wordpress消息)

因此,在
wp\u insert\u post\u data
filter函数中添加重定向过滤器

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;
}
function my_redirect_post_location_filter($location) {
  remove_filter('redirect_post_location', __FUNCTION__, 99);
  $location = add_query_arg('message', 99, $location);
  return $location;
}
然后在重定向过滤器函数中添加一个消息变量

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;
}
function my_redirect_post_location_filter($location) {
  remove_filter('redirect_post_location', __FUNCTION__, 99);
  $location = add_query_arg('message', 99, $location);
  return $location;
}
最后连接到
post\u updated\u 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;
}

您不能在筛选器中打印错误,因为在此之后用户立即被重定向。最好的方法是挂接重定向过滤器,并将消息变量添加到查询字符串中(这将覆盖任何现有Wordpress消息)

因此,在
wp\u insert\u post\u data
filter函数中添加重定向过滤器

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;
}
function my_redirect_post_location_filter($location) {
  remove_filter('redirect_post_location', __FUNCTION__, 99);
  $location = add_query_arg('message', 99, $location);
  return $location;
}
然后在重定向过滤器函数中添加一个消息变量

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;
}
function my_redirect_post_location_filter($location) {
  remove_filter('redirect_post_location', __FUNCTION__, 99);
  $location = add_query_arg('message', 99, $location);
  return $location;
}
最后连接到
post\u updated\u 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;
}