0

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!

Erik Berger
  • 599
  • 1
  • 10
  • 24

3 Answers3

1

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}
Community
  • 1
  • 1
Derek Hogue
  • 4,589
  • 1
  • 15
  • 27
  • I'm sorry but as you can probably tell I am just learning a lot of this. Where exactly in the plugin file do I paste in the regex? I tried a few different spots but am having trouble getting the plugin manager in EE to not blow up when I refresh to see if I can see the my newly created plugin. Can you let me know what area exactly this should go in? You have been so helpful, thanks. – Erik Berger Aug 21 '12 at 14:05
  • to be more clear when I refresh the plugin manager after I ftp my plugin into the /sys/plugins/ directory I get "No direct script access allowed" – Erik Berger Aug 21 '12 at 14:34
  • Eureka!!! With a couple modifications. I had to ad "pi." to the plugin file for it to be recognized by my plugin manager so pi.get_pinterest_image.php. I also edited your regex to '/src="([^"]+)"/' which I stole from this Wordpress Pinterest widget:http://plugins.svn.wordpress.org/mg-pinterest-strips-widget/trunk/mg-pinterest-strips.php. Thank you soo much!! – Erik Berger Aug 21 '12 at 15:11
0

Sure - enable PHP on output, then use this regex:

{exp:magpie url="http://pinterest.com/amazon/feed.rss" refresh="720" limit="5"}
{items}
<?php
    preg_match("/src=\"(.+)(?=\"&gt;&lt;\/a&gt;)/ui", '{description}', $matches);
    $src = (!empty($matches)) ? $matches[1] : '/path/to/default-image.png';
?>
    <a href="{link}" target="_blank"><img src="<?php echo $src; ?>" alt="{title}"></a>
{/items}
{/exp:magpie}

This code includes a fallback for another image to use in case the image src can't be found for some reason.

Not tested, but it should work.

Derek Hogue
  • 4,589
  • 1
  • 15
  • 27
  • Hmm, I get 5 broken image boxes because the img src is your fallback of /path/to/default-image.png which doesn't exist (yet). The link and alt tag work correctly. Any guesses as to why? Thanks so much! I need to learn about rexeg. – Erik Berger Aug 20 '12 at 20:10
  • Sorry, the pattern wasn't specific enough. Try the updated code? – Derek Hogue Aug 20 '12 at 20:31
  • Nope, still giving me the default image path. I ran your regular expression and the value of one tag on this tester: http://www.spaweditor.com/scripts/regex/index.php and the result was an empty array. Thanks so much for helping me with this. – Erik Berger Aug 20 '12 at 20:46
  • Ack, sorry, forgot to escape a forward slash. Try one more time! – Derek Hogue Aug 20 '12 at 22:06
  • Still no luck. I think it is having trouble actually pulling the input string out of {description}. I've tried using very simple regular expressions to populate src="" with anything to no avail. Could it be a limitation of Magpie that you can't use regex within it to drill down to a specific part of an XML object? – Erik Berger Aug 20 '12 at 22:34
  • Do you have PHP enabled on output? – Derek Hogue Aug 20 '12 at 22:35
  • I have 'Yes' ticked for 'Allow PHP'. There is other PHP used within this template that I am trying your code on. 'PHP Parsing Stage' is set for 'Input' not 'Output' but I checked all other templates on the site and say input there. – Erik Berger Aug 20 '12 at 22:44
  • Yeah, getting the description into that PHP expression is the issue, and it has to do with quotes. – Derek Hogue Aug 20 '12 at 23:00
0

RSS parsing is hard! If you're willing to go a little more complicated and bring the feed content into EE, I've had good luck with the EE2-only DataGrab, so you might find some happiness with the free EE1 predecessor: FeedGrab.

Scott Hepler
  • 422
  • 3
  • 12