New action hook wp_ after_ insert_ post in WordPress 5.6

WordPress 5.6 adds an action hook WP_ after_ insert_ Post to allow theme and plugin developers to run custom code after the article and its taxonomy and metadata have been updated.

Original save_ Post and related action hooks can also be used for this purpose, but they are triggered in scenarios other than classic editors (for example, in the rest API, in the customizer, and when creating automatic drafts) before category items and metadata are updated.

The new hook can send up to three parameters:

$post_ Id the updated article ID is an integer. $post is the complete object of the updated article, that is, WP_ Post object. $updated whether the article has been updated is a Boolean value.

By default, WordPress is in WP_ insert_ This hook is triggered at the end of post(). A third parameter is added to the function to allow developers to prevent the hook from triggering automatically:

$fire_ after_ Does hooks insert WP_ after_ insert_ Post hook, Boolean value, optional, true by default.

The same parameters have been added to WP_ update_ Post () is added to WP as the third parameter and_ insert_ Attachment () as the fifth argument.

If developers call any of these functions and prevent hook triggering, they can expect to call the new function WP manually_ after_ insert_ Post () to trigger a new action. This new feature requires two parameters:

$post saved article ID or object, integer or WP_ Post object $update whether the article has been updated, Boolean value.Simple example

If you are calling WP_ insert_ Post () or one of the related functions listed above, it is recommended that you prevent the hook from running and call it manually if you subsequently update the category item or metadata of the article respectively. For example:

$post_id = wp_insert_post(
    array(
        'post_title'   => 'My post title',
        'post_content' => 'My post content',
        'post_type'    => 'my_cpt',
    ),
    false,
    false
);
 
add_post_meta( $post_id, 'my_meta_key', 'my meta value' );
 
wp_after_insert_post( $post_id, false );
// Second parameter is true for an updated post.

For more information, please see the official log.

Related Article
The first release candidate for WordPress 5.7 is now available!

You can test the WordPress 5.7 release candidate in two ways: Try the WordPress Beta Tester plugin (choose the “Bleeding edge” channel Read more

WordPress 5.6.2 is now available!

WordPress 5.6.2 is now available! This maintenance release includes 5 bug fixes. These bugs affect WordPress version 5.6.1, so you’ll Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image