Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-09
6
Medium Priority
?
331 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 1500 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 

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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Now that you've installed WordPress 2.9 (http://www.experts-exchange.com/articles/Web_Development/Blogs/WordPress/WordPress-2-9-What-to-Expect-When-Upgrading-to-WordPress-2-9.html?) on your site, you need to install some plugins to get the most out …
WordPress has made it easy to create scalable sites based on their powerful CMS, but as great as WordPress is, there are some SEO issues that haven’t been addressed out of the box.  Fortunately these problems can be solved with a few plugins.  The f…
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…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

715 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