What is the wordpress hook when I publish a new post, (not when I update a published one)?

左心房为你撑大大i 提交于 2019-12-05 11:33:48
DᴀʀᴛʜVᴀᴅᴇʀ

I think the hook you're looking for is draft_to_publish

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 );
Blackbam

You should read Post Status Transitions from the WordPress Codex in order to fully understand the problem.

A possible solution which covers most cases for instance is:

add_action('draft_to_publish', 'postPublished');
add_action('future_to_publish', 'postPublished');
add_action('private_to_publish', 'postPublished');
function your_callback( $post_id, $post  ) {

        if (strpos($_SERVER['HTTP_REFERER'], '&action=edit') !== false) {

            //edit previous post
        }
        else{
            //just add a new post
        }
    }
    add_action( 'publish_{your_post_type}', 'your_callback', 10, 3 );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!