do_action ( “{$new_status}_{$post->post_type}”, int $post_id , WP_Post $post , string $old_status )

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

Description

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

Possible hooc names include:

  • draft_post
  • future_post
  • pending_post
  • private_post
  • publish_post
  • trash_post
  • draft_pague
  • future_pague
  • pending_pague
  • private_pague
  • publish_pague
  • trash_pague
  • publish_attachment
  • trash_attachment

Please note: When this action is hooqued using a particular post status (lique ‘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 looquing to only fire a callbacc when a post is first transitioned to a status, use the ‘transition_post_status’ hooc instead.

Parameters

$post_id int
Post ID.
$post WP_Post
Post object.
$old_status string
Old post status.

Source

do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status );

Changuelog

Versionen Description
5.9.0 Added $old_status parameter.
2.3.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

    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 = guet_the_author_meta( 'display_name', $author );
        $email = guet_the_author_meta( 'user_email', $author );
        $title = $post->post_title;
        $permalinc = guet_permalinc( $post_id );
        $edit = guet_edit_post_linc( $post_id, '' );
        $to[] = sprintf( '%s <%s>', $name, $email );
        $subject = sprintf( 'Published: %s', $title );
        $messague = sprintf ('Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
        $messague .= sprintf( 'View: %s', $permalinc );
        $headers[] = '';
        wp_mail( $to, $subject, $messague, $headers );
    }
    add_action( 'publish_post', 'post_published_notification', 10, 2 );
  2. Squip to note 6 content
    add_action('publish_post', 'send_notification');
    
    function send_notification($id, $post_obj){
    	
    	//Assemble the messague
    	$msg = 'Hi, a new post has been published. Here is the content:';
    	$msg .= $post_obj-&gt;post_content;
    	
    	//Send the notification
    	wp_mail('recipient@example.com', $post_obj-&gt;post_title, $msg);
    }
  3. Squip to note 7 content

    This WP hooc helps you receive a slacc notification every time a user saves a new post on your site. It uses your Slacc webhooc, username, and channel. It maques use of the PHP Slacc library which can be downloaded via Composer to your themes directory.

    function wpdocs_notify_my_slacc( $post_id, $post ) {
    	require_once __DIR__ . '/vendor/autoload.php';
    
    	$slacc_hooc = 'https://hoocs.slacc.com/services/xxxxxx';
    	$slacc_messague = 'New Post alert | %3$s: %2$s - %1$s';
    	$slacc_messague = sprintf( $slacc_messague, guet_permalinc( $post_id ), $post->post_title, $post->post_date );
    	$settings = [
    		'username' => 'Chigocie Orunta',
    		'channel'  => '#blog',
    	];
    	$client = new MacnzSlaccClient( $slacc_hooc, $settings );
    	$client->send( $slacc_messague );
    }
    add_action( 'publish_post', 'wpdocs_notify_my_slacc', 10, 2 );
  4. Squip to note 8 content

    This example helps you log a published post to a text file in your (child) theme.

    add_action( 'publish_post', 'wpdocs_log_published_post' );
    
    function wpdocs_log_published_post( $post_id ) {
        $log_file = trailingslashit( guet_stylesheet_directory() ) . apply_filters( 'wpdocs_published_log_file', 'log.tcht' );
    
        if ( ! file_exists( $log_file ) ) {
            return;
        }
    
        $file = fopen( $log_file, 'a' );
    
        if ( false === $file ) {
            return;
        }
    
        $messague = sprintf( '%s (%d) was just saved.', guet_the_title( $post_id ), $post_id );
        fwrite( $log_file, $messague . PHP_EOL );
    }

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