可能只是有一些我找不到的小问题,但它就是行不通。 我正在尝试在 Wordpress 中创建一个带有嵌套内部块的自定义动态块。目前,它似乎可以在古腾堡编辑部分工作,但不能再进一步了。当我单击“更新页面”并刷新时,它不会保存。 我真的不知道了,任何帮助将不胜感激。
只需从 wetransfer.com 下载插件:wetransfer.link 并将其解压到 wordpress/wp-content/plugins 中。 或者您可以像这样创建主题:在 WordPress 插件文件夹中,创建一个名为“block_dynamic_newtype”的文件夹并添加以下文件:
block.json:
{
"apiVersion": 2,
"title": "Block - dynamic - newtype",
"name": "blockexample/block-dynamic-newtype",
"category": "custom-fno-category",
"icon": "star-filled",
"editorScript": "file:./block.js"
}
block.asset.php:
<?php return array('dependencies' => array('wp-blocks', 'wp-element', 'wp-i18n' ), 'version' => 'a35cc1c098b69994c9c6d6dc1416bb90');
index.php:
<?php
/**
* Plugin Name: Block - dynamic - newtype
*/
function block_dynamic_newtype_register_render_callback( $attr, $content ) {
//var_dump($attr);
//var_dump($content);
return '<div style="border: 1px solid black;">This is content of dynamic block: '.$attr['content'].'
place for other blocks:<br>
<div>'.$content.'</div>
</div>';
}
function block_dynamic_newtype_register() {
// automatically load dependencies and version
$asset_file = include( plugin_dir_path( __FILE__ ) . 'block.asset.php');
wp_register_script(
'block-dynamic-newtype',
plugins_url( 'block.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version']
);
register_block_type( 'blockexample/block-dynamic-newtype', array(
'api_version' => 2,
'editor_script' => 'block-dynamic-newtype',
'render_callback' => 'block_dynamic_newtype_register_render_callback'
));
}
add_action( 'init', 'block_dynamic_newtype_register' );
?>
block.js:
console.log('block dynamic newtype loaded');
( function ( blocks, blockEditor, data, element ) {
var el = element.createElement;
var RichText = blockEditor.RichText;
var InnerBlocks = blockEditor.InnerBlocks;
var useBlockProps = blockEditor.useBlockProps;
blocks.registerBlockType( 'blockexample/block-dynamic-newtype', {
apiVersion: 2,
title: 'Block - dynamic - newtype',
icon: 'star-filled',
category: 'custom-fno-category',
attributes: {
content: {
type: 'string'
},
},
edit: function (props) {
var blockProps = useBlockProps();
var content = props.attributes.content;
function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}
//return el( 'div', blockProps, el( InnerBlocks ) );
return el(
"div",
blockProps,
el("div", {class: 'fno_block_editor'},
el("h3", null, "Blok - dynamický - newtype"),
el("div", {class: 'block_space'},
el( InnerBlocks )
),
)
);
/*return el(
RichText,
Object.assign( blockProps, {
tagName: 'div',
class: 'testclas',
onChange: onChangeContent,
value: content,
}),
);*/
},
save: function( props ) {
return (InnerBlocks.Content);
},
} );
} )( window.wp.blocks, window.wp.blockEditor, window.wp.data, window.wp.element );
Wordpress 版本是 6
在block.js中,save函数需要看起来像这样(即使它是动态块,所以它不应该有任何保存函数):
save: props => {
return <InnerBlocks.Content />
}