Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Let's assume I have a widget that displays only its name:

    <p>
         <?php echo $args['widget_id'] ?>
    </p>

So when I drag & drop my widget to any sidebar it shows:

<p>
     myWidget-number
</p>

The problem is I want to call this widget with a shortcode:

    (...)
            ob_start();    
            the_widget(MyWidget);
            return ob_get_clean();
   }

    add_shortcode('myWidget_short', 'myWidget_shortcode');

And when i do [myWidget_short] it shows only

<p>
</p>

Any ideas how to call widget's ID with a shortcode?

share|improve this question
2  
IDs are not assigned to arbitrary widgets, only the ones registered inside sidebars have them –  onetrickpony Mar 2 '11 at 16:27
add comment

2 Answers

up vote 0 down vote accepted

I believe @One Trick Pony was right.

Shortcode widgets have no ID, so I've found a way around.

Firstly I used PHP rand function:

$var = rand();

And then added the "var" to the ID, so it doesn't collide with other shortcodes calling the same widget (each one has different random number at the end of the ID):

<div id="myWidget-<?php echo $var?>;"></div>
share|improve this answer
add comment

I built a shortcode for calling sidebars and a shortcode for calling widgets into Total Widget Control. The sidebars where pretty easy to create, just calling dynamic_sidebar inside of ob_start();

However, the calling of individual widgets was quite complicated. global $wp_registered_widgets I think is the global variable that contains the complete widget instance. You'll need to first create your own function that allows you to grab the widget instance and then you need to call the widgets callback function in order to display the widget.

The code that you're trying to use the_widget actually required that you pass the widget instance as a parameter. It's not capable of hunting down the widget itself.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.