【问题标题】:How I can add a custom control to a page?如何将自定义控件添加到页面?
【发布时间】:2019-10-29 00:39:52
【问题描述】:

我需要在没有第三方插件(如 ACF)的情况下在 Gutenberg 的页面编辑表单中添加一个复选框。我做了一些教程,包括官方 Wordpress 页面上的教程,但它的行为不像我需要的那样。

我已经在侧边栏添加了(我用一个工具替换了复选框,但它会是一样的),元素本身不起作用,如果我点击不会改变它的状态,我也不能存储值保存页面。

以前我会用 metabox 解决它,但它不再与这个版本的 Wordpress 兼容。

我应该在代码中修改组件以更改其状态,然后在保存页面时将其存储到数据库中?

我试过这个并且工作正常,但不是我需要的:https://developer.wordpress.org/block-editor/tutorials/plugin-sidebar-0/plugin-sidebar-1-up-and-running/

我试过这个: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,
    } );
  }
}

此代码注册侧边栏,添加控件但未更改其状态,也未保存在数据库中。

欢迎任何帮助。

问候!

【问题讨论】:

标签: wordpress wordpress-gutenberg


【解决方案1】:

如果您想保存稍后添加到古腾堡块上的数据,您需要使用 addFilter。我可以向您展示我正在使用的示例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    相关资源
    最近更新 更多