Last active
November 25, 2021 05:42
-
-
Save groucho75/191005961f5aae53ad04 to your computer and use it in GitHub Desktop.
WP - How to Flush Rewrite Rules for all sites on Multisite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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