Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

Ricardo on "Force rewrite rules when updating in Multisite"

$
0
0

In many of my WP sites, custom post type and taxonomy URLs return 404 errors right after certain updates. Fixing this is as easy as flushing rewrite rules visiting the Permalinks page, but when you're updating multiple sites at once this can be very annoying.

So I thought a possible solution could be to force a flush right after any plugin, theme or core update. And I came pretty close:

function flush_after_update() {
	global $wp_rewrite;
	//If multisite, we loop through all sites
	if (is_multisite()) {
		global $wpdb;
		//Pick up all active sites
		$sites = $wpdb->get_results( $wpdb->prepare( "SELECT blog_id, domain FROM wp_blogs WHERE archived = '0' AND spam = '0' AND deleted = '0' ORDER BY blog_id", '' ) );
		//Loop through them
		foreach ($sites as $site) {
			//Switch to current site
			switch_to_blog($site->blog_id);
			//Rebuild rewrite rules for this site
			$wp_rewrite->init();
			//Flush them
			$wp_rewrite->flush_rules();
			echo 'Rewrite rules flushed for site ' . $site->blog_id . ' (' . $site->domain . ').<br />';
		}
		//Back to normal
		restore_current_blog();
		$wp_rewrite->init();
	} else {
		//Flush rewrite rules
		$wp_rewrite->flush_rules();
		echo 'Rewrite rules flushed.';
	}
}
//Hook it up
add_filter('upgrader_post_install', 'flush_after_update', 10, 2);

The single-site part works just fine. The problem is in Multisite, and I think it is because $wp_rewrite->init() doesn't bring the rules for custom post types and taxonomies.

What do you think? Can this code be fixed? Does anyone have a better approach?

Thanks in advance,

Ricardo


Viewing all articles
Browse latest Browse all 8245

Trending Articles