Using Gravity Forms ‘gform_admin_error_messages’ PHP action

The gform_admin_error_messages action allows you to modify error messages displayed by Gravity Forms in the WordPress admin.

Usage

add_action('gform_admin_error_messages', 'my_custom_function');

Parameters

  • $error_messages (array): An array of error messages to be displayed below the title on Gravity Form pages.

More information

See Gravity Forms Docs: gform_admin_error_messages

Examples

Remove an error message

This example demonstrates how to remove a specific error message.

add_action('gform_admin_error_messages', 'my_remove_error_message');

function my_remove_error_message($errors) {
    // Loop through error messages
    for ($i = count($errors); $i > 0; $i--) {
        // Check if this is the error message to remove
        if ($errors[$i] == __('Please select the fields to be exported', 'gravityforms')) {
            unset($errors[$i]);
        }
    }
    return $errors;
}

Add a custom error message

This example demonstrates how to add a custom error message.

add_action('gform_admin_error_messages', 'my_add_error_message');

function my_add_error_message($errors) {
    // Add custom error message
    $errors[] = __('This is a custom error message', 'gravityforms');
    return $errors;
}

Replace an error message

This example shows how to replace a specific error message with a custom one.

add_action('gform_admin_error_messages', 'my_replace_error_message');

function my_replace_error_message($errors) {
    // Loop through error messages
    foreach ($errors as $key => $error) {
        // Check if this is the error message to replace
        if ($error == __('This field is required.', 'gravityforms')) {
            $errors[$key] = __('Please fill out this field.', 'gravityforms');
        }
    }
    return $errors;
}

Clear all error messages

This example demonstrates how to clear all error messages.

add_action('gform_admin_error_messages', 'my_clear_error_messages');

function my_clear_error_messages($errors) {
    // Clear all error messages
    $errors = array();
    return $errors;
}

Modify error messages based on a condition

This example shows how to modify error messages based on a specific condition.

add_action('gform_admin_error_messages', 'my_modify_error_messages_based_on_condition');

function my_modify_error_messages_based_on_condition($errors) {
    // Check if a specific condition is met
    if (current_user_can('editor')) {
        // Modify error messages for editors
        foreach ($errors as $key => $error) {
            $errors[$key] = __('(Editor) ' . $error, 'gravityforms');
        }
    }
    return $errors;
}