获取或设置以发布元为单位的值

时间:2020-03-04 作者:danihazler

我根据以下教程构建了这个主题选项https://rudrastyh.com/gutenberg/plugin-sidebars.html#registerPlugin

但现在我很难弄清楚如何发送帖子/页面数据(包括选择的颜色)。

谁能帮我一下吗?

干杯

const PrimaryThemeColor = withState( {
    color: \'#fff\',
} )( ( { color, setState } ) => {
    console.log(\'PrimaryThemeColor\', color)

    return (
        <ColorPicker
            color={ color }
            onChangeComplete={ ( value ) => setState({color: value.hex})}
            disableAlpha
        />
    )
} );

const SecondaryThemeColor = withState( {
    color: \'#fff\',
} )( ( { color, setState } ) => {
    console.log(\'SecondaryThemeColor\', color)

    return (
        <ColorPicker
            color={ color }
            onChangeComplete={ ( value ) => setState({color: value.hex})}
            disableAlpha
        />
    )
} );



export const ThemeColorPalette = () => {
    return (
        <PluginSidebar
            name="theme-plugin-sidebar"
            title="Theme Plugin"
            icon=\'admin-appearance\'
        >
            <div>
                <h3>Primary Colour</h3>
                <PrimaryThemeColor />
            </div>

            <div style={{ marginTop: \'50px\'}}>
                <h3>Secondary Colour</h3>
                <SecondaryThemeColor />
            </div>
        </PluginSidebar>
    );
};
registerPlugin( \'brave-theme-plugin-sidebar\', { render: ThemeColorPalette } );

1 个回复
SO网友:danihazler

最后,我成功地做到了这一点

(function( plugins, editPost, element, components, data, compose ) {

    const el = element.createElement;

    const { Fragment } = element;
    const { registerPlugin } = plugins;
    const { PluginSidebar, PluginSidebarMoreMenuItem } = editPost;
    const { PanelBody, ColorPicker } = components;
    const { withSelect, withDispatch } = data;

    const MetaThemeControl = compose.compose(
        withDispatch( function( dispatch, props ) {
            return {
                setMetaValue: function( metaValue ) {
                    dispatch( \'core/editor\' ).editPost(
                        { meta: { [ props.metaKey ]: metaValue } }
                    );
                }
            }
        } ),
        withSelect( function( select, props ) {
            return {
                metaValue: select( \'core/editor\' ).getEditedPostAttribute( \'meta\' )[ props.metaKey ],
            }
        } ) )( function( props ) {
            return el( ColorPicker, {
                label: props.title,
                value: props.metaValue,
                onChangeComplete: function( content ) {
                    props.setMetaValue( content.hex );
                },
            });
        }
    );

    registerPlugin( \'b-theme-sidebar\', {
        render: function() {
            return el( Fragment, {},
                el( PluginSidebarMoreMenuItem,
                    {
                        target: \'b-theme-sidebar\',
                        icon: \'admin-appearance\',
                    },
                    \'B Theme\'
                ),
                el( PluginSidebar,
                    {
                        name: "b-theme-sidebar",
                        title: "B Theme",
                        icon: \'admin-appearance\',
                    },
                    el( PanelBody, {},
                        // primary color
                        el( MetaThemeControl,
                            {
                                metaKey: \'primary_theme\',
                                title : \'Primary Theme\',
                            }
                        ),
                        // secondary color
                        el( MetaThemeControl,
                            {
                                metaKey: \'secondary_theme\',
                                title : \'Secondary Theme\',
                            }
                        ),
                    )
                )
            );
        }
    } );
} )(
    window.wp.plugins,
    window.wp.editPost,
    window.wp.element,
    window.wp.components,
    window.wp.data,
    window.wp.compose
);
但我想为我的第一次尝试找到一个解决方案,因为它更具可读性。

编辑:解决第一种情况的教程:

https://css-tricks.com/managing-wordpress-metadata-in-gutenberg-using-a-sidebar-plugin/

相关推荐

修改CPT上的发布Metabox位置

我一直在努力想如何正确地做到这一点,但还没有找到解决办法。我有一个带ACF字段的CPT。我不能使用前端表单,因为我正在跨多个子网站同步CPT,所以需要使用后端表单。我已经设置好了,发布表单默认为一列,但我无法将发布元框移动到表单底部。。。它位于帖子标题字段下方。这似乎很愚蠢。S你能提供的任何帮助都是巨大的!谢谢