最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Gutenberg Block - Post Featured Image Filter Hook

programmeradmin0浏览0评论

I'm wanting to add some functionality to the featured image block in the Gutenberg editor. I've got pretty close:

The problem is, I'm unable to select an image, and posts that already had a featured image assigned are not loading the image in the block.

I think this must be due to the original block object's key not being set. I've tried a random value, the post id, the media post id, 'render', 'thumbnail', and 'post-thumbnail', but no dice. What is 'my-key' suppose to be set to? Why is the featured image not loading, and not able to be set and saved?

window.wp.hooks.addFilter( 
    'editor.PostFeaturedImage', 
    'myplugin/myhook', 
    function( original ) { 
        console.log (original);
        return function() { 
            return (
                window.wp.element.createElement( 
                    'div', 
                    { key: 'outer' + Math.random() }, 
                    [
                        'Prepend above',
                        _.extend( original( {} ), { key: 'my-key' } ),
                        'Append below' 
                    ]
                )
            );
        } 
    } 
);

I'm wanting to add some functionality to the featured image block in the Gutenberg editor. I've got pretty close:

The problem is, I'm unable to select an image, and posts that already had a featured image assigned are not loading the image in the block.

I think this must be due to the original block object's key not being set. I've tried a random value, the post id, the media post id, 'render', 'thumbnail', and 'post-thumbnail', but no dice. What is 'my-key' suppose to be set to? Why is the featured image not loading, and not able to be set and saved?

window.wp.hooks.addFilter( 
    'editor.PostFeaturedImage', 
    'myplugin/myhook', 
    function( original ) { 
        console.log (original);
        return function() { 
            return (
                window.wp.element.createElement( 
                    'div', 
                    { key: 'outer' + Math.random() }, 
                    [
                        'Prepend above',
                        _.extend( original( {} ), { key: 'my-key' } ),
                        'Append below' 
                    ]
                )
            );
        } 
    } 
);
Share Improve this question edited Jul 23, 2019 at 3:16 iRector asked Jul 22, 2019 at 23:29 iRectoriRector 1293 silver badges8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

From the documentation:

Prepend and append to the panel contents:

var el = wp.element.createElement;

function wrapPostFeaturedImage( OriginalComponent ) { 
  return function( props ) {
      return (
          el(
              wp.element.Fragment,
              {}, 
              'Prepend above',
              el(
                  OriginalComponent,
                  props
              ),
              'Append below'
          )
      );
  } 
} 

wp.hooks.addFilter( 
  'editor.PostFeaturedImage', 
  'my-plugin/wrap-post-featured-image', 
  wrapPostFeaturedImage
);

So did you intentionally not using wp.element.Fragment?

You may also want to check the example here:

  • Gutenberg: Extending Featured Image Component
发布评论

评论列表(0)

  1. 暂无评论