admin管理员组文章数量:1023876
I am trying to create a custom block for Gutenberg. So I tried something simple as this:
JS File:
var el = wp.element.createElement;
wp.blocks.registerBlockType('test/blocks', {
title: 'Test Blocks',
icon: 'smiley',
category: 'common',
attributes: {
content: {type: 'string'}
}
},
edit: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
},
save: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
})
The js file is part of a plugin I made and it gets loaded by the .php file. The problem is it doesn't even appear as a block option inside the Gutenberg editor. So my questions are:
- What's wrong with my current code?
- How can I add some fields? TextEdit, Button, etc?
- Is it possible to have multiple fields like a repeater?
- Why everyone makes a plugin for this? Why not just load the js file from functions.php?
I am trying to create a custom block for Gutenberg. So I tried something simple as this:
JS File:
var el = wp.element.createElement;
wp.blocks.registerBlockType('test/blocks', {
title: 'Test Blocks',
icon: 'smiley',
category: 'common',
attributes: {
content: {type: 'string'}
}
},
edit: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
},
save: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
})
The js file is part of a plugin I made and it gets loaded by the .php file. The problem is it doesn't even appear as a block option inside the Gutenberg editor. So my questions are:
- What's wrong with my current code?
- How can I add some fields? TextEdit, Button, etc?
- Is it possible to have multiple fields like a repeater?
- Why everyone makes a plugin for this? Why not just load the js file from functions.php?
1 Answer
Reset to default 1to 1. You closed registerBlockType too early and to avoid further react errors, you should print your return in the same line as return
in your edit/save functions.
var el = wp.element.createElement;
wp.blocks.registerBlockType('test/blocks', {
title: 'Test Blocks',
icon: 'smiley',
category: 'common',
attributes: {
content: {type: 'string'}
},
edit: function(props) {
return el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
)
},
save: function(props) {
return el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
}
});
to 2. You can find elements in the Gutenberg Handbook here. Notice the search field in the top.
to 3. Where do you want to have multiple fields for what?
to 4. If the block is a plugin it is easy to load and to extend the files. You can also register editor and frontend css files for your block, and it's all in the box. ;o)
I am trying to create a custom block for Gutenberg. So I tried something simple as this:
JS File:
var el = wp.element.createElement;
wp.blocks.registerBlockType('test/blocks', {
title: 'Test Blocks',
icon: 'smiley',
category: 'common',
attributes: {
content: {type: 'string'}
}
},
edit: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
},
save: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
})
The js file is part of a plugin I made and it gets loaded by the .php file. The problem is it doesn't even appear as a block option inside the Gutenberg editor. So my questions are:
- What's wrong with my current code?
- How can I add some fields? TextEdit, Button, etc?
- Is it possible to have multiple fields like a repeater?
- Why everyone makes a plugin for this? Why not just load the js file from functions.php?
I am trying to create a custom block for Gutenberg. So I tried something simple as this:
JS File:
var el = wp.element.createElement;
wp.blocks.registerBlockType('test/blocks', {
title: 'Test Blocks',
icon: 'smiley',
category: 'common',
attributes: {
content: {type: 'string'}
}
},
edit: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
},
save: function(props) {
return
el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
})
The js file is part of a plugin I made and it gets loaded by the .php file. The problem is it doesn't even appear as a block option inside the Gutenberg editor. So my questions are:
- What's wrong with my current code?
- How can I add some fields? TextEdit, Button, etc?
- Is it possible to have multiple fields like a repeater?
- Why everyone makes a plugin for this? Why not just load the js file from functions.php?
1 Answer
Reset to default 1to 1. You closed registerBlockType too early and to avoid further react errors, you should print your return in the same line as return
in your edit/save functions.
var el = wp.element.createElement;
wp.blocks.registerBlockType('test/blocks', {
title: 'Test Blocks',
icon: 'smiley',
category: 'common',
attributes: {
content: {type: 'string'}
},
edit: function(props) {
return el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
)
},
save: function(props) {
return el( 'div', {},
el( 'p', {}, window.wp.i18n.__( 'Hello World!!!' ) )
);
}
});
to 2. You can find elements in the Gutenberg Handbook here. Notice the search field in the top.
to 3. Where do you want to have multiple fields for what?
to 4. If the block is a plugin it is easy to load and to extend the files. You can also register editor and frontend css files for your block, and it's all in the box. ;o)
本文标签: Gutenberg Custom Block
版权声明:本文标题:Gutenberg Custom Block 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745517609a2154146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论