Fixing Global Styles import issue with Merlin WP Importer

Fixing Global Styles import issue with Merlin WP Importer

Global styles features enable users to have control over whole website’s aesthetics and layouts with a single click.

FSE theme developers, with the help of WordPress or Merlin WP Importer tool, can import content from another WordPress site.

However, for custom Global styles, it works well on first time import content. But when we try to import content next time, it doesn’t import global styles at all.

Solution

WordPress stores Global styles as a post content in custom post type called wp_global_styles for the current theme.

We can solve this issue by setting Global styles after content import using an action hook ‘import_end‘ for WordPress Importer and ‘merlin_after_all_import‘ for Merlin WP Importer.

Sample code :

//Execute custom code after the whole import has finished
add_action( 'merlin_after_all_import', 'after_import_setup_global_styles' );

//function to update global styles
function after_import_setup_global_styles( $index ) {
 require_once( ABSPATH . "wp-includes/class-wp-theme-json-resolver.php" );
 $style_post = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( 
                 wp_get_theme()  
              );
 //User defined function to get custom global styles 
 $custom_global_style = get_demo_custom_global_styles( $index );

 //update global styles
 if ( ! empty( $style_post ) && ! empty( $style_post['ID'] ) ) {
    wp_update_post( array(
	'ID'           => $style_post['ID'],
	'post_content' => $custom_global_style,
    ) );			
 }
}