Using WordPress ‘doing_it_wrong_run’ PHP action

The doing_it_wrong_run WordPress action fires when a given function is being used incorrectly.

Usage

add_action('doing_it_wrong_run', 'your_custom_function', 10, 3);

function your_custom_function($function_name, $message, $version) {
    // your custom code here
}

Parameters

  • $function_name (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.

More information

See WordPress Developer Resources: doing_it_wrong_run

Examples

Log incorrect usage to a file

Log incorrect function usage to a file for further analysis.

add_action('doing_it_wrong_run', 'log_incorrect_usage', 10, 3);

function log_incorrect_usage($function_name, $message, $version) {
    $log_file = fopen('wp-content/incorrect_usage.log', 'a');
    $log_message = date('Y-m-d H:i:s') . " - Function: $function_name, Message: $message, Version: $version" . PHP_EOL;
    fwrite($log_file, $log_message);
    fclose($log_file);
}

Email incorrect usage to the administrator

Send an email to the administrator when a function is used incorrectly.

add_action('doing_it_wrong_run', 'email_incorrect_usage', 10, 3);

function email_incorrect_usage($function_name, $message, $version) {
    $to = get_option('admin_email');
    $subject = "Incorrect function usage detected";
    $email_message = "Function: $function_name\nMessage: $message\nVersion: $version";
    wp_mail($to, $subject, $email_message);
}

Display incorrect usage as an admin notice

Show an admin notice in the WordPress dashboard when a function is used incorrectly.

add_action('doing_it_wrong_run', 'admin_notice_incorrect_usage', 10, 3);

function admin_notice_incorrect_usage($function_name, $message, $version) {
    add_action('admin_notices', function () use ($function_name, $message, $version) {
        echo "<div class='notice notice-error'><p>Incorrect function usage detected: Function <strong>$function_name</strong>, Message: $message, Version: $version</p></div>";
    });
}

Disable a plugin when a function is used incorrectly

Deactivate a specific plugin when a function is used incorrectly.

add_action('doing_it_wrong_run', 'deactivate_plugin_on_incorrect_usage', 10, 3);

function deactivate_plugin_on_incorrect_usage($function_name, $message, $version) {
    if ($function_name == 'specific_function_name') {
        deactivate_plugins('plugin-directory/plugin-file.php');
    }
}

Limit incorrect usage alerts to a specific version

Only trigger custom code for incorrect function usage in a specific WordPress version.

add_action('doing_it_wrong_run', 'specific_version_incorrect_usage', 10, 3);

function specific_version_incorrect_usage($function_name, $message, $version) {
    if ($version == '5.5.0') {
        // your custom code here
    }
}