wordpress自动发布
One of the best parts of WordPress is its hook/action system; this special hook system is WordPress' way of assigning callbacks when certain events occur. One event that there seems to be a lot of confusion over is which hook to use to detect when a post is initially published. There's the publish_post
hook but that fires when you click the "Update" button after a post has already been published; that's not ideal.
WordPress最好的部分之一是它的钩子/动作系统。 这种特殊的挂钩系统是WordPress在某些事件发生时分配回调的方式。 似乎有很多困惑的事件是用于检测帖子最初发布时间的钩子。 有publish_post
钩子,但是在发布了一个帖子后单击“更新”按钮时会触发该钩子; 那不理想。
Scour the WordPress documentation and forums and you're sure to see a dozen other solutions but none work as well as the transition_post_status
hook:
搜寻WordPress文档和论坛,您肯定会看到许多其他解决方案,但没有一个能像transition_post_status
挂钩一样有效:
// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);
// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
// Do something!
}
}
The transition_post_status
occurs when a post goes from one status to another; you can check out the post status list to see other possible values. I've also added a post_type
check to ensure the post is a blog post and not a page.
当帖子从一种状态变为另一种状态时,会发生transition_post_status
。 您可以查看发布状态列表以查看其他可能的值。 我还添加了一个post_type
检查,以确保该帖子是博客帖子而不是页面。
Whew, took me a while to find what I needed here -- hopefully this saves you a lot of searching and pain!
ew,花了我一段时间在这里找到我需要的东西-希望这可以节省您很多搜索和痛苦!
wordpress自动发布