函数文档

strip_core_block_namespace()

💡 云策文档标注

概述

strip_core_block_namespace() 函数用于序列化时移除块名称中的默认“core/”命名空间,返回处理后的块名称。它处理字符串类型的块名,并支持 null 值输入。

关键要点

  • 函数功能:移除块名称中的“core/”前缀,适用于序列化场景
  • 参数:$block_name 为可选的原始块名称,可以是字符串或 null(例如 Classic blocks 的块名为 null)
  • 返回值:返回字符串类型的块名称,用于序列化
  • 实现逻辑:检查 $block_name 是否为字符串且以“core/”开头,如果是则移除前5个字符,否则返回原值
  • 引入版本:WordPress 5.3.1

代码示例

function strip_core_block_namespace( $block_name = null ) {
    if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
        return substr( $block_name, 5 );
    }

    return $block_name;
}

📄 原文内容

Returns the block name to use for serialization. This will remove the default “core/” namespace from a block name.

Parameters

$block_namestring|nulloptional
Original block name. Null if the block name is unknown, e.g. Classic blocks have their name set to null.

Default:null

Return

string Block name to use for serialization.

Source

function strip_core_block_namespace( $block_name = null ) {
	if ( is_string( $block_name ) && str_starts_with( $block_name, 'core/' ) ) {
		return substr( $block_name, 5 );
	}

	return $block_name;
}

Changelog

Version Description
5.3.1 Introduced.