How I can add a custom control to a page?

流过昼夜 提交于 2019-12-11 19:09:17

问题


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;
});


来源:https://stackoverflow.com/questions/56597095/how-i-can-add-a-custom-control-to-a-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!