Я пытаюсь создать настраиваемый блок в Гутенберге, который включает загрузку изображения. Проблема в том, что render
в MediaUpload
не работает. Думаю, что-то упускаю, но не могу найти.
Всякий раз, когда я пытаюсь поместить элемент MediaUpload
в блок, он оказывается пустым. В приведенном ниже коде, если вы проверите его, вы увидите <div class="image-test">
, но внутри него ничего не будет.
Я попытался упростить приведенный ниже код, чтобы убедиться, что ему ничего не мешает, но у меня это все равно не сработало.
Это код block.js
:
const { registerBlockType } = wp.blocks;
const { MediaUpload, MediaUploadCheck } = wp.editor;
const { Button } = wp.components;
registerBlockType( 'custom/image-test', {
title: 'Image Test',
icon: 'warning',
category: 'custom-blocks',
edit( { attributes, className, setAttributes } ) {
return (
<div className="image-test">
<MediaUploadCheck>
<MediaUpload
onSelect={ media => console.log( media.length ) }
render={ ({ open }) => (
<Button onClick={ open }>
Open Media Library
</Button>
)}
/>
</MediaUploadCheck>
</div>
);
},
save( { attributes } ) {
return (
<div class="image-test">
<p>Image Test</p>
</div>
);
},
} );
Здесь я объявляю блок в функциях:
function image_test_block(){
wp_register_script(
'image-test-script', // name of script
get_template_directory_uri() . '/js/block-image-test.js', // path to script
array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components' ) // dependencies
);
register_block_type('custom/image-test', array(
'editor_script' => 'image-test-script'
) );
}
add_action( 'init', 'image_test_block', 10, 0 );