钩子文档

{$new_status}_{$post->post_type}

💡 云策文档标注

概述

本文档介绍了 WordPress 钩子 {$new_status}_{$post->post_type},它在文章状态转换时触发。钩子名称动态部分 $new_status 和 $post->post_type 分别代表新状态和文章类型,可用于执行自定义操作。

关键要点

  • 钩子 {$new_status}_{$post->post_type} 在文章状态改变时触发,例如从草稿到发布。
  • 动态钩子名称示例包括 draft_post、publish_post、trash_page 等,覆盖不同文章类型和状态。
  • 注意:当使用特定状态(如 publish_{$post->post_type})挂钩时,会在首次转换到该状态以及后续更新(新旧状态相同)时都触发。
  • 如果只想在首次转换到状态时触发回调,应使用 transition_post_status 钩子替代。
  • 参数包括 $post_id(文章ID)、$post(文章对象)和 $old_status(旧状态)。

代码示例

function post_published_notification( $post_id, $post ) {
    $author = $post->post_author;
    $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 );
    $to[] = sprintf( '%s ', $name, $email );
    $subject = sprintf( 'Published: %s', $title );
    $message = sprintf ('Congratulations, %s! Your article "%s" has been published.' . "nn", $name, $title );
    $message .= sprintf( 'View: %s', $permalink );
    $headers[] = '';
    wp_mail( $to, $subject, $message, $headers );
}
add_action( 'publish_post', 'post_published_notification', 10, 2 );

注意事项

  • 钩子触发时机包括首次状态转换和后续更新,需根据需求选择合适的钩子。
  • 参数 $old_status 在 WordPress 5.9.0 版本中添加,使用时注意版本兼容性。

📄 原文内容

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

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.

Parameters

$post_idint
Post ID.
$postWP_Post
Post object.
$old_statusstring
Old post status.

Source

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

Changelog

Version Description
5.9.0 Added $old_status parameter.
2.3.0 Introduced.

User Contributed Notes

  1. Skip 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 = 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.' . "nn", $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 6 content

    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 7 content

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

  4. Skip 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( get_stylesheet_directory() ) . apply_filters( 'wpdocs_published_log_file', 'log.txt' );
    
        if ( ! file_exists( $log_file ) ) {
            return;
        }
    
        $file = fopen( $log_file, 'a' );
    
        if ( false === $file ) {
            return;
        }
    
        $message = sprintf( '%s (%d) was just saved.', get_the_title( $post_id ), $post_id );
        fwrite( $log_file, $message . PHP_EOL );
    }