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

How to use delete_widget action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
October 6, 2022
5 minutes read

delete_widget action

Fires immediately after a widget has been marked for deletion.

To use delete_widget action, first you have to register it using add_action. You can write this code into functions.php of your activated theme or in a custom WordPress Plugin.

We at WePlugins, always prefer to create a custom WordPress Plugin while using hooks so nothing breaks when you update your WordPress Theme in the future.

In the below live example, we have defined a function execute_on_delete_widget_event which takes 3 parameters and we registered using add_action. The first parameter delete_widget is the name of the hook, The second parameter execute_on_delete_widget_event is the name of the function which needs to be called, the third parameter is the priority of calling the hook if the same hook is used multiple times and the last parameter is the number of arguments (if any) to be passed in the registered function.

Sometime, you have to remove a registered hook so you can use remove_action to remove delete_widget action.

Parameters

Below are the 3 parameters required to use this hook.

  • $widget_id : (string) ID of the widget marked for deletion.
  • $sidebar_id : (string) ID of the sidebar the widget was deleted from.
  • $id_base : (string) ID base for the widget.

Live Example

Below is an example how you can use this hook.

    function weplugins_execute_on_delete_widget_event($widget_id, $sidebar_id, $id_base){
       //You can write code here to be executed when this action occurs in WordPress. Use the parameters received in the function arguments & implement the required additional custom functionality according to your website requirements.
    }
    // add the action
    add_action( "delete_widget", "weplugins_execute_on_delete_widget_event" , 10, 3);
    

To remove a hook callback, use the example below.

remove_action( "delete_widget", "weplugins_execute_on_delete_widget_event", 10, 3 );

Please make sure to provide the same callback function name, priority and number of arguments while removing the hook callback.

Example 1: Logging Widget Deletion

This example logs the widget deletion event into a custom log file.

    function weplugins_log_widget_deletion($widget_id, $sidebar_id, $id_base){
        $log_message = "Widget with ID $widget_id from sidebar $sidebar_id with base ID $id_base was deleted.";
        error_log($log_message, 3, WP_CONTENT_DIR . '/widget_deletion.log');
    }
    add_action( "delete_widget", "weplugins_log_widget_deletion" , 10, 3);
    

Example 2: Custom Notification

This example sends an email notification to the admin whenever a widget is deleted.

    function weplugins_notify_admin_on_widget_deletion($widget_id, $sidebar_id, $id_base){
        $admin_email = get_option('admin_email');
        $subject = "Widget Deleted";
        $message = "Widget with ID $widget_id from sidebar $sidebar_id with base ID $id_base was deleted.";
        wp_mail($admin_email, $subject, $message);
    }
    add_action( "delete_widget", "weplugins_notify_admin_on_widget_deletion" , 10, 3);
    

Example 3: Update Custom Database Table

This example updates a custom database table when a widget is deleted.

    function weplugins_update_custom_table_on_widget_deletion($widget_id, $sidebar_id, $id_base){
        global $wpdb;
        $table_name = $wpdb->prefix . 'widget_deletions';
        $wpdb->insert($table_name, array(
            'widget_id' => $widget_id,
            'sidebar_id' => $sidebar_id,
            'id_base' => $id_base,
            'deleted_at' => current_time('mysql')
        ));
    }
    add_action( "delete_widget", "weplugins_update_custom_table_on_widget_deletion" , 10, 3);
    

Access Premium WordPress Plugins

Contact Us

If you need any customization or have any questions, feel free to 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.