4

Im creating my own theme. When I have a clean wordpress installation, there are some default widgets in the sidebar (search, category, recent posts etc). I know that I can remove them from the sidebar by adding a widget to that sidebar, but I want them removed in the sidebar by default. Is there a way to do that without disabling the widget (unregister_widget())?

4
  • the 'default widgets' are generally no widgets but some hardcoded functionality in the sidebar file of the theme. when you create your own theme, do not add the corresponding code into the sidebar file.
    – Michael
    Feb 11, 2014 at 9:47
  • Just open sidebar.php and delete everything except the call to the specific sidebar Feb 11, 2014 at 10:25
  • 1
    Thanks for the reply. I dont have a hardcoded function, i only use <?php dynamic_sidebar( 'sidebar-main' ); ?>. Could it be that when I activate a clean wordpress installation, and it uses twentyfourteen by default, it copies that theme's default widgets into my theme when I change to my theme under Apparence/Theme?
    – Keat
    Feb 11, 2014 at 10:28
  • @Keat yes I think that's possibly the cause... you can set WP_DEFAULT_THEME in your wp-config.php on brand new installs and make sure your own theme folder exists properly in order to avoid default WP themes being activated. Nov 2, 2022 at 20:38

3 Answers 3

3

This function will disable all widgets:

add_filter( 'sidebars_widgets', 'wpse134172_disable_all_widgets' );

function wpse134172_disable_all_widgets( $sidebars_widgets ) {
   if (true == true) {
     $sidebars_widgets = array( false );
     }
   return $sidebars_widgets;
   }

Now the true=true conditional will disable them all the time, while you only want this to happen with a clean install. So you will have to add a different conditional. Which one depends on your actual purpose.

You could use is_active_widget to test whether only the standard widgets are active.

Another option would be to use the after_switch_theme hook to make the deactivation only happen when your theme is activated.

You could even detect whether the user has visited the widgets page in the backend and decide that after this he apparently is cool with the widgets as they are. This would involve setting an option in the database.

1

It works for me. Deregister all widgets from sidebar. You have to call it once, for example in after_switch_theme action

EDIT:

https://developer.wordpress.org/reference/functions/wp_get_sidebars_widgets/

"This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions"

add_action('after_switch_theme', 'deregister_sidebar_widgets');
function deregister_sidebar_widgets () {   
    $sidebar_widgets = wp_get_sidebars_widgets();

    foreach($sidebar_widgets['sidebar'] as $i => $widget) {
        unset($sidebars_widgets['sidebar'][$i]);
    }
    wp_set_sidebars_widgets($sidebars_widgets);
}
6
  • wp_get_sidebars_widgets is an internal function and you can't use it in your code directly. if you know the widget class there is a better solution: stackoverflow.com/a/9219730/4110112
    – Den Pat
    Jun 29, 2020 at 6:08
  • @DenPat You CAN use it in code directly. And your suggested solution does not work for me
    – G.F.
    Jun 30, 2020 at 15:25
  • Please check the codex: developer.wordpress.org/reference/functions/… You should not use that function when codex says it, we should not do it.
    – Den Pat
    Jul 1, 2020 at 8:29
  • @DenPat But you can use it, and wordpress gives you that ability. In codex not pointed that you shouldn't use it, just being told "is not intended for use by plugin or theme developers". Please, be precise in expressions :)
    – G.F.
    Jul 2, 2020 at 18:28
  • 1
    @DenPat not, it's not directly translated as shouldn't. I'm completely disagree with you. You can interpret words like you want. My interpretation is: not recommended but allowed to use. I will edit the post to aware users about it
    – G.F.
    Jul 3, 2020 at 11:02
0

I have a widget area called "aftercontent" and I wanted to disable the Categories and Annual Archive Widgets when viewing a custom post type called Staff. This is how I did it.

add_filter( 'sidebars_widgets', 'epca_disable_widgets' );
function epca_disable_widgets( $sidebars_widgets ) {
    if ('staff' == get_post_type()) {
        foreach($sidebars_widgets['aftercontent'] as $i => $widget){
            if(substr( $widget, 0, 10 ) === "categories"){
                unset($sidebars_widgets['aftercontent'][$i]);
            }
            if(substr( $widget, 0, 21 ) ===  'annual_archive_widget'){
                unset($sidebars_widgets['aftercontent'][$i]);
            }
        }
    }
    return $sidebars_widgets;
}

I used "substr( $widget, 0, 10 )" because if I want to remove any widget that begins with "categories". Else I could have just said "if($widget == 'categories-3')" or whatever the exact name of the widget is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.