Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use deleted_post action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 15, 2023
5 minutes read

Welcome, fellow WordPress enthusiasts! Let’s dive into the world of WordPress hooks with the deleted_post action. This handy hook is triggered right after a post is deleted from the database. If you’re developing on WordPress, utilizing hooks like deleted_post can make your life a lot easier.

To get started with the deleted_post action, you need to register it using add_action. You can include this in the functions.php of your active theme or in a custom WordPress Plugin. WePlugins recommends creating a custom plugin to ensure your modifications remain intact even after theme updates.

Here’s how you can define a function for the deleted_post event, which takes two parameters. Then, register it using add_action. The first argument is the hook name, the second is the function to be executed, followed by the priority and the number of arguments (if any) for the function.

Example 1: Basic Usage

Here’s a simple example of how you can use this hook:

    function weplugins_execute_on_deleted_post_event($postid, $post){
        // Code to execute when the post is deleted.
    }
    add_action("deleted_post", "weplugins_execute_on_deleted_post_event", 10, 2);
    

Example 2: Removing a Hook

If you need to remove a registered hook, you can use remove_action like so:

    remove_action("deleted_post", "weplugins_execute_on_deleted_post_event", 10, 2);
    

Make sure to provide the same callback function name, priority, and number of arguments when removing the hook.

Example 3: Custom Functionality

Imagine adding custom functionality that runs when a post is deleted. Here’s how you can implement it:

    function weplugins_custom_function_on_delete($postid, $post){
        // Custom actions or modifications when the post is deleted.
    }
    add_action("deleted_post", "weplugins_custom_function_on_delete", 10, 2);
    

Access Premium WordPress Plugins

Parameters:

  • $postid: (int) Post ID.
  • $post: (WP_Post) Post object.
Contact Us:

If you need any help customizing this hook or have any questions, feel free to reach out to us. Our team at WePlugins is always ready to assist. Contact Us

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.