WordPress.org

Ready to get started?Download WordPress

Forums

Widgets: Unregistering standard widgets (6 posts)

  1. Jeriko
    Member
    Posted 7 years ago #

    Hi!

    Since two of the standard widgets supplied by the plugin are making trouble with the theme I've developed, I want to unregister them so they don't appear in the panel.

    However a

    unregister_sidebar_widget('Calendar');
    unregister_sidebar_widget('Search');

    , which should be correct according to the plugin API, doesn't seem to work, the widgets still appear in the panel. Am I missing something?

    Cheers, Jeriko

  2. Austin Matzko
    Member
    Posted 7 years ago #

    The trick is unregistering them after the widgets plugin registers them. Since it registers them using the 'init' action hook at priority 5, the following will unregister them:

    function unregister_them() {
    unregister_sidebar_widget('Calendar');
    unregister_sidebar_widget('Search');
    }
    add_action('init','unregister_them',10);

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    While that will indeed work, it's not the best way. Relying on the priority is haphazard at best. Useful for a quick fix and such, but not really something you want to depend on.

    A better way is to notice that the widgets plugin has its own action hook for this sort of thing, called "widgets_init". It happens just after the Widgets plugin registers all its own widgets.

    function unregister_problem_widgets() {
    unregister_sidebar_widget('Calendar');
    unregister_sidebar_widget('Search');
    }
    add_action('widgets_init','unregister_problem_widgets');

  4. Austin Matzko
    Member
    Posted 7 years ago #

    Good call, Otto42.

  5. Jeriko
    Member
    Posted 7 years ago #

    Works like a charm, thank you so much

  6. kretze
    Member
    Posted 7 years ago #

    Thank you very much, same here. Works like charm.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags