wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_enqueue_global_styles › WordPress Function

Since5.8.0
Deprecatedn/a
wp_enqueue_global_styles ( No parameters )
Defined at:
Codex:

Enqueues the global styles defined via theme.json.



Source

function wp_enqueue_global_styles() {
	if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
		return;
	}

	$separate_assets = wp_should_load_separate_core_block_assets();

	/*
	 * Global styles should be printed in the head when loading all styles combined.
	 * The footer should only be used to print global styles for classic themes with separate core assets enabled.
	 *
	 * See https://core.trac.wordpress.org/ticket/53494.
	 */
	if ( ( ! $separate_assets && doing_action( 'wp_footer' ) ) || ( $separate_assets && doing_action( 'wp_enqueue_scripts' ) ) ) {
		return;
	}

	$can_use_cache = (
		( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
		( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
		( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
		! is_admin()
	);

	$stylesheet = null;
	$transient_name = 'global_styles_' . get_stylesheet();
	if ( $can_use_cache ) {
		$cache = get_transient( $transient_name );
		if ( $cache ) {
			$stylesheet = $cache;
		}
	}

	if ( null === $stylesheet ) {
		$settings   = get_default_block_editor_settings();
		$theme_json = WP_Theme_JSON_Resolver::get_merged_data( $settings );
		$stylesheet = $theme_json->get_stylesheet();

		if ( $can_use_cache ) {
			set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
		}
	}

	if ( empty( $stylesheet ) ) {
		return;
	}

	wp_register_style( 'global-styles', false, array(), true, true );
	wp_add_inline_style( 'global-styles', $stylesheet );
	wp_enqueue_style( 'global-styles' );
}