Using WordPress ‘customize_refresh_nonces’ PHP filter

The customize_refresh_nonces WordPress PHP filter allows you to modify the nonces for the Customizer in WordPress.

Usage

add_filter('customize_refresh_nonces', 'my_custom_function', 10, 2);

function my_custom_function($nonces, $manager) {
    // your custom code here
    return $nonces;
}

Parameters

  • $nonces (string[]): An array of refreshed nonces for save and preview actions.
  • $manager (WP_Customize_Manager): An instance of WP_Customize_Manager.

More information

See WordPress Developer Resources: customize_refresh_nonces

Examples

Change nonces for custom actions

Modify the nonces array to include custom actions.

add_filter('customize_refresh_nonces', 'add_custom_action_nonce', 10, 2);

function add_custom_action_nonce($nonces, $manager) {
    $nonces['custom_action'] = wp_create_nonce('custom_action_nonce');
    return $nonces;
}

Add a prefix to existing nonces

Add a prefix to the existing nonces in the array.

add_filter('customize_refresh_nonces', 'prefix_nonces', 10, 2);

function prefix_nonces($nonces, $manager) {
    foreach ($nonces as $key => $value) {
        $nonces[$key] = 'prefix_' . $value;
    }
    return $nonces;
}

Remove a specific nonce

Remove a specific nonce from the nonces array.

add_filter('customize_refresh_nonces', 'remove_specific_nonce', 10, 2);

function remove_specific_nonce($nonces, $manager) {
    unset($nonces['preview']);
    return $nonces;
}

Change nonce lifetime

Change the lifetime of the nonces by modifying the expiration time.

add_filter('nonce_life', 'change_nonce_lifetime');

function change_nonce_lifetime() {
    return 12 * HOUR_IN_SECONDS;
}

Conditionally modify nonces

Modify the nonces only if a specific condition is met.

add_filter('customize_refresh_nonces', 'conditionally_modify_nonces', 10, 2);

function conditionally_modify_nonces($nonces, $manager) {
    if (current_user_can('edit_theme_options')) {
        $nonces['save'] = wp_create_nonce('custom_save_nonce');
    }
    return $nonces;
}