我无法为 wordpress 站点注册 gutenberg 块,当我看到控制台显示错误时
'Uncaught TypeError: wp.blocks.blockRegisterType is not a function at custom-cta-script.js?ver=1:1:11'
有人可以帮忙修吗?
入队 js 文件的 PHP 代码:
function include_cta_js()
{
wp_enqueue_script('custom-cta-script', plugin_dir_url(__FILE__) . 'custom-cta-script.js', array(
'wp-blocks', 'wp-editor'
), true, false);
}
add_action('enqueue_block_editor_assets', 'include_cta_js');
JS文件:
wp.blocks.blockRegisterType('custom-cta-script/custom-block', {
title: 'CTA',
icon: 'smiley',
category: 'text',
attributes: {
text: { type: 'string' },
url: { type: 'string' },
backgroundColor: { type: 'string' },
textColor: { type: 'string' }
},
edit: function (props) {
return React.createElement("div", null, /*#__PURE__*/React.createElement("label", null, "Button Text"), /*#__PURE__*/React.createElement("input", {
type: "text",
value: "",
placeholder: "Button Text"
}), /*#__PURE__*/React.createElement("label", null, "Button URL"), /*#__PURE__*/React.createElement("input", {
type: "text",
value: "",
placeholder: "Button URL"
}), /*#__PURE__*/React.createElement("label", null, "Button Background Color"), /*#__PURE__*/React.createElement("input", {
type: "text",
value: "",
placeholder: "Button Background Color"
}), /*#__PURE__*/React.createElement("label", null, "Button Text Color"), /*#__PURE__*/React.createElement("input", {
type: "text",
value: "",
placeholder: "Button Text Color"
}));
},
save: function (props) {
return null;
}
});
从 Gutenberg 5.8 版本开始,函数
wp.blocks.registerBlockType()
替换了 wp.blocks.blockRegisterType()
以注册自定义块。因此,您应该在 JavaScript 代码中将 wp.blocks.blockRegisterType
替换为 wp.blocks.registerBlockType
。