Using WordPress ‘is_protected_ajax_action()’ PHP function

The is_protected_ajax_action() WordPress PHP function determines whether we are currently handling an Ajax action that should be protected against WSODs (White Screens of Death).

On this pageJump to a section

Usage

To use the function, simply call it in your code like this:

$is_protected = is_protected_ajax_action();

Parameters

This function does not have any parameters.

More information

See WordPress Developer Resources: is_protected_ajax_action

Examples

Check if an Ajax action is protected

This example checks if the current Ajax action is protected and outputs a message accordingly.

$is_protected = is_protected_ajax_action();

if ($is_protected) {
    echo "This Ajax action is protected.";
} else {
    echo "This Ajax action is not protected.";
}

Conditionally load an Ajax action

This example loads a custom Ajax action only if the current action is not protected.

$is_protected = is_protected_ajax_action();

if (!$is_protected) {
    // Register the custom Ajax action
    add_action('wp_ajax_my_custom_action', 'my_custom_action_callback');
}

Disable a plugin if the current Ajax action is protected

This example disables a plugin if the current Ajax action is protected.

$is_protected = is_protected_ajax_action();

if ($is_protected) {
    deactivate_plugins('my-plugin/my-plugin.php');
}

Add a custom filter for protected Ajax actions

This example adds a custom filter that only runs when the current Ajax action is protected.

$is_protected = is_protected_ajax_action();

if ($is_protected) {
    add_filter('my_custom_filter', 'my_custom_filter_callback');
}

Log protected Ajax actions

This example logs protected Ajax actions for debugging purposes.

$is_protected = is_protected_ajax_action();

if ($is_protected) {
    error_log('A protected Ajax action was executed.');
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Type guide in lowercase to submit your comment.