admin管理员组文章数量:1287191
I'm building a custom gutenberg block that should have a dropdown (SelectControl) to select which css class should be added to my block. So far, nothing special. My problem is that depending on the theme I'm using, the available classes change. So for ThemeA, the available classes are "test1, test2, test3" and for ThemeB "test2, test4". So how can my theme tell my block which classes are available? I'm thinking of a solution in which my theme forwards the classes to my block. But I can't find any hooks or other wordpress functions to inject data from my theme into my editor js frontend (into my SelectControl).
Variants are no option because the available classes change. I have a solution how to apply the classes to my block, I just don't know how the extend the SelectControl in the InspectorControls.
I'm building a custom gutenberg block that should have a dropdown (SelectControl) to select which css class should be added to my block. So far, nothing special. My problem is that depending on the theme I'm using, the available classes change. So for ThemeA, the available classes are "test1, test2, test3" and for ThemeB "test2, test4". So how can my theme tell my block which classes are available? I'm thinking of a solution in which my theme forwards the classes to my block. But I can't find any hooks or other wordpress functions to inject data from my theme into my editor js frontend (into my SelectControl).
Variants are no option because the available classes change. I have a solution how to apply the classes to my block, I just don't know how the extend the SelectControl in the InspectorControls.
Share Improve this question edited Oct 4, 2021 at 20:19 Jan-Cedric asked Oct 4, 2021 at 19:20 Jan-CedricJan-Cedric 1112 bronze badges 4 |1 Answer
Reset to default 0You can use wp_localize_script()
to pass JavaScript variables to the editor:
$asset_file = include(get_template_directory() . '/path/to/blocks/index.asset.php');
wp_register_script(
'pb-theme-blocks',
get_template_directory_uri() . '/path/to/blocks/index.js',
$asset_file['dependencies'],
$asset_file['version']
);
wp_localize_script(
'pb-theme-blocks',
'pbThemeBlocksVars',
array(
'classes' => array(), // Your theme classes go here
)
);
Then in your edit
function:
const classes = pbThemeBlocksVars.classes;
本文标签: phpHow to extend SelectControl with data from my theme
版权声明:本文标题:php - How to extend SelectControl with data from my theme 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741285983a2370270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
core/embed
, yet they show up with different UIs, names and icons in the inserter. The only difference between them is attribute values. The same with lots of other blocks, they're all the same block except their html class attribute is different – Tom J Nowell ♦ Commented Oct 4, 2021 at 22:11