ThemeForest

Wordpress Widgets

242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says

OK, I know in the past, Wordpress has allowed custom widgets to only be used once. Does anyone know if this has changed? Or does anyone know how to make a widget that can be used multiple times like the official Wordpress widgets?

-Chris

2 years ago
1266 posts Chris Robinson
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Author had a File in an Envato Bundle
  • Interviewed on the Envato Notes blog
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • United States
  • Referred between 200 and 499 users
contempoinc says

Definitely can, all you have to do is register multiple instances:

register_sidebar_widget('Twitter', 'twitterWidget');
register_sidebar_widget('Twitter 2', 'twitterWidget');
register_sidebar_widget('Twitter 3', 'twitterWidget');
register_widget_control('Twitter', 'twitterWidgetAdmin', 400, 200);
2 years ago
242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says
Definitely can, all you have to do is register multiple instances:
register_sidebar_widget('Twitter', 'twitterWidget');
register_sidebar_widget('Twitter 2', 'twitterWidget');
register_sidebar_widget('Twitter 3', 'twitterWidget');
register_widget_control('Twitter', 'twitterWidgetAdmin', 400, 200);

What about controls. Will all the settings be set the same for each widget?

2 years ago
27 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Europe
Pranick says
Or does anyone know how to make a widget that can be used multiple times like the official Wordpress widgets?
You have to write a specific class. A little example:
class Follow_Link extends WP_Widget
{
    function Follow_Link()
    {
        $widget_ops = array(
            'description' => __('"Follow me on Twitter" link.')
        );
        $control_ops = array(
            // 'width' => 300
        );
        $this->WP_Widget('follow_link', __('Follow Link'), $widget_ops, $control_ops);
    }

    /**
     * Function 'widget' is required.
     * It is actual widget code.
     */
    function widget($args, $instance)
    {
        extract($args);

        if (empty($instance['username']))
        {
            return;
        }

        $url = 'http://twitter.com/'.$instance['username'];

        $anchor = $instance['anchor'];

        echo $before_widget;

        echo '<a href="'.$url.'">'.$anchor.'</a>';

        echo $after_widget;
    }

    /**
     * Function form is optional.
     * It creates widget controls in admin panel.
     */
    function form($instance)
    {
        $username = $instance['username'];
        $anchor = isset($instance['anchor']) ? $instance['anchor'] : 'Follow me on Twitter';

        if ($instance['error'])
        {
            echo '<p style="color:#f00;font-weight:900">'.$instance['error'].'</p>';
        }

        ?>
        <p><label for="<?php echo $this->get_field_id('username'); ?>"><?php _e('Username'); ?>:</label>
        <input name="<?php echo $this->get_field_name('username'); ?>" class="widefat" id="<?php echo $this->get_field_id('username'); ?>" value="<?php echo esc_attr($username); ?>" /></p>
        <p><label for="<?php echo $this->get_field_id('anchor'); ?>"><?php _e('Anchor text'); ?>:</label>
        <input name="<?php echo $this->get_field_name('anchor'); ?>" class="widefat" id="<?php echo $this->get_field_id('anchor'); ?>" value="<?php echo esc_attr($anchor); ?>" /></p>
        <?php }

    /**
     * Function update is optional.
     * It updates widget settings.
     * If you won't provide your own update function, then the default update function will be used.
     * And the default one simply returns $new_instance.
     */
    function update($new_instance, $old_instance)
    {
        if (strlen($new_instance['username'])?> 15 || preg_match('/[^a-zA-Z0-9_]/', $new_instance['username']))
        {
            $instance['error'] = __('Wrong username');
        }
        else
        {
            $instance['username'] = $new_instance['username'];
        }

        $instance['anchor'] = $new_instance['anchor'];

        return $instance;
    }
}

/**
 * Register the widget
 */
add_action('widgets_init', create_function('', 'return register_widget("Follow_Link");'));
If you need more examples, default widgets are defined in wp-includes/default-widgets.php.
2 years ago
1266 posts Chris Robinson
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Author had a File in an Envato Bundle
  • Interviewed on the Envato Notes blog
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • United States
  • Referred between 200 and 499 users
contempoinc says
Definitely can, all you have to do is register multiple instances:
register_sidebar_widget('Twitter', 'twitterWidget');
register_sidebar_widget('Twitter 2', 'twitterWidget');
register_sidebar_widget('Twitter 3', 'twitterWidget');
register_widget_control('Twitter', 'twitterWidgetAdmin', 400, 200);
What about controls. Will all the settings be set the same for each widget?

You’ll need to make a new control for each one I believe

2 years ago
242 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
ATLChris says

Yeah I already new you could do it that way. I wanted to know if there is any way to do it, with out having to have multiple of the same code. I guess not!

The way you suggested is the way I already do it. Thanks for the info though!

2 years ago
374 posts
  • Has been a member for 2-3 years
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
_rohan says
OK, I know in the past, Wordpress has allowed custom widgets to only be used once.

Not completely true – see here http://net.tutsplus.com/tutorials/wordpress/dissecting-the-wordpress-text-widget/

-Rohan.

2 years ago
by
by
by
by
by