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
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
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);
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?
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.Definitely can, all you have to do is register multiple instances:What about controls. Will all the settings be set the same for each widget?register_sidebar_widget('Twitter', 'twitterWidget'); register_sidebar_widget('Twitter 2', 'twitterWidget'); register_sidebar_widget('Twitter 3', 'twitterWidget'); register_widget_control('Twitter', 'twitterWidgetAdmin', 400, 200);
You’ll need to make a new control for each one I believe
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!
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.
COPYRIGHT © 2012 ENVATO| TERMS OF USAGE| SUPPORT/HELP| ICONS BY TANGO + WEFUNCTION + FAMFAMFAM
Adobe®, Flash®, Flex®, Fireworks®, Photoshop®, Illustrator®, InDesign® and After Effects® are registered trademarks of Adobe Systems Incorporated.