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

How to use deleted_option action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
March 27, 2023
5 minutes read

Hey there! If you’re diving into the world of WordPress hooks, you’re in for a treat. Today, we’re looking at the deleted_option action. This hook is quite nifty as it fires right after an option has been deleted. Whether you’re building a theme or a plugin, knowing how to use this hook can really step up your WordPress game!

To make use of the deleted_option action, you first need to register it using add_action. You can pop this code into your theme’s functions.php file or, even better, create a custom WordPress plugin. We at WePlugins always recommend creating custom plugins to avoid any hiccups during theme updates.

Sometimes, you might want to remove a registered hook. No worries, you can use remove_action to remove the deleted_option action.

Access Premium WordPress Plugins

Example 1: Basic Usage

Here’s a straightforward example of how you can use the deleted_option hook.

    function weplugins_execute_on_deleted_option_event($option){
       // Custom code to execute when the option is deleted.
    }
    // Add the action
    add_action("deleted_option", "weplugins_execute_on_deleted_option_event", 10, 1);
    

Example 2: Removing a Hook

Need to remove a hook callback? Here’s how you can do it:

    remove_action("deleted_option", "weplugins_execute_on_deleted_option_event", 10, 1);
    

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

Example 3: Advanced Implementation

Looking to implement something a bit more advanced? Try adding some custom functionality based on the deleted option name:

    function weplugins_advanced_deleted_option_handler($option){
        if($option === 'my_custom_option'){
            // Perform some special action
        }
    }
    add_action("deleted_option", "weplugins_advanced_deleted_option_handler", 10, 1);
    

If you need any customization or run into any issues, we’re here to help! Feel free to Contact Us for any assistance or custom development needs.

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.