WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] widgets.php not included? (5 posts)

  1. richard4339
    Member
    Posted 6 years ago #

    I'm trying to write a plugin to show a sidebar widget, but I'm stumped.

    From what I understand, widgets.php is in wp-includes, meaning the widgets.php functions should be available to me. If I add the function:
    register_sidebar_widget('Servers','widget_server_widget');
    I am told that the function register_sidebar_widget() is not a registered function.
    If I add the typical:

    if ( !function_exists('register_sidebar_widget') ) {
    		include("/home/rage/public_html/wp-includes/widgets.php");
    }
    register_sidebar_widget('Servers','widget_server_widget');

    Then my entire widget configuration in the Presentation tab disappears.

    Are the widgets.php functions not available by default, even in 2.2.3, like the documentation says? Or have I just majorly screwed something up somewhere?

  2. Matt Mullenweg
    Troublemaker
    Posted 6 years ago #

    Widgets are normally included in function.php of a theme, if you're trying to do them in a plugin you need to wrap them in an init function and attach it to the init action.

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

    Your plugins load before widgets do. So you need to defer your plugins operation like matt says.

    But matt is slightly incorrect, you don't want to hook to 'init', but to 'widgets_init' instead, because that hook is called after the normal widgets are initialized.

    So, do it like so:

    function do_my_stuff() {
    //register_sidebar_widget('Servers','widget_server_widget');
    // or whatever
    }
    add_action('widgets_init','do_my_stuff');

    In practice, if you're only registering new widgets, then it doesn't matter if you use 'init'. But if you happen to be replacing existing widgets, then you need to hook to widgets_init to be sure that they init first and then allow you to replace them. So using widgets_init is the recommended way for all cases of creating widgets.

  4. dpga10
    Member
    Posted 6 years ago #

    Does the theme need to have widgets all ready in it? Found a theme that I have started with, yet as with no experience, do not know anything, except that some how I got the page up, But cannot do anything but add new blog. i.e Side columns empty. TIA, DP

  5. richard4339
    Member
    Posted 6 years ago #

    I downloaded the plugin anyway and simply activated the sample plugins (delicious and Google) they provided (which worked), and went from there. Like Otto42 said, the widget_init hook resolved everything.

    The biggest issue for me was that the actual widget plugin readme that automatic has online mentions loading plugins first, but didn't go far enough for me to know about that hook. And the WordPress documentation doesn't have that hook listed either (that I can find), and for that matter, doesn't even have the widget functions from widgets.php listed in the functions reference. Maybe I need to go try to put some documentation in there on some of these, I've been meaning to do it for the mail function anyway.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags