WordPress.org

Forums

[resolved] “publish_post” action hook shouldn’t be depreciated! Why is it? (6 posts)

  1. lukejanicke
    Member
    Posted 10 months ago #

    On the Action Reference page, publish_post is listed as depreciated. Likewise on the publish_post page itself. It is said to have been depreciated since Version 2.3.

    However...

    On the new Post Status Transitions page there are three NEW kinds of action hooks.

    1. {$old_status}_to_{$new_status} (using the statuses below)
    2. transition_post_status (a generic action hook that fires on every transition)
    3. status_object (e.g. publish_post)

    Statuses

    • new
    • publish
    • pending
    • draft
    • auto-draft
    • future
    • private
    • inherit
    • trash

    So, according to all this publish_post should still be a legitimate action hook!

    I want to use it! In fact, I also want to use pending_post and future_post.

    If I don’t use these, I have to use multiple action hooks to accommodate every possible $old_status that a post may come from. publish_post is a useful action hook that fires every time a post status is changed to publish, irrespective on whether it was previously new, draft, auto_draft, pending, or future.

    What’s the deal?

  2. lukejanicke
    Member
    Posted 10 months ago #

    And perhaps the real crux of my question is what gets passed to these action hooks? Does $post get passed to all of them? Or just the post ID?

  3. lukejanicke
    Member
    Posted 10 months ago #

    I may have found answers but am yet to test them in my plugin.

    Firstly, on the add_action page is the following note.

    Notes

    To find out the number and name of arguments for an action, simply search the code base for the matching do_action() call. For example, if you are hooking into 'save_post', you would find it in post.php:

    <?php do_action( 'save_post', $post_ID, $post, $update ); ?>

    Your add_action call would look like:

    <?php add_action( 'save_post', 'my_save_post', 10, 3 ); ?>

    And your function would be:

    function my_save_post( $post_ID, $post, $update ) {
        // Do stuff here.
    }

    I looked in wp_includes/post.php and found this…

    /**
    * 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.
    *
    * @since 2.3.0
    *
    * @param int     $post_id Post ID.
    * @param WP_Post $post    Post object.
    */
    do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );

    My plugin function and action call will now look like this…

    function my_function( $ID, $post ) {
        // Do stuff here.
    }
    add_action( 'publish_post', 'my_function', 10, 2 );

    Will post results success or otherwise.

  4. lukejanicke
    Member
    Posted 10 months ago #

    Yes. It works.

    The trick, apparently, seems to be putting '2' into the arguments of add_action.

    My full plugin code is on Github:
    https://github.com/lukejanicke/roots-edit-flow/blob/master/roots-edit-flow.php

  5. lukejanicke
    Member
    Posted 10 months ago #

    Resolved.

  6. lukejanicke
    Member
    Posted 10 months ago #

    I've updated the Codex in all the relevant places with current and correct information about all the post status transition hooks. Hopefully, if I have understood the code thoroughly enough, the Codex should be complete and correct.

Reply

You must log in to post.

About this Topic