Solved

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

Posted on 2011-09-09
6
305 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

If you are looking for plug-ins to add functions to your WordPress small business web site, take some time to read though this comprehensive list.  These are all the plugins I use for my customers WordPress web sites, as well as my own.  Be sure to …
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now