render_block_core_file

函式
render_block_core_file ( $attributes, $content )
引數
  • (array) $attributes The block attributes.
    Required:
  • (string) $content The block content.
    Required:
返回值
  • (string) Returns the block content.
定義位置
相關方法
render_block_core_imageregister_block_core_filerender_block_core_site_titlerender_block_core_rssrender_block_core_block
引入
-
棄用
-

render_block_core_file: 這個函式用來渲染WordPress中的檔案塊。檔案塊允許使用者在其網站上上傳和顯示檔案,如PDF或其他檔案: 這個函式負責生成檔案塊的HTML標記。

當”core/file”區塊正在渲染時,檢查我們是否需要排隊等待”‘wp-block-file-view”指令碼。

function render_block_core_file( $attributes, $content ) {
	$should_load_view_script = ! empty( $attributes['displayPreview'] ) && ! wp_script_is( 'wp-block-file-view' );
	if ( $should_load_view_script ) {
		wp_enqueue_script( 'wp-block-file-view' );
	}

	// Update object's aria-label attribute if present in block HTML.

	// Match an aria-label attribute from an object tag.
	$pattern = '@<object.+(?<attribute>aria-label="(?<filename>[^"]+)?")@i';
	$content = preg_replace_callback(
		$pattern,
		function ( $matches ) {
			$filename     = ! empty( $matches['filename'] ) ? $matches['filename'] : '';
			$has_filename = ! empty( $filename ) && 'PDF embed' !== $filename;
			$label        = $has_filename ?
				sprintf(
					/* translators: %s: filename. */
					__( 'Embed of %s.' ),
					$filename
				)
				: __( 'PDF embed' );

			return str_replace( $matches['attribute'], sprintf( 'aria-label="%s"', $label ), $matches[0] );
		},
		$content
	);

	return $content;
}

常見問題

FAQs
檢視更多 >