Solved

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

Posted on 2011-09-09
6
329 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Did you use a default hosting installation of wordpress with the intention of it being your actual site?  Maybe you’ve installed it as a Blog to your current site and recently converted it to be the main site and do away with the old .htm pages.  An…
Who says nothing in life is free? WordPress.com is a freebie. WordPress.org's downloadable publishing platform is free. Heck, even WordPressMU is free. WordPress is an open source project, which means it can be used on any personal or commerc…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…

751 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