Advertisement
danmandle

Add Shortcode to RSS Excerpt in WordPress

Jun 21st, 2012
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. /*
  2. RE: http://wpthemetutorial.com/2012/02/14/removing-a-specific-shortcode-from-the_content/
  3.  
  4. Thank you for posting this. I have spent most of my afternoon trying to figure out how to efficiently see if a post had a shortcode in it. In my case, I needed to put an image in the RSS Excerpt... with variables from the shortcode. Here's my code for anyone is a simular situation:
  5. */
  6.  
  7. add_filter('the_excerpt_rss', 'rssexcerpt');
  8. function rssexcerpt($rss_content) {
  9.     global $post;
  10.     $regex_pattern = get_shortcode_regex();
  11.     preg_match('/'.$regex_pattern.'/s', $post->post_content, $regex_matches);
  12.     if ($regex_matches[2] == 'YOUR_SHORTCODE') {
  13.         $atts = str_replace(" ", "", $matches[3]); // this is some sort of invisible char that's not a space
  14.         $atts = str_replace(" ", "&", $atts);
  15.         $atts = str_replace('"', '', $atts);
  16.         parse_str($atts, $atts);
  17.         $rss_content = YOUR_SHORTCODE_FUNCTION($atts).$rss_content;
  18.     }  
  19.     return $rss_content;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement