do_action( "{$new_status}_{$post->post_type}", int $post_id, WP_Post $post )

Fires when a post is transitioned from one status to another.


Description Description

The dynamic portions of the hook name, $new_status and $post->post_type, refer to the new post status and post type, respectively.

Possible hook names include:

  • draft_post
  • future_post
  • pending_post
  • private_post
  • publish_post
  • trash_post
  • draft_page
  • future_page
  • pending_page
  • private_page
  • publish_page
  • trash_page
  • publish_attachment
  • trash_attachment

Please note: When this action is hooked using a particular post status (like ‘publish’, as publish_{$post->post_type}), it will fire both when a post is first transitioned to that status from something else, as well as upon subsequent post updates (old and new status are both the same).

Therefore, if you are looking to only fire a callback when a post is first transitioned to a status, use the ‘transition_post_status’ hook instead.


Top ↑

Parameters Parameters

$post_id

(int) Post ID.

$post

(WP_Post) Post object.


Top ↑

Source Source

File: wp-includes/post.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by stevenlinx

    Example Migrated from Codex:

    The example below will send an email via wp_mail() to the post author when their article is published.

    function post_published_notification( $post_id, $post ) {
        $author = $post->post_author; /* Post author ID. */
        $name = get_the_author_meta( 'display_name', $author );
        $email = get_the_author_meta( 'user_email', $author );
        $title = $post->post_title;
        $permalink = get_permalink( $post_id );
        $edit = get_edit_post_link( $post_id, '' );
        $to[] = sprintf( '%s <%s>', $name, $email );
        $subject = sprintf( 'Published: %s', $title );
        $message = sprintf ('Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
        $message .= sprintf( 'View: %s', $permalink );
        $headers[] = '';
        wp_mail( $to, $subject, $message, $headers );
    }
    add_action( 'publish_post', 'post_published_notification', 10, 2 );
    
  2. Skip to note 2 content
    Contributed by tdatu
    add_action('publish_post', 'send_notification');
    
    function send_notification($id, $post_obj){
    	
    	//Assemble the message
    	$msg = 'Hi, a new post has been published. Here is the content:';
    	$msg .= $post_obj->post_content;
    	
    	//Send the notification
    	wp_mail('recipient@example.com', $post_obj->post_title, $msg);
    }
    
  3. Skip to note 3 content
    Contributed by Chigozie Orunta

    This WP hook helps you receive a slack notification every time a user saves a new post on your site. It uses your Slack webhook, username, and channel. It makes use of the PHP Slack library which can be downloaded via Composer to your themes directory.

    function wpdocs_notify_my_slack( $post_id, $post ) {
    	require_once __DIR__ . '/vendor/autoload.php';
    
    	$slack_hook = 'https://hooks.slack.com/services/xxxxxx';
    	$slack_message = 'New Post alert | %3$s: %2$s - %1$s';
    	$slack_message = sprintf( $slack_message, get_permalink( $post_id ), $post->post_title, $post->post_date );
    	$settings = [
    		'username' => 'Chigozie Orunta',
    		'channel'  => '#blog',
    	];
    	$client = new MaknzSlackClient( $slack_hook, $settings );
    	$client->send( $slack_message );
    }
    add_action( 'publish_post', 'wpdocs_notify_my_slack', 10, 2 );
    

Top ↑

You must log in before being able to contribute a note or feedback.