You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've created a custom block from an example I found online
When I'm adding the block to the page editor, The RichText components are rendered but the MediaUpload component is not.
What am I doing wrong?
This is my code:
(function(blocks,components,i18n,element){varel=element.createElement;varRichText=wp.editor.RichText;// For creating editable elements.varMediaUpload=wp.editor.MediaUpload;varIconButton=components.IconButton;blocks.registerBlockType(// The name of our block. Must be a string with prefix. Example: my-plugin/my-custom-block.'gutenberg-blocks/testblock',{// The title of our block.title: i18n.__('Testimonial'),// Dashicon icon for our block.icon: 'megaphone',// The category of the block.category: 'common',// Necessary for saving block content.attributes: {name: {type: 'array',source: 'children',selector: 'p.nelio-testimonial-name',},position: {type: 'array',source: 'children',selector: 'p.nelio-testimonial-position',},testimonial: {type: 'array',source: 'children',selector: 'p',},mediaID: {type: 'number',},mediaURL: {type: 'string',source: 'attribute',selector: 'img',attribute: 'src',},alignment: {type: 'string',default: 'center',}},edit: function(props){varfocus=props.focus;varfocusedEditable=props.focus ? props.focus.editable||'name' : null;varalignment=props.attributes.alignment;varattributes=props.attributes;varcontactURL=props.attributes.contactURL;varonSelectImage=function(media){returnprops.setAttributes({mediaURL: media.url,mediaID: media.id,});};functiononChangeAlignment(newAlignment){props.setAttributes({alignment: newAlignment});}return[!!focus&&el(// Display controls when the block is clicked on.blocks.BlockControls,{key: 'controls'},el(blocks.AlignmentToolbar,{value: alignment,onChange: onChangeAlignment,}),),el('div',{className: props.className},el('div',{className: attributes.mediaID ? 'nelio-testimonial-image image-active' : 'nelio-testimonial-image image-inactive',style: attributes.mediaID ? {backgroundImage: 'url('+attributes.mediaURL+')'} : {}},el(MediaUpload,{onSelect: onSelectImage,type: 'image',value: attributes.mediaID,render: function(obj){returnel(IconButton,{className: attributes.mediaID ? 'image-button' : 'button button-large',onClick: obj.open},!attributes.mediaID ? i18n.__('Upload Image') : el('img',{src: attributes.mediaURL}));}})),el('div',{className: 'nelio-testimonial-content',style: {textAlign: alignment}},el(RichText,{tagName: 'p',inline: true,placeholder: i18n.__('Write the testimonial here...'),value: attributes.testimonial,onChange: function(newTestimonial){props.setAttributes({testimonial: newTestimonial});},focus: focusedEditable==='testimonial' ? focus : null,onFocus: function(focus){props.setFocus(_.extend({},focus,{editable: 'testimonial'}));},}),el(RichText,{tagName: 'p',className: 'nelio-testimonial-name',inline: false,placeholder: i18n.__('Name'),value: attributes.name,onChange: function(newName){props.setAttributes({name: newName});},focus: focusedEditable==='name' ? focus : null,onFocus: function(focus){props.setFocus(_.extend({},focus,{editable: 'name'}));},}),el(RichText,{tagName: 'p',className: 'nelio-testimonial-position',inline: false,placeholder: i18n.__('Position'),value: attributes.position,onChange: function(newPosition){props.setAttributes({position: newPosition});},focus: focusedEditable==='position' ? focus : null,onFocus: function(focus){props.setFocus(_.extend({},focus,{editable: 'position'}));}}),),)];},save: function(props){varattributes=props.attributes;varalignment=props.attributes.alignment;return(el('div',{className: props.className},attributes.mediaURL&&el('div',{className: 'nelio-testimonial-image',style: {backgroundImage: 'url('+attributes.mediaURL+')'}},el('img',{src: attributes.mediaURL}),),el('div',{className: 'nelio-testimonial-content',style: {textAlign: attributes.alignment}},attributes.testimonial&&el('p',{},attributes.testimonial),el('p',{className: 'nelio-testimonial-name'},attributes.name),attributes.position&&el('p',{className: 'nelio-testimonial-position'},attributes.position))));},});})(window.wp.blocks,window.wp.components,window.wp.i18n,window.wp.element,);
The text was updated successfully, but these errors were encountered:
if you are using it in wordpress when you are finding the file you need to add an extra dependency that is called wp_editor, with that it works. For example
Eventually solved it by editing the PHP file, filemtime was commented out and thus the server wasn't serving the latest version.
wp_enqueue_script(
'action_block-cgb-block-js', // Handle.plugin_dir_url( __FILE__ ) . '/dist/blocks.build.js', // Block.build.js: We register the block here. Built with Webpack.array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ), // Dependencies, defined above.filemtime(plugin_dir_path( __FILE__ ) . '/dist/blocks.build.js'), // Version: File modification time (cachebusting)true // Enqueue the script in the footer.
);
I've created a custom block from an example I found online
When I'm adding the block to the page editor, The RichText components are rendered but the MediaUpload component is not.
What am I doing wrong?
This is my code:
The text was updated successfully, but these errors were encountered: