Solved

cannot add more than one instance of custom widget I have created in wordpress, help!?

Posted on 2011-09-09
6
318 Views
Last Modified: 2012-05-12
Hi Experts,

I have created a custom widget, called 'Recent Blog Posts', it works fine, but when I create another side bar, i cannot add another instance of the custom widget to my new sidebar.  It is not appearing in my widget list, but appears in my first sidebar.  What could be the problem?
0
Comment
Question by:kasondeneddy
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:c_a_n_o_n
ID: 36510410
From what I can tell, there is no native support for Duplicate widgets, however, a plugin should do it for you:
http://wordpress.org/extend/plugins/mycustomwidget/
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 36515166
It is possible, or there wouldn't be a plugin. If you provide your code, I might be able to help you out.
0
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 500 total points
ID: 36517099
I've hacked up a few different widgets to show you how to add a widget to your theme that can be used more than once. For the example it's a recent posts widget:

<?php
class EE_Recent_Posts extends WP_Widget {
	function EE_Recent_Posts() {
		global  $theme_name; //This will use the current themes name for the widget title
		// define widget title and description
		$widget_ops = array('classname' => 'EE_recent_posts',
							'description' => __( 'The most recent posts with thumbnails','YOURthemes') );
		// register the widget
		$this->WP_Widget('EE_Recent_Posts',$theme_name . " " . __('Recent Posts','YOURthemes'), $widget_ops);
	}
	// display the widget in the theme
	function widget( $args, $instance ) {
		global $wpdb;
		extract($args);
		$exclude_blog_cats = preg_replace("!(\d)+!","-${0}$0", get_option('EE_excluded_cats'));
		$posts_number  = (int) $instance['posts_number'];
		$posts = get_posts("cat=$exclude_blog_cats&numberposts=$posts_number&offset=0");
		$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts','YOURthemes') : $instance['title'], $instance, $this->id_base);
		echo $before_widget;
		if ( $title ) echo $before_title . $title . $after_title;
		if($posts){ ?>
		<ul class="thumb-list">
		<?php foreach($posts as $post){
		$post_title = stripslashes($post->post_title);
		$post_date = $post->post_date;
		$post_date = mysql2date('F j, Y', $post_date, false);
		$permalink = get_permalink($post->ID);
		$post_thumbnail = get_the_post_thumbnail($post->ID, array(60, 60), array("class" => "post_thumbnail"));
		if(!$post_thumbnail){
			$post_thumbnail = '<img alt="image" src="'.get_template_directory_uri() .'/images/empty_thumb.gif" />';
		}
		?>
		<li><a class="fl" href="<?php echo $permalink; ?>" title="<?php echo $post_title; ?>">
        <span class="frame"><?php echo $post_thumbnail;?></span></a>
		<a class="thumb-title" href="<?php echo $permalink; ?>" rel="bookmark"><?php echo $post_title; ?></a><br />
		<span class="date"><?php echo $post_date; ?></span></li>
		<?php } ?>
		</ul>
		<?php }
		echo $after_widget;
		//end
	}
	// update the widget when new options have been entered
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		// replace old with new
		$instance['posts_number'] = (int) strip_tags($new_instance['posts_number']);
		$instance['title'] =  strip_tags($new_instance['title']);
		return $instance;
	}
	// print the widget option form on the widget management screen
	function form( $instance ) {
	// combine provided fields with defaults
	$instance = wp_parse_args( (array) $instance, array( 'posts_number' => 3, 'title'=>__('Recent Posts','YOURthemes') ) );
	$posts_number = (int) strip_tags($instance['posts_number']);
	$title =  strip_tags($instance['title']);
	// print the form fields
?>
	<p><label for="<?php echo $this->get_field_id('title'); ?>">
	<?php _e('Title:','YOURthemes'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo
		esc_attr($title); ?>" /></p>    
    <p><label for="<?php echo $this->get_field_id('posts_number'); ?>">
	<?php _e('Number of Posts:','YOURthemes'); ?></label>
	<input class="widefat" id="<?php echo $this->get_field_id('posts_number'); ?>" name="<?php echo $this->get_field_name('posts_number'); ?>" type="text" value="<?php echo
		esc_attr($posts_number); ?>" /></p>
<?php
	}
}
function EE_widgets_init() {
	register_widget('EE_Recent_Posts');
	do_action('widgets_init');
}
add_action('init', 'EE_widgets_init', 1);	
?>

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:kasondeneddy
ID: 36518679
Hi,

Can you please advise where I should place the above code, install it as plugin code, or edit exisitng widget code to include the above?  I have been tryign to attach my code but it is 19mb n taking forever to upload.  Thanks for the help and quick feedback.
0
 
LVL 23

Assisted Solution

by:jeremyjared74
jeremyjared74 earned 500 total points
ID: 36519246
There are a few ways to handle the code above. I show you two ways to add it to your theme.

1. You can copy the code above and add it to the top of your themes functions.php file.

2. You can copy the code above and paste it into a blank text document and save it as widgets.php, then you would place this in your functions.php code:
<?php
if(is_admin()) {	
	require_once('widgets.php');
}
?>

Open in new window


To use the second method you would need to upload the widgets.php file to your themes root directory.
0
 

Author Closing Comment

by:kasondeneddy
ID: 36529616
Thanks, worked well!!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Like there are many different versions of Wordpress, there are also a handful of related side projects WordPress users should be familiar with.   All of these projects are open source and GPL licensed, which means there are few restrictions to us…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question