Search code examples
rssexpressionenginepinterestmagpie

Need help extracting image src url from a pinterest rss feed


Basically I am trying to add a Pinterest feed widget to an ExpressionEngine site. I found this tutorial which was very helpful but the issue is that I can't use the Tag Stripping plugin that is referenced because the site I am helping out with uses ExpressionEngine 1x and the plugin is only for 2x.

If you look at the source of a Pinterest rss feed like: http://pinterest.com/amazon/feed.rss you will see that within the <description> tags is the url for the Pinterest image along with the pin's title, tags and a relative link to the pin on pinterest. I'd like to be able to extract only the value between the double quotes after 'img src'.

The tutorial also uses a plugin called Magpie which is already loaded on the site so I am good there.

Ideally, I'd like my code to look something like this:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" parse="inward"
refresh="720" limit="5"} {items}<a href="{link}" target="_blank"><img
src="<?php code-to-grab-the-img-src->{description} ?>"
alt="{title}"></a>{/items} {/exp:magpie}

Obviously "code-to-grab-the-img-src->{description}" doesn't work so I am looking for a function or something that will. How can I tell Magpie to extract only the img src value without using the SuperGeekery Tag Stripper plugin?

Thanks!


Solution

  • I'd suggest building a simple plugin to handle this, you could use the same regex as my other answer (which is failing due to trouble getting the {description} content into PHP).

    Here's what your plugin could look like:

    <?php
    
    $plugin_info = array(
      'pi_name' => 'Get Pinterest Image',
      'pi_version' => '1.0',
      'pi_author' => 'Derek Hogue',
      'pi_author_url' => 'http://amphibian.info',
      'pi_description' => 'Grabs the image URL from a Pinterest RSS feed description element.',
      'pi_usage' => Get_pinterest_image::usage()
      );
    
    class Get_pinterest_image
    {
    
    var $return_data = "";
    
      function Get_pinterest_image()
      {
        global $TMPL;
        $fallback = $TMPL->fetch_param('fallback_image');
        preg_match("/src=\"(.+)(?=\"&gt;&lt;\/a&gt;)/ui", $TMPL->tagdata, $matches);
        $this->return_data = (!empty($matches)) ? $matches[1] : $fallback;
      }
    
      function usage()
      {
      ob_start(); 
      ?>
        {exp:get_pinterest_image fallback="/path/to/fallback_image.png"}{description}{/exp:get_pinterest_image}
    
        Where {description} is the "description" XML node from the Pinterest RSS feed (likely parsed via the Magpie plugin).
      <?php
      $buffer = ob_get_contents();
      ob_end_clean(); 
      return $buffer;
      }  
    
    }
    
    ?>
    

    This would, as per EE conventions, be named get_pinterest_image.php, and go into your /plugins/ folder. Then in your template:

    {exp:magpie url="http://pinterest.com/amazon/feed.rss" refresh="720" limit="5" parse="inward"}
        {items}
            <a href="{link}" target="_blank"><img src="{exp:get_pinterest_image fallback="/path/to/fallback_image.png"}{description}{/exp:get_pinterest_image}" alt="{title}" /></a>
        {/items}
    {/exp:magpie}