Theme widgets

Hey folks,
I need a little bit of coding help. I am trying to create several different widgets that will be packaged along with a series of themes I am developing. Seems simple enough right? I have found examples of other peoples widgets and have been able to activate them but they seem to override each other. Any help or input will be greatly appreciated!

  • czydrm

    Hey Dr. Mike,
    Exactly... I am putting the widget code into the theme's function file. The issue I am running into is that the code example I got from WP's codex (http://codex.wordpress.org/Widgets_API) over-writes any other widget code I place in there... so for example if I create one widget to display a slideshow and then use the same code, changing all the stuff that needs to be changed, to display a newsletter signup form, the newsletter widget over-writes the slideshow widget. I am probably overlooking something very simple. Here is the code I am working with....
    /**
    * FooWidget Class
    */
    class FooWidget extends WP_Widget {
    /** constructor */
    function FooWidget() {
    parent::WP_Widget(false, $name = 'FooWidget');
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {
    extract( $args );
    $title = apply_filters('widget_title', $instance['title']);
    ?>
    <?php echo $before_widget; ?>
    <?php if ( $title )
    echo $before_title . $title . $after_title; ?>
    Hello, World!
    <?php echo $after_widget; ?>
    <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {
    $title = esc_attr($instance['title']);
    ?>
    <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
    <?php
    }

    } // class FooWidget
    // register FooWidget widget
    add_action('widgets_init', create_function('', 'return register_widget("FooWidget");'));

  • Barry

    have found examples of other peoples widgets and have been able to activate them but they seem to override each other.

    Not sure what you mean, unless you are repeatedly re-initialising the plugins with the same names.

    Easiest plugin to look at from here is the ad widget - https://premium.wpmudev.org/project/ad-widget

    Grab that and take a look at the following lines:
    33, 35 and 44,45,46 and 258

    line 33 gives the widget a name, in this case "adlitewidget", line 35 is a function with the same name as the class "adlitewidget" and is the initialisation function.

    You'll notice the same name "adlitewidget" multiple times in lines 44,45,46 this is to tell the widget internally what it is called - i like to keep all these things the same so it's easy to keep track. Each different widget should have a different name (line 33) and thus different values in 35, 44, 45 and 46.

    Finally line 258 registers the widget, same text again in that function call.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.