serialize_block_attributes

函式
serialize_block_attributes ( $block_attributes )
引數
  • (array) $block_attributes Attributes object.
    Required:
返回值
  • (string) Serialized attributes.
定義位置
相關方法
serialize_blocksserialize_blockwp_pre_kses_block_attributeswp_sanitize_script_attributeswp_kses_uri_attributes
引入
5.3.1
棄用
-

serialize_block_attributes: 這是一個WordPress的函式,它將一個塊的屬性序列化為一個字串。它通常用於將塊的屬性儲存到資料庫或通過網路傳送: 這個函式需要一個引數,即屬性陣列。

給定一個屬性陣列,返回一個為文章內容準備的序列化屬性格式的字串。

序列化的結果是一個JSON編碼的字串,用unicode轉義序列替代那些可能影響結果嵌入到HTML註釋中的字元。

這個函式產生的輸出必須與塊編輯器中serializeAttributes JavaScript函式的輸出保持同步,以確保PHP和JavaScript之間的操作一致。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function serialize_block_attributes( $block_attributes ) {
$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
$encoded_attributes = preg_replace( '/--/', '\u002d\u002d', $encoded_attributes );
$encoded_attributes = preg_replace( '/</', '\u003c', $encoded_attributes );
$encoded_attributes = preg_replace( '/>/', '\u003e', $encoded_attributes );
$encoded_attributes = preg_replace( '/&/', '\u0026', $encoded_attributes );
// Regex: /\"/
$encoded_attributes = preg_replace( '/\\"/', '\u0022', $encoded_attributes );
return $encoded_attributes;
}
function serialize_block_attributes( $block_attributes ) { $encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $encoded_attributes = preg_replace( '/--/', '\u002d\u002d', $encoded_attributes ); $encoded_attributes = preg_replace( '/</', '\u003c', $encoded_attributes ); $encoded_attributes = preg_replace( '/>/', '\u003e', $encoded_attributes ); $encoded_attributes = preg_replace( '/&/', '\u0026', $encoded_attributes ); // Regex: /\"/ $encoded_attributes = preg_replace( '/\\"/', '\u0022', $encoded_attributes ); return $encoded_attributes; }
function serialize_block_attributes( $block_attributes ) {
	$encoded_attributes = wp_json_encode( $block_attributes, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
	$encoded_attributes = preg_replace( '/--/', '\u002d\u002d', $encoded_attributes );
	$encoded_attributes = preg_replace( '/</', '\u003c', $encoded_attributes );
	$encoded_attributes = preg_replace( '/>/', '\u003e', $encoded_attributes );
	$encoded_attributes = preg_replace( '/&/', '\u0026', $encoded_attributes );
	// Regex: /\"/
	$encoded_attributes = preg_replace( '/\\"/', '\u0022', $encoded_attributes );

	return $encoded_attributes;
}

常見問題

FAQs
檢視更多 >