save_post

Dictionary - Projects Engine 2 minutes read

“save_post” is a WordPress hook that is triggered whenever a post or page is saved or updated in the database. This hook is often used by developers to execute custom code whenever a post or page is saved. Here’s an example of how to use the “save_post” hook:

function my_custom_function( $post_id ) {
    // Your custom code goes here
}
add_action( 'save_post', 'my_custom_function' );

In this example, we define a custom function called “my_custom_function” that accepts the post ID as a parameter. We then use the “add_action” function to attach this function to the “save_post” hook.

When a post or page is saved or updated, WordPress will call the “my_custom_function” function and pass in the ID of the post or page that was saved or updated. You can use this ID to retrieve the post data from the database and perform any custom actions that you need to.

It’s important to note that the “save_post” hook is called for all post types, including custom post types. If you only want your custom function to be executed for a specific post type, you can check the post type before executing your code:

function my_custom_function( $post_id ) {
    $post_type = get_post_type( $post_id );
    if ( $post_type === 'my_custom_post_type' ) {
        // Your custom code goes here
    }
}
add_action( 'save_post', 'my_custom_function' );
Was this article helpful?

Add comment