我正在重写PHP中的核心图像块。一切都很顺利,只是我发现Javascript中该块的所有属性都没有传递给PHP供我使用。
如何覆盖图像块:
//Override core/image block with render callback
function override_image_block_output($attributes, $content){
//Override the image block
var_dump($attributes);
}
function reregister_image_block(){
register_block_type( \'core/image\', array(
\'render_callback\' => \'override_image_block_output\',
) );
}
add_action(\'init\', \'reregister_image_block\');
当我这样做,var\\u转储$attributes
, 我只有这些array(3) { ["id"]=> int(282) ["sizeSlug"]=> string(5) "large" ["linkDestination"]=> string(6) "custom" }
但在Javascript中,您可以通过以下方式查看该块的属性:const { addFilter } = wp.hooks;
const __ = wp.i18n.__; // The __() for internationalization.
const el = wp.element.createElement; // The wp.element.createElement() function to create elements.
const { RichText } = wp.blockEditor;
const { InnerBlocks } = wp.blockEditor;
const OverrideImageBlock = (element, blockType, attributes) => {
if ( blockType.name === \'core/image\' ) {
console.log(attributes);
return element;
}
return element;
};
addFilter(
"blocks.getSaveElement",
"custom-blocks/override-core-image-block",
OverrideImageBlock
);
属性返回以下内容:
如何将“href”从块的属性传递到PHP?为什么有些通过了,但有些最初没有通过?