如何使用wp.data.dispatch以编程方式设置Gutenberg首选项?

时间:2021-12-08 作者:jla

古腾堡Options > Preferences 菜单包含许多其他设置,可定制古腾堡体验。默认GUI如下所示。我想以编程方式启用Reduce the interface 选项

enter image description here

当前代码

使用wp.data.select 作用

const reduceInterface = wp.data.select( \'core/edit-post\' )
    .isFeatureActive( \'reduceInterface\' )

console.log( reduceInterface )
这正确地显示true/false 当选项Reduce the interface 通过GUI启用/禁用。

但是,使用以编程方式更改值wp.data.dispatch, 正如通常所做的那样,似乎不起作用。

if ( !reduceInterface ) {
    wp.data.dispatch( \'core/edit-post\' )
        .toggleFeature( \'reduceInterface\' )
}
不会引发错误,但不会更改选项。

问题

古腾堡偏好如何Reduce the interface, 是否以编程方式更新?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

这正确地显示true/false 当选项Reduce the interface 通过GUI启用/禁用。

你确定吗isFeatureActive( \'reduceInterface\' ) 工作了吗?

因为我查看了源代码(参见wp-includes/js/dist/edit-post.js) 然后我发现功能名称实际上是reducedUI 而不是reduceInterface.

因此,请尝试reducedUI 而是:

let reduceInterface = wp.data.select( \'core/edit-post\' ).isFeatureActive( \'reducedUI\' );
console.log( \'before\', reduceInterface );

wp.data.dispatch( \'core/edit-post\' ).toggleFeature( \'reducedUI\' ); // enable/disable it

reduceInterface = wp.data.select( \'core/edit-post\' ).isFeatureActive( \'reducedUI\' );

console.log( \'after\', reduceInterface );
上述内容在WordPress 5.8和5.8.2(写作时的最新版本)上对我有效。

相关推荐

批量操作在单击时重定向到“options.php”页面(WP_LIST_TABLE)

我对WP\\U List\\U表格有问题。我正在学习一个关于互联网的教程(https://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/) 因为我需要在插件页面上显示一个表。问题是批量操作不起作用,我已经尝试使用我刚才提到的链接提供的原始代码,并且我也尝试遵循另一个教程。但是,当我按下批量操作按钮时,wordpress会将我重定向到wp admin/options。php页面,无论我按哪个动作块。我不知道为什么会这样。