Solved

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

Posted on 2011-09-09
6
330 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

623 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