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

How to use doing_it_wrong_run action in WordPress

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

doing_it_wrong_run action

Fires when the given function is being used incorrectly.

To use the doing_it_wrong_run 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 weplugins_execute_on_doing_it_wrong_run_event which takes 3 parameters and we registered it using add_action. The first parameter doing_it_wrong_run is the name of the hook, The second parameter weplugins_execute_on_doing_it_wrong_run_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.

Sometimes, you have to remove a registered hook so you can use remove_action to remove the doing_it_wrong_run action.

Parameters

    Below are the 3 parameters required to use this hook.

  • $function: (string) The function that was called.
  • $message: (string) A message explaining what has been done incorrectly.
  • $version: (string) The version of WordPress where the message was added.

Live Example

Below is an example of how you can use this hook.

    function weplugins_execute_on_doing_it_wrong_run_event($function, $message, $version){
	   //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("doing_it_wrong_run", "weplugins_execute_on_doing_it_wrong_run_event", 10, 3);
    

Example 1: Logging Incorrect Function Usage

In this example, we log the incorrect function usage details to a custom log file.

    function weplugins_log_doing_it_wrong($function, $message, $version){
        error_log("Function $function is being used incorrectly: $message (Added in $version)");
    }
    add_action("doing_it_wrong_run", "weplugins_log_doing_it_wrong", 10, 3);
    

Example 2: Displaying Admin Notice

This example shows how to display an admin notice when an incorrect function usage is detected.

    function weplugins_admin_notice_doing_it_wrong($function, $message, $version){
        add_action('admin_notices', function() use ($function, $message, $version) {
            echo "<div class='notice notice-error'><p>Function $function is being used incorrectly: $message (Added in $version)</p></div>";
        });
    }
    add_action("doing_it_wrong_run", "weplugins_admin_notice_doing_it_wrong", 10, 3);
    

Example 3: Sending Email Notification

In this example, we send an email notification to the site admin when an incorrect function usage is detected.

    function weplugins_email_doing_it_wrong($function, $message, $version){
        $admin_email = get_option('admin_email');
        $subject = "Incorrect Function Usage Detected";
        $body = "Function $function is being used incorrectly: $message (Added in $version)";
        wp_mail($admin_email, $subject, $body);
    }
    add_action("doing_it_wrong_run", "weplugins_email_doing_it_wrong", 10, 3);
    

To remove a hook callback, use the example below.

    remove_action("doing_it_wrong_run", "weplugins_execute_on_doing_it_wrong_run_event", 10, 3);
    

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

Access Premium WordPress Plugins

Contact Us

If you need customization or have any trouble using this hook, please 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.