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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.