Return $post_id when DOING_AUTOSAVE?

I see the following pattern over and over, on this site and on other places:

add_action( 'save_post', 'wpse14169_save_post' );
function wpse14169_save_post( $post_id )
{
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    // Other code...
}

Why should I return $post_id? save_post is an action, and the return value of an action handler is ignored. The WordPress core itself doesn’t do it either.

The Codex example does return the $post_id, but it would not be the first incorrect (or outdated) line in the Codex.

Am I missing something? Do I need to return $post_id? Was there a there a time when this was needed?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The 'save_post' action was added to core in 2.0, and has always been an action. Looking through the current autosave procedures, it doesn’t appear to call the 'save_post' action directly at any time.

So the short answer is, no. There is no reason, and has never been any reason, to return any value on this action. Of course, it doesn’t hurt at all to do return the post id.

Method 2

Since nothing is being done with the return value, returning the post ID is pointless and should not be done. It only provides room for confusion.

Just tried it out, the following save_post action works fine.

function my_save_post($post_id)
{
    // Stop WP from clearing custom fields on autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    // Prevent quick edit from clearing custom fields
    if (defined('DOING_AJAX') && DOING_AJAX)
        return;

    // Sanitize, validate and save ...

}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x