I'm trying to create a link in the top admin bar with a single link to purge all caches. However, after awhile the nonce expires. How can I make this work?

function add_toolbar_items($admin_bar){
    global $wp_admin_bar, $current_user;

    if ($current_user->ID == 1)

    $admin_bar->add_menu( array(
        'id'    => 'purge-all-caches',
        'title' => 'Purge All Caches',
        'href'  => '/wp-admin/admin.php?page=w3tc_dashboard&w3tc_flush_all&_wpnonce=ce99c46c21',
        'meta'  => array(
            'title' => __('Purge All Caches'),            

add_action('admin_bar_menu', 'add_toolbar_items', 100);

1 Answer 1


You should use wp_nonce_url() to generate the URL with a valid nonce rather than hard-coding one, e.g. here's the code from w3_total_cache itself that does this:

'href' => wp_nonce_url( network_admin_url(
        'admin.php?page=w3tc_dashboard&w3tc_flush_all' ),
    'w3tc' )

You could also use a permissions check instead of checking if the user is ID 1, although I appreciate there are benefits to locking this down to a single user too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.