diff --git a/inc/class-domain-mapping.php b/inc/class-domain-mapping.php index 0c38e084..8873e4af 100644 --- a/inc/class-domain-mapping.php +++ b/inc/class-domain-mapping.php @@ -491,6 +491,7 @@ public function register_mapped_filters(): void { add_filter('site_url', [$this, 'mangle_url'], -10, 4); add_filter('home_url', [$this, 'mangle_url'], -10, 4); add_filter('option_siteurl', [$this, 'mangle_url'], 20); + add_filter('option_home', [$this, 'mangle_url'], 20); add_filter('theme_file_uri', [$this, 'mangle_url']); add_filter('stylesheet_directory_uri', [$this, 'mangle_url']); diff --git a/inc/compat/class-general-compat.php b/inc/compat/class-general-compat.php index 310a1bdb..a7cd6aa1 100644 --- a/inc/compat/class-general-compat.php +++ b/inc/compat/class-general-compat.php @@ -129,6 +129,17 @@ public function init(): void { */ add_filter('wp_initialize_site_args', [$this, 'fix_new_site_url_scheme'], 10, 3); + /** + * Flush rewrite rules after site duplication. + * + * Cloned sites inherit the template's rewrite_rules option verbatim. + * If CPT permalink slugs were changed after the template was created, + * the cloned site will 404 on those CPT URLs until rules are regenerated. + * + * @since 2.0.24 + */ + add_action('wu_duplicate_site', [$this, 'flush_rewrite_rules_on_new_site'], 20); + /** * Rank Math (Free and Pro) * @@ -501,6 +512,32 @@ public function fix_new_site_url_scheme(array $args, \WP_Site $site, \WP_Network return $args; } + /** + * Flush rewrite rules after site duplication. + * + * Cloned sites inherit the template's rewrite_rules option verbatim. + * If CPT permalink slugs were changed after the template was created, + * the cloned site will 404 on those CPT URLs until rules are regenerated. + * + * @since 2.0.24 + * @param int|object $new_site The new site ID or data object. + * @return void + */ + public function flush_rewrite_rules_on_new_site($new_site): void { + + $blog_id = is_object($new_site) && isset($new_site->blog_id) + ? (int) $new_site->blog_id + : (int) $new_site; + + if ($blog_id < 2) { + return; + } + + switch_to_blog($blog_id); + flush_rewrite_rules(true); + restore_current_blog(); + } + /** * Removes RankMath and RankMath Pro Installer::wpmu_new_blog action * This registered action causes error on database during site creation