Categories: Tips & Tricks

Turn your RSS feed into a shortcode

Last week I wrote how to “Use WordPress to print a RSS feed for Eventbrite attendees“. It was pretty popular, but then I found myself in a place that was more annoying. Trying to incorporate that into a blog post or page.

Without having to download a plugin that will allow PHP to be executed inside a post, I would have to create a template file and use that. Which is what I did, and it works just fine. But for some reason I forgot all about shortcodes! With a shortcode, I could generate all the PHP in the functions file and then just call the shortcode when/where I want.

Okay, so lets show the completed PHP code:

function attendee_feed_print_2009() {
 global $wpdb;
 include_once( ABSPATH . WPINC . '/rss.php' );
 $rss = fetch_rss( 'http://www.eventbrite.com/rss/event_list_attendees/384870157' );
 $items = array_slice( $rss->items, 0 );
 if ( empty( $items ) ) echo '<ul style="list-style-type: none; list-style-image: none; list-style-position: outside;"><li>No items</li></ul>';

 else

 foreach ( $items as $item ) : ?>
 <ul style="list-style-type:none; list-style-image:none; list-style-position:outside; margin-bottom: 0px">
 <li><!--<strong><?php echo $item[ 'title' ]; //User name ?></strong><br />-->

 <?php echo $item[ 'content' ][ 'encoded' ]; ?>

 <hr style="border: 1px solid #ddd; margin-bottom: 8px" />
 </li>
 </ul>
 <?php endforeach;
}

Now, this code has to be changed for it to work as a shortcode. We’ll have to return the function and not print/echo it.

I also wanted to be able to use multiple instances of the code with different feeds. To do so I had to create a argument to extract from the completed short code. I found a demo at: Alex Mansfield’s post.

Remember this:

function attendee_feed_print_2009() {
 global $wpdb;
 include_once( ABSPATH . WPINC . '/rss.php' );
 $rss = fetch_rss( 'http://www.eventbrite.com/rss/event_list_attendees/384870157' );

We are going to update it to read as follows ( changes in bold ) :

function attendee_feed_print_2009( $rss_nbr ) {
 global $wpdb;
 extract( shortcode_atts( array( 'rss' => ''), $rss_nbr ) );
 include_once( ABSPATH . WPINC . '/rss.php' );
 $rss = fetch_rss( $rss );

And the final code with the fields updated to return the arguments ( put into your functions.php file ( in between <?php ?> ) ) :

function attendee_feed_print_2009( $rss_nbr ) {
  global $wpdb;
  extract( shortcode_atts( array( 'rss' => ''), $rss_nbr ) );
  include_once( ABSPATH . WPINC . '/rss.php' );
  $rss = fetch_rss( $rss );
  $items = array_slice( $rss->items, 0 );
  
  $rss_html = '<div id="eventbrite-attendee-list" style="clear:both;">';
  
  if ( empty( $items ) ) $rss_html .= '<ul style="list-style:none;"><li>No attendees, yet.</li></ul>';

  else

  foreach ( $items as $item ) :

  $rss_html .= '<ul style="background:none; list-style:none; margin:0px">';
  $rss_html .= '<li style="background:none; list-style:none;">';
  $rss_html .= $item[ 'content' ][ 'encoded' ];
    
  $rss_html .= '<hr style="border: 1px solid #ddd; margin-bottom: 10px" />';
  $rss_html .= '</li>';
  $rss_html .= '</ul>';
  
  endforeach;

  $rss_html .= '</div>';

  return $rss_html;
  }

And lets not forget to add the shortcode function!

add_shortcode( 'eventbrite-attendees', 'attendee_feed_print_2009' );

Final outcome with look like this `[eventbrite-attendees rss="http://your-rss-feed.com/"]`

I’ve created this into a plugin!

Download the plugin: http://wordpress.org/extend/plugins/eventbrite-attendees-shortcode/

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

Recent Posts

Swekey: A safe web enabler usb?

I received a Swekey in the mail this week. What's a Swekey you ask? Like…

2 hours ago

Add additional meta boxes to Hybrid Theme

At the time of writing this post I have Theme Hybrid as my parent theme,…

22 hours ago

What do Software as a Service (SaaS) Companies Do?

What do Software as a Service (SaaS) Companies Do?

1 day ago

Sick of images being to large for the content area?

Have you ever uploaded an image that might have been a tad bigger than the…

2 days ago

And the winners who receive a free book are.

See, I really did pull the winner out of a hat, lol. Well In my…

2 days ago