Connect with us

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/

7 Comments

7 Comments

  1. Alex Mansfield

    August 5, 2009 at 10:47 pm

    Nice post. I’m glad you found my shortcode tutorial helpful!
    .-= Alex Mansfield´s last blog ..Adding Custom WordPress Shortcodes =-.

  2. Berg

    August 10, 2009 at 2:35 am

    Hey, thanks for the code.

  3. Austin

    August 10, 2009 at 10:50 am

    Check out the plugin!! Eventbrite Attendee Shortcode

  4. jon

    August 25, 2009 at 10:23 pm

    thanks for the code.

  5. Auctions

    September 1, 2009 at 10:05 pm

    Thanks for this code… I will try this….Great informative article…

  6. Arveno

    September 20, 2009 at 8:55 am

    Works great. I appreciate!

  7. John Zitto

    September 22, 2009 at 12:55 pm

    Thanks! This code is useful to me!

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

Top Searches

powered by WassUp

Recent Posts: Fully Net Worth . com

YNW BSlime Net Worth: How Much is the Rapper Actually Worth?

YNW BSlime Net Worth: How Much is the Rapper Actually Worth?

YNW BSlime is an American rapper from Florida. He is known for working alongside his older brother YNW Melly. He has dropped tracks like Slime Dreams, Just Want You, and Gucci Belt. As of 2019, YNW BSlime net worth is estimated to be $150,000. YNW BSlime’s real name is Brandon King and was born on […]

Guapdad 4000 Net Worth: How Rich is the Rapper Actually?

Guapdad 4000 Net Worth: How Rich is the Rapper Actually?

Akeem Hayes, who is better known by his stage name as Guapdad 4000, is an American rapper who is best known for his debut album that was only recently released called Dior Deposits. In the past, he has released a mixtape too called Scamboy Color. As of 2019, Guapdad 4000 net worth is estimated to […]

Michael J Pollard Net Worth: How Rich was the Actor Actually?

Michael J Pollard Net Worth: How Rich was the Actor Actually?

Michael J Pollard was an American actor and comedian best known for his role as C.W. Moss in the movie ‘Bonnie and Clyde’. Michael J Pollard net worth before his death is estimated to be $1.5 million. Pollard was born on May 30, 1939, in Passaic, New Jersey as the son to Sonia V. and […]

%d bloggers like this: