admin管理员组

文章数量:1122832

I'd like to add settings to sidebars defined in my custom theme - sometimes, for example, I'd like to be able to set a widget area to not display. Is this possible, I can't find any references for it?

I'd like to add settings to sidebars defined in my custom theme - sometimes, for example, I'd like to be able to set a widget area to not display. Is this possible, I can't find any references for it?

Share Improve this question asked Mar 28, 2023 at 12:56 toby1kenobitoby1kenobi 2112 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Looks like it is not something that is supported in WordPress. Pretty much the only thing that can be set for the whole Sidebar that is displayed in the admin UI is the title and description. Description allows some basic HTML (like <a>, <b>, <em> etc.) but there is no way to render form controls there and they would not be saved anyway.

WordPress however, has support to add settings for individual Widgets in Sidebars. This may be less intuitive as it is not really a setting for the whole sidebar. You can use the in_widget_form action to render such settings and the widget_update_callback filter to save them for individual Widgets. Than you can simply return false in the widget_display_callback filter to prevent the widget from being shown, based on your setting. Check out this tutorial for more details on adding custom Widget setting to control their display.

Note that this only applies to legacy Widgets. If the new Block (Gutenberg) editor is used it is possible to add blocks to sidebars, and those blocks will not have such settings. Find details about how the Gutenberg sidebar editor is different than the classic one here

本文标签: sidebarAdd custom settings to widget areas