当我发布新帖子时(而不是更新已发布的帖子时),wordpress 钩子是什么? [英] What is the wordpress hook when I publish a new post, (not when I update a published one)?
问题描述
我想在新帖子首次发布时(而不是在编辑时)发送电子邮件
I want to send an email any time a new post is FIRST published (not when it is edited)
我试过了:
add_action('publish_post', 'postPublished');
但是 postPublished 在我更新已发布的帖子时也会调用.我只想让它仅在第一次发布帖子时调用.
But postPublished is ALSO called when I update an already published post. I just want to get it called ONLY at first time a post is published.
问候
推荐答案
我认为您正在寻找的钩子是 draft_to_publish
I think the hook you're looking for is draft_to_publish
如果您只想在新帖子上发送电子邮件,我会考虑在帖子发布时进行定位.您甚至可以查看 transition_post_status
钩子,然后判断帖子是否已被编辑,这样的事情可以工作:
If you are wanting to only send an email on a new post I would consider targeting when the post is published. You could even look at the transition_post_status
hook and just conditionalize if the post has been edited, something like this could work:
function so_post_40744782( $new_status, $old_status, $post ) {
if ( $new_status == 'publish' && $old_status != 'publish' ) {
$author = "foobar";
$message = "We wanted to notify you a new post has been published.";
wp_mail($author, "New Post Published", $message);
}
}
add_action('transition_post_status', 'so_post_40744782', 10, 3 );
这篇关于当我发布新帖子时(而不是更新已发布的帖子时),wordpress 钩子是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!