In January 2010 I wrote a post about making a template tag for standard plugins using the built-in WordPress function the_widget(). This post will further expand on the possible uses of the_widget() as the basis of the code you can use to create a shortcode. Shortcodes are well described in the WordPress codex as:

… macro codes for use in post content.
Shortcode API

We will be using the same sample code from the previous post: WPFirstAid Sample Plugin (339)

We have actually done all the heavy lifting to add the appropriate code to the sample widget when we worked out the code to use the_widget() as a template tag.

Let’s jump right in. We start with the basic “template tag” code:

<?php /* Using the_widget() to make a plugin template tag */
the_widget(
  WPFA_Sample_Widget,
  $instance = array(
        'title'                 => __('WPFirstAid Sample'),
        'choices'               => 'The Doctor',
        'show_choices'          => true,
        'optionals'             => 'right'
  ),
  $args = array (
  'before_widget'   => '',
  'before_title'    => '',
  'after_title'     => '',
  'after_widget'    => ''
  )
);
/* NB: This will work, now! */ ?>

Then we will add the necessary code to use it as a function:

<?php
function wpfa_sample_shortcode ($atts) {
  /* Using the_widget() to make a plugin template tag */
  the_widget(
    WPFA_Sample_Widget,
    $instance = shortcode_atts( array(
          'title'                 => __('WPFirstAid Sample'),
          'choices'               => 'The Doctor',
          'show_choices'          => true,
          'optionals'             => 'right'
    ), $atts ),
    $args = array (
    'before_widget'   => '',
    'before_title'    => '',
    'after_title'     => '',
    'after_widget'    => ''
    )
  );
}
?>

Notice lines 6 and 11 above are highlighted. This is the key difference in the code to allow for the shortcode to have working options directly related to the widget.

Now we need to add the necessary code to capture the output and return it. Then we need to add the final line of code, add_shortcode(), to complete this section. Note the highlighted lines below:

<?php
function wpfa_sample_shortcode ($atts) {
  ob_start(); /* Start capture */
  /* Using the_widget() to make a plugin template tag */
  the_widget(
    WPFA_Sample_Widget,
    $instance = shortcode_atts( array(
          'title'                 => __('WPFirstAid Sample'),
          'choices'               => 'The Doctor',
          'show_choices'          => true,
          'optionals'             => 'right'
    ), $atts ),
    $args = array (
    'before_widget'   => '',
    'before_title'    => '',
    'after_title'     => '',
    'after_widget'    => ''
    )
  );
  $wpfa_sample_output = ob_get_contents(); /* Captured output */
  ob_end_clean(); /* Stop capture */
  
  return $wpfa_sample_output;
}
add_shortcode( 'wpfa_sample', 'wpfa_sample_shortcode' );
?>

Now we are ready to add the above code to the end of the sample widget code. In this particular case, the lines 2 through 25 (inclusive) can be copied and pasted at the end of the ‘wpfa-sample-widget.php’ file, just before the close ?> tag. Below the line is the shortcode (slightly customized) in action. Enjoy!


The Doctor is in ... step to your left

NB: The sample widget code download now also contains the shortcode modifications. Here is the link again: WPFirstAid Sample Plugin (339)