wordpress自动发布_WordPress发布帖子挂钩

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,花了我一段时间在这里找到我需要的东西-希望这可以节省您很多搜索和痛苦!

翻译自: https://davidwalsh.name/wordpress-publish-post-hook

wordpress自动发布

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值