How I can add a custom control to a page?

2019-08-21 13:02发布

问题:

I need to add a checkbox to the page editing form in Gutenberg without third-party plugins like ACF. I did some tutorials, including the one on the official Wordpress page, but it does not behave as I need to.

I already have the addition to the sidebar (I replaced the checkbox with a toogle but it would be the same), the element does not work itself, if I click does not change its status, nor can I store the value when saving the page.

Formerly I would have solved it with metabox, but it is no longer compatible with this version of Wordpress.

What should I modify in the code for the component to change its status and then store it in database when saving a page?

I tried with this and works, but isn't what I need: https://developer.wordpress.org/block-editor/tutorials/plugin-sidebar-0/plugin-sidebar-1-up-and-running/

I tried whit this: https://www.codeinwp.com/blog/make-plugin-compatible-with-gutenberg-sidebar-api/

export class MyPluginSidebar{
  constructor(wp){
    const { __ } = wp.i18n;

    const {
      PluginSidebar,
      PluginSidebarMoreMenuItem
    } = wp.editPost;

    const {
      PanelBody,
      TextControl,
      ToggleControl
    } = wp.components;

    const {
      Component,
      Fragment
    } = wp.element;

    const { withSelect } = wp.data;

    const { registerPlugin } = wp.plugins;

    const { withState } = wp.compose;
    class Hello_Gutenberg extends Component {
      constructor() {
        super( ...arguments );

        this.state = {
          key: '_hello_gutenberg_field',
          value: '',
        }

        wp.apiFetch( { path: `/wp/v2/posts/${this.props.postId}`, method: 'GET' } ).then(
          ( data ) => {
            console.log('apiFetch data', data.meta);
            this.setState( { 
              value: data.meta._hello_gutenberg_field
            } );
            return data;
          },
          ( err ) => {
            console.log('wp api fetch error', err);
            return err;
          }
        );
      }

      static getDerivedStateFromProps( nextProps, state ) {
        if ( ( nextProps.isPublishing || nextProps.isSaving ) && !nextProps.isAutoSaving ) {
          wp.apiRequest( { path: `/hello-gutenberg/v1/update-meta?id=${nextProps.postId}`, method: 'POST', data: state } ).then(
            ( data ) => {
              return data;
            },
            ( err ) => {
              return err;
            }
          );
        }
      }

      render() {
        var hasFixedBackground = true;
        return (
          <Fragment>
          <PluginSidebarMoreMenuItem
            target="hello-gutenberg-sidebar"
          >
            { __( 'Sidebar title' ) }
          </PluginSidebarMoreMenuItem>
          <PluginSidebar
            name="hello-gutenberg-sidebar"
            title={ __( 'Sidebar title' ) }
          >
            <PanelBody>
              <ToggleControl
                label="Control label"
                //help={ hasFixedBackground ? 'Has fixed background.' : 'No fixed background.' }
                checked={ hasFixedBackground }
                //onChange={ () => this.setState( ( state ) => ( { hasFixedBackground: ! state.hasFixedBackground } ) ) }
              />

            </PanelBody>
          </PluginSidebar>
        </Fragment>
        )
      }
    }

    // Pass post ID to plugin class
    // Prevent to save on each state change, just save on post save
    const HOC = withSelect( ( select, { forceIsSaving } ) => {
      const {
        getCurrentPostId,
        isSavingPost,
        isPublishingPost,
        isAutosavingPost,
      } = select( 'core/editor' );
      return {
        postId: getCurrentPostId(),
        isSaving: forceIsSaving || isSavingPost(),
        isAutoSaving: isAutosavingPost(),
        isPublishing: isPublishingPost(),
      };
    } )( Hello_Gutenberg );

    registerPlugin( 'hello-gutenberg', {
      icon: 'admin-site',
      render: HOC,
    } );
  }
}

This code register the sidebar, add the control but didn't change his state neither save in database.

Any help are welcome.

Regards!

回答1:

If you want to save data that you have added later on you gutenberg block you need to use addFilter. I can show you an example i am using:

wp.hooks.addFilter('blocks.registerBlockType', 'custom/filter', function(x,y) {
    if(x.hasOwnProperty('attributes')){
        x.attributes.background_image = {
            type: 'string',
            default: ''
        };
    }
    return x;
});