Title: Using few the same widgets &#8211; wrong ID
Last modified: February 7, 2018

---

# Using few the same widgets – wrong ID

 *  Resolved [mlyczko](https://wordpress.org/support/users/mlyczko/)
 * (@mlyczko)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/)
 * Hi
 * My problem is that i’m using
    `$this->id` in my widget’s code to show widget’s
   ID. It’s working correctly if i display few widgets in some sidebar but there
   is a problem with Widget Shortcode – every widget displays ID: “-1” instead of
   for example “my_widget-5”. Is there maybe a workaround?
 * Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [shazdeh](https://wordpress.org/support/users/shazdeh/)
 * (@shazdeh)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9948969)
 * Hi,
 * I’m missing the context for this issue 🙂 Where are you adding the `$this->id`
   code? Does `$this->id` fail if you have the Widget Shortcode active (and it works
   otherwise)? Any guide on how I could replicate this issue?
 *  Thread Starter [mlyczko](https://wordpress.org/support/users/mlyczko/)
 * (@mlyczko)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9949342)
 * Hi
 * Please take a look here: [http://ml.dev.pax1.eu/testy/wordpress/](http://ml.dev.pax1.eu/testy/wordpress/)
   
   There are 3 widgets in content and 3 widgets in sidebar. There are wrong IDs 
   in content: [https://www.screencast.com/t/FBN5zGQNt8f](https://www.screencast.com/t/FBN5zGQNt8f)
   OK in sidebar: [https://www.screencast.com/t/ofPp9Tnk](https://www.screencast.com/t/ofPp9Tnk)
 * Thanks
 *  Plugin Author [shazdeh](https://wordpress.org/support/users/shazdeh/)
 * (@shazdeh)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9949709)
 * I see. I checked and the `-1` ID is set specifically by the `the_widget` function
   which is what the plugin uses in the plugin to display the widget outside a widget
   area.
    Might I ask why you want this? Note that the widget wrapper has the proper
   ID, and if you wish to add the widget ID as a classname you could do:
 *     ```
       <?php
   
       function custom_widget_shortcode_classes( $atts ) {
       	$atts['container_class'] .= ' widget-%1$s';
       	return $atts;
       }
       add_filter( 'shortcode_atts_widget', 'custom_widget_shortcode_classes' );
       ```
   
 * The `widget-{widgetID}` class is now added to all widgets displayed by the plugin.
 *  Thread Starter [mlyczko](https://wordpress.org/support/users/mlyczko/)
 * (@mlyczko)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9951071)
 * Hi
 * Thank you.
    I have this code inside plugin:
 *     ```
       		<script>
       			jQuery(document).ready(
       				function($)
       				{
       				    $('#<?php echo $this->id; ?> .pe-recent-posts-outer').carousel({
       				    	interval: <?php echo $interval; ?>,
       						pause: "<?php echo $slider_pause; ?>"
       				    })
       				}
       			);
       		</script>
       ```
   
 * I need a some way to get widget ID inside the plugin’s code.
 * Thanks.
 *  Plugin Author [shazdeh](https://wordpress.org/support/users/shazdeh/)
 * (@shazdeh)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9952636)
 * Hi!
 * Inside the `widget( $args, $instance )` method, you can access `$args['widget_id']`
   and that properly outputs the widget ID. This is not overrided with a fake ID
   and so it should work as you expect.
 * Another alternative (which I personally prefer) would be to ditch the inline `
   <script>` tag and instead do it like so:
 *     ```
       <script>
       jQuery( function($) {
       	$( '.pe-recent-posts-outer' ).each( function(){
       		var $this = $( this );
       		$this.carousel({
       			interval: $this.data( 'interval' ),
       			pause: $this.data( 'pause' )
       		});
       	} );
       } );
       </script>
       ```
   
 * And in your widget output, you’d use `data-interval=""` HTML attribute; this 
   way the same JavaScript code is used for all the widgets and their parameters
   are dynamic as well.
 * Hope this helps!
 *  Thread Starter [mlyczko](https://wordpress.org/support/users/mlyczko/)
 * (@mlyczko)
 * [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9952769)
 * Great support.
 * Thank you very much 🙂

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Using few the same widgets – wrong ID’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/widget-shortcode.svg)
 * [Widget Shortcode](https://wordpress.org/plugins/widget-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/widget-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/widget-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/widget-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/widget-shortcode/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [mlyczko](https://wordpress.org/support/users/mlyczko/)
 * Last activity: [8 years, 4 months ago](https://wordpress.org/support/topic/using-few-the-same-widgets-wrong-id/#post-9952769)
 * Status: resolved