Title: Creating a custom widget
Last modified: August 21, 2016

---

# Creating a custom widget

 *  Resolved [Bloke](https://wordpress.org/support/users/bloke/)
 * (@bloke)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-a-custom-widget/)
 * I am trying to create his widget to display person of the month. I am able to
   save the name in the widget but it doesn’t pass the variable to here: query_posts(‘
   cat=10&name=Bob&posts_per_page=1’);//works
    I want to switch it to say : query_posts(‘
   cat=10&name=$person_name&posts_per_page=1’);//doesn’t work
 *     ```
       class PersonofMonthWidget extends WP_Widget
       {
   
         function PersonofMonthWidget ()
         {
           $widget_ops = array('classname' => 'PersonofMonthWidget', 'description' => 'Displays Person of Month' );
           $this->WP_Widget('PersonofMonthWidget', 'Person Month', $widget_ops);
         }
   
         function form($instance)
         {
           $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
           $title = $instance['title'];
   
       	$person_name = $instance['person_name'];
   
       ?>
         <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
          <p><label for="<?php echo $this->get_field_id('person_name'); ?>">Person Name: <input class="widefat" id="<?php echo $this->get_field_id('person_name'); ?>" name="<?php echo $this->get_field_name('person_name'); ?>" type="text" value="<?php echo attribute_escape($person_name); ?>" /></label></p>
       <?php
         }
   
        function update($new_instance, $old_instance)
         {
           $instance = $old_instance;
           $instance['title'] = $new_instance['title'];
       	 $instance['person_name'] = $new_instance['person_name'];
           return $instance;
         }
   
         function widget($args, $instance)
         {
           extract($args, EXTR_SKIP);
   
           echo $before_widget;
           $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
   
           if (!empty($title))
             echo '<p class="title">' .$before_title . $title . $after_title;
   
           // WIDGET CODE GOES HERE
   
       query_posts('cat=10&name=Bob&posts_per_page=1');	
   
       if (have_posts()) : ?>
       	<?php while (have_posts()) : the_post();echo ':  ';
       		 the_title(); echo '<br/>';?></p>
       <?php the_post_thumbnail(apply_filters('graphene_excerpt_thumbnail_size', 'thumbnail'));
   
       $content = substr(get_the_content(),0,290);
   
       echo '<p span class="here">'; echo preg_replace ('/\[.*\]/', '', $content); echo '</span></p>';
       	endwhile;
       endif; ?>
   
       <?php wp_reset_query();
   
           echo $after_widget;
         }
   
       }
       add_action( 'widgets_init', create_function('', 'return register_widget("PersonofMonthWidget");') );?>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [Bloke](https://wordpress.org/support/users/bloke/)
 * (@bloke)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-a-custom-widget/#post-3790133)
 * I got it working. I had to declare the variable again inside the function.
    $
   person_name = $instance[‘person_name’]; query_posts(‘cat=10&name=’.$person_name.’&
   posts_per_page=1′);//added ‘. on either side of variable.

Viewing 1 replies (of 1 total)

The topic ‘Creating a custom widget’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [Bloke](https://wordpress.org/support/users/bloke/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/creating-a-custom-widget/#post-3790133)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
