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

发布于 2024-11-10 16:03:25 字数 431 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-11-17 16:03:25

您无法在 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;
}

然后在重定向过滤器函数中添加消息变量。

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 知道要打印什么。

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;
}

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.

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文