如果你的WordPress站点支持多个作者进行文章的发布,那么可能迟早会存在一种情况就是手误点击发布按钮,虽然出现这样的情况不会造成很大的影响,但是为了避免这样的情况发生,我推荐大家使用下面的代码,作为一道防护。

代码方面如下:

上面的代码通过publish_post钩子来进行处理,该钩子介绍如下:

  • {$new_status}_{$post->post_type} 函数介绍
  • 函数用法
  • do_action( "{$new_status}_{$post->post_type}", int $post_id, WP_Post $post )

    Fires when a post is transitioned from one status to another.

  • 功能介绍
  • The dynamic portions of the hook name, $new_status and $post->post_type, refer to the new post status and post type, respectively.

    Possible hook names include:

      draft_post future_post pending_post private_post publish_post trash_post draft_page future_page pending_page private_page publish_page trash_page publish_attachment trash_attachment

    Please note: When this action is hooked using a particular post status (like ‘publish’, as publish_{$post->post_type}), it will fire both when a post is first transitioned to that status from something else, as well as upon subsequent post updates (old and new status are both the same).

    Therefore, if you are looking to only fire a callback when a post is first transitioned to a status, use the ‘transition_post_status’ hook instead.

  • 参数介绍
  • $post_id
    (int) Post ID.
    $post
    (WP_Post) Post object.
  • 返回值
  • 暂无数据
  • 通过上面的代码,我们同样可以限制文章的最大字数等等,只要是publish_post也就是发布之前你需要进行的判断都可以通过上面的代码进行处理,使用率还是比较高的,感兴趣的朋友可以自行尝试一下。

    使用方面直接将代码添加到function.php中即可,如果不会添加或者权限不足,可以尝试安装插件来解决该问题。