Halloween Sale 15% off Ends in See the plans
PHP

Flush Permalinks From the Admin Bar

Adds a "Flush permalinks" link to the admin bar for administrators that rebuilds the rewrite rules in one click, handy after registering post types or custom rewrite rules.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'admin_bar_menu', function ( $admin_bar ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $admin_bar->add_node( array(
        'id'     => 'wpide-flush-rewrites',
        'parent' => 'site-name',
        'title'  => 'Flush permalinks',
        'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=wpide_flush_rewrites' ), 'wpide_flush_rewrites' ),
    ) );
}, 100 );

add_action( 'admin_post_wpide_flush_rewrites', function () {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Not allowed.', 403 );
    }
    check_admin_referer( 'wpide_flush_rewrites' );

    flush_rewrite_rules();

    wp_safe_redirect( add_query_arg( 'wpide_flushed', 1, wp_get_referer() ? wp_get_referer() : admin_url() ) );
    exit;
} );

add_action( 'admin_notices', function () {
    if ( isset( $_GET['wpide_flushed'] ) && current_user_can( 'manage_options' ) ) {
        echo '<div class="notice notice-success is-dismissible"><p>Permalinks flushed.</p></div>';
    }
} );

Notes

Same effect as saving Settings > Permalinks; the link is protected with a nonce.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.