Пользовательский блок Gutenberg, как отправить данные из файла индекса плагина в файл js
Я зарегистрировал пользовательский блок в основном / индексном php файле плагина.
register_block_type('gutenberg-custom/show-information', array(
'editor_script' => 'gutenberg-show-information',
'style' => 'gutenberg-customblock-css',
));
Я хочу отправить некоторые данные из этого файла PHP в файл js, который содержит фактическую реализацию блока. Причина в том, что мне нужно выполнить вызов API из PHP и использовать этот ответ в параметре selectbox в коде js. см. закомментированный код ниже. Вот код для js.
edit: function(props) {
console.log("props", props);
return el(
Fragment,
{},
el(
InspectorControls,
{},
el(
PanelBody,
{ title: "Show Settings", initialOpen: true },
el(
PanelRow,
{},
el(SelectControl, {
label: "Select Exhibitor",
options: [ // **these options are static I have given but I want these to be dynamic coming from the plugin main file or can be any PHP file
{ label: "Big", value: "100%" },
{ label: "Medium", value: "50%" },
{ label: "Small", value: "25%" }
],
onChange: value => {
props.setAttributes({ exhibitor_id: value });
},
value: props.attributes.exhibitor_id
})
)
)
),
el(
"div",
{},
"[show-information]"
)
);
},
1 ответ
Когда вы ставите свой скрипт редактора в очередь, вы можете "вставлять" переменные, переданные в JS:
wp_register_script('gutenberg-show-information', $pathToScript, [], null, true);
wp_localize_script(
'gutenberg-show-information',
YOURJSOBJECT,
['myVar' => 'foobarbaz'] <--- array of data you want to pass
);
wp_enqueue_script('gutenberg-show-information');
в вашем файле javaScript вы можете получить доступ YOURJSOBJECT.myVar
который должен выводить foobarbaz