Skip to content

Instantly share code, notes, and snippets.

@groucho75
Last active November 25, 2021 05:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groucho75/191005961f5aae53ad04 to your computer and use it in GitHub Desktop.
Save groucho75/191005961f5aae53ad04 to your computer and use it in GitHub Desktop.
WP - How to Flush Rewrite Rules for all sites on Multisite
<?php
/**
* Flush permalink on EVERY site on a mulsitite, so you don't have to visit
* Permalink dashboard page of every single blog.
*
* How to use:
* - upload it in /wp-content/mu-plugins/
* - leave the file there for some days
* - then REMOVE the file!
*/
add_action('init', function() {
if ( !is_multisite() ) {
return;
}
$sites = wp_get_sites( array( 'limit' => false ) );
foreach ( $sites as $site ) {
// Flush sometimes, not on evey page load
if ( get_transient( 'flushed_rules_'.$site['blog_id'] ) ) {
continue;
}
switch_to_blog( $site['blog_id'] );
flush_rewrite_rules( false );
error_log( __FILE__ . '(' . __LINE__ . ') ' . __METHOD__ . '(): ' . 'flushed blod ID ' . $site['blog_id'] );
set_transient( 'flushed_rules_'.$site['blog_id'], 'yes', 30 * DAY_IN_SECONDS );
restore_current_blog();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment