register_block_style_handle

函数
register_block_style_handle ( $metadata, $field_name, $index = 0 )
参数
  • (array) $metadata Block metadata.
    Required:
  • (string) $field_name Field name to pick from metadata.
    Required:
  • (int) $index Optional. Index of the style to register when multiple items passed. Default 0.
    Required:
返回值
  • (string|false) Style handle provided directly or created through style's registration, or false on failure.
定义位置
相关方法
register_block_script_handleregister_block_styleunregister_block_styleregister_block_typeunregister_block_type
引入
5.5.0
弃用
-

register_block_style_handle: 该函数用于注册一个新的块样式句柄,以用于定制块的样式。它需要三个参数:$handle, $style_args, 和$asset_callback。$handle是块样式的名称,$style_args是一个样式选项数组,而$asset_callback是一个为块样式注册样式资产(CSS和JS)的函数。

为块元数据域找到一个样式句柄。它检测是否提供了文件的路径,并在自动生成的句柄名下注册样式。否则,它返回未处理的样式句柄。

function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
	if ( empty( $metadata[ $field_name ] ) ) {
		return false;
	}

	static $wpinc_path_norm = '';
	if ( ! $wpinc_path_norm ) {
		$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
	}

	$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
	// Skip registering individual styles for each core block when a bundled version provided.
	if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
		return false;
	}

	$style_handle = $metadata[ $field_name ];
	if ( is_array( $style_handle ) ) {
		if ( empty( $style_handle[ $index ] ) ) {
			return false;
		}
		$style_handle = $style_handle[ $index ];
	}

	$style_path      = remove_block_asset_path_prefix( $style_handle );
	$is_style_handle = $style_handle === $style_path;
	// Allow only passing style handles for core blocks.
	if ( $is_core_block && ! $is_style_handle ) {
		return false;
	}
	// Return the style handle unless it's the first item for every core block that requires special treatment.
	if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
		return $style_handle;
	}

	// Check whether styles should have a ".min" suffix or not.
	$suffix = SCRIPT_DEBUG ? '' : '.min';
	if ( $is_core_block ) {
		$style_path = "style$suffix.css";
	}

	$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
	$has_style_file  = '' !== $style_path_norm;

	if ( $has_style_file ) {
		$style_uri = plugins_url( $style_path, $metadata['file'] );

		// Cache $theme_path_norm to avoid calling get_theme_file_path() multiple times.
		static $theme_path_norm = '';
		if ( ! $theme_path_norm ) {
			$theme_path_norm = wp_normalize_path( get_theme_file_path() );
		}

		$is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );

		if ( $is_theme_block ) {
			$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
		} elseif ( $is_core_block ) {
			$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
		}
	} else {
		$style_uri = false;
	}

	$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
	$version      = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
	$result       = wp_register_style(
		$style_handle,
		$style_uri,
		array(),
		$version
	);
	if ( ! $result ) {
		return false;
	}

	if ( $has_style_file ) {
		wp_style_add_data( $style_handle, 'path', $style_path_norm );

		$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );

		if ( is_rtl() && file_exists( $rtl_file ) ) {
			wp_style_add_data( $style_handle, 'rtl', 'replace' );
			wp_style_add_data( $style_handle, 'suffix', $suffix );
			wp_style_add_data( $style_handle, 'path', $rtl_file );
		}
	}

	return $style_handle;
}

常见问题

FAQs
查看更多 >