What is WordPress Hook: wp_trash_post
The wp_trash_post hook is a specific WordPress hook that is triggered when a post is moved to the trash. It allows developers to perform actions or execute custom code when a post is trashed within the WordPress system.
Understanding the Hook: wp_trash_post
The wp_trash_post hook is located within the wp-includes/post.php file in WordPress. It is called after the post is actually trashed. This hook provides the post ID as its only parameter, allowing developers to target specific posts that have been trashed.
Hook Parameters (if applicable): wp_trash_post
The wp_trash_post hook accepts a single parameter, which is the post ID of the trashed post. This parameter allows developers to access information about the trashed post and perform actions based on its ID.
Hook Doesn’t Work: wp_trash_post
If the wp_trash_post hook doesn’t seem to be working, it could be due to a few reasons. Firstly, ensure that the hook is being used correctly and is located in the appropriate place within the code. Additionally, check for any conflicts with other plugins or themes that may be affecting the functionality of the hook.
Best Practices & Usage Notes (if applicable): wp_trash_post
When using the wp_trash_post hook, it’s important to note that it is specifically for trashing posts and not for other types of content. Additionally, developers should be cautious when performing actions within this hook, as it directly affects the trashing of posts within the WordPress system.
Usage Example: wp_trash_post
“`php
function custom_trash_post_action( $post_id ) {
// Perform custom actions when a post is trashed
// Example: Log the trashed post ID
error_log( ‘Post trashed – ID: ‘ . $post_id );
}
add_action( ‘wp_trash_post’, ‘custom_trash_post_action’ );
“`