bioneural.net site preferences

Accessibility

Toggle width/ text size:

style

Default/Alternate

Suits visual impairment, mobile devices

Styling

Change the theme:

layout

Sorry, this option is not enabled

Link behaviour

Links with an icon are off-site:

links

Right-click any link to optionally open in a new window or tab


 

Embedding iStockPhoto feeds revisited

In an earlier post I described a JavaScript-based method to include recently-approved iStockPhoto images in a page on your website. When I migrated from a static blogging system (iBlog) to WordPress I found a useful plugin that worked very well in MAMP. Unfortunately it broke when I moved my developmental site to a live DreamHost server. Here is a bit more background to the problem and an eventual solution that uses SimplePie. Although the focus here is on displaying photographs, it's a solution that should work with practically any feed you might wish to embed.

No go with latestistockimages.php

I was initially using Christine's (of UTW fame) iStockPhoto plug-in for WordPress to display my latest approved images. This worked fine under a localhost development environment, but when I went "live" with DreamHost it stopped working due to the blocking out "outdated" PHP code that posed a security risk. For some reason my e-mails to the author went unanswered, and the download link has been a 404 for ages.

Plugging the gap with Feed2JS

I turned back to Feed2JS, with this in my sidebar:

<?php if (is_page("photos")) { ?>
	<div class="sb-istock"><h2>My iStockPhoto photocast</h2>
	<span class="metalink"><a href="http://www.istockphoto.com/istock_myfiles_rss.php?ID=430123&amp;refnum=bmckenzie" title="RSS feed for iStockPhoto photocast" class="feedlink"><img src="<?php bloginfo('template_directory'); ?>/images/feedicon16px.png" alt="RSS" /></a></span>
	</div>
	<div class="mistock">
		<script type="text/javascript" src="http://jade.mcli.dist.maricopa.edu/feed/feed2js.php?src=http://www.istockphoto.com/istock_myfiles_rss.php?ID=430123&amp;refnum=bmckenzie&amp;num=3&amp;targ=y&amp;html=a"></script>
	<noscript><p class="alert">Please enable JavaScript for full functionality</p></noscript>
	</div>
<?php } ?>

Here's what I got (with appropriate styling):

Picture 1-1

It worked pretty well for the most part, although relied on an external server which I had hoped to avoid. Sometimes is did seem to increase page loading times considerably.

Let's try feedList

I decided to try and replicate this with feedList, a WordPress plugin by Bill Rawlinson with "the ability to display linked lists imported from an RSS or ATOM feed". Here's what I put in my sidebar:

<?php if (is_page("photos")) { ?>
	<div class="sb-istock"><h2>My iStockPhoto photocast</h2>
	<span class="metalink"><a href="http://www.istockphoto.com/istock_myfiles_rss.php?ID=430123&amp;refnum=bmckenzie" title="RSS feed for iStockPhoto photocast" class="feedlink"><img src="<?php bloginfo('template_directory'); ?>/images/feedicon16px.png" alt="RSS" /></a></span>
	</div>
	<div class="mistock">
		<ul>
		<?php
			feedList(array("rss_feed_url"=>"http://www.istockphoto.com/istock_myfiles_rss.php?ID=430123&amp;refnum=bmckenzie",
							"num_items"=>3,
							"show_description"=>true,
							"random"=>false,
							"sort"=>"none",
							"new_window"=>true,
							"show_date"=>false
					)
			);
		?>
		</ul>
	</div>
<?php } ?>

The result was the "same"—except the image was missing altogether!

Picture 3-1

I contacted Bill who felt it was an issue with the built-in XML parser, as called by Magpie (probably not the bird), and resulting from an empty <img> tag embedded within an empty link. Hmmm, I see... Well, we did agree it was "very odd", but no fix was on the horizon.

SimplePie to the rescue

SimplePie is:

... a very fast and easy-to-use class, written in PHP, for reading RSS and Atom syndication feeds.

I have to admit when I first encountered SimplePie (as beta 1) I was totally lost. I couldn't make sense of the documentation and was unable to get it working. But Ryan and team have done much of late to reach out to those of us with rudimentary knowledge of PHP. Beta 2 sees the release of a WordPress plugin, and SimplePie has matured into a true easy-to-use solution for "the rest of us". It's so simple that even the potentially scary bit—modifying CHMOD file permissions on the cache folder—was no big deal using Transmit:

755

With simplepie.inc installed, the cache folder created with appropriate permissions, and the plugin installed and activated, I could then replace the above code blocks with this:

<?php if (is_page("photos")) { ?>
<div class="sb-istock"><h2>My iStockPhoto photocast</h2>
	<span class="metalink"><a href="http://www.istockphoto.com/istock_myfiles_rss.php?ID=430123&amp;refnum=bmckenzie" title="RSS feed for iStockPhoto photocast" class="feedlink"><img src="<?php bloginfo('template_directory'); ?>/images/feedicon16px.png" alt="RSS" /></a></span>
	</div>
	<div class="mistock">
		<?php echo SimplePieWP('http://www.istockphoto.com/istock_myfiles_rss.php?ID=430123&amp;refnum=bmckenzie', 'items:3, showdate:, shortdesc:'); ?>
</div>
<?php } ?>

The output of the SimplePieWP function is sent to the screen with "echo" and is given user-input values for the parameters of feed URL, and for display characteristics (number of items to show e.g. 3, whether and how to show the post date e.g. blank, and how much of the description to show e.g. all). Now to toss in a bit of CSS to the "mistock" class (credit to Dave Kellam at Eightface):

.mistock {
	text-align: center;
	display: inline;
	}

.mistock ul, .mistock li {
	list-style: none;
	}

.mistock h3 {
	display: none;
	}

.mistock a img {
	background: #fff;
	border: 1px #ccc solid !important;
	padding: 3px;
	margin: 5px 3px 5px;
	}

.mistock a:hover img {
	border: 1px #999 solid !important;
	}

The finished product

Your iStockPhoto embedded photocast should now look something like this:

Finished

The only persisting problem is that iStockPhoto offer a poorly-formed feed. I've contacted them twice about encoding ampersands and using "alt" tags to avoid validation errors, but I guess they see this as a low priority.

Update 31.08.06: Although I've not seen any official announcements, iStockPhoto began testing new RSS and Atom feeds in June (I was asked to give feedback). They now offer valid RSS 2.0 and Atom 1.0 feeds, but in the process of testing we discovered some bugs in SimplePie 1.0 beta 2 which, as yet, have not been resolved. The new feeds make it much easier to apply custom CSS when embedding the feed into your site.

Update 05.11.06: SimplePie 1.0 beta 3 has been released and allows you to integrate a valid iStockPhoto RSS feed (entity-encoding issues persist with the Atom feeds)..

6 responses to Embedding iStockPhoto feeds revisited


  1. 1 Ryan Parman

    Bruce, I'd have to say that it was less a matter of me helping you, and more a matter of yoou helping us! :) I spent countless hours pouring over documentation in an effort to make it all as clear as possible, but I never would have been able to get to that point had it not been for your frank and candid feedback. We appreciate your help in this tremendously!

  2. 2 Bruce

    You're welcome Ryan. Your commitment is obvious and it's reassuring to see a developer so willing to take heed of feedback from less technical folk. It's nice to realise that you know where we're coming from, and that you care to spend considerable effort getting things as close to perfection as they can be. I really respect that.

  3. 3 francesco

    Hi, thanks for writing up this easy to understand guide. I wonder if you know or happent o be working on a widget-ized version of this, where a user could simply input a list of photo ids and even style tags (in the same way that the amazon showcase widget works)

    Thanks
    Francesco

  4. 4 Bruce

    @francesco: A quick search turned up RSS Image Widget (requiring SimplePie); reading the description it might do want you are looking for. My code continues do do what I need it too, so I've no plans to look at it again it soon (note the age of this post; there have been SimplePie revisions since and I'm pretty sure that the code I'm currently using differs from the above).

  5. 5 Teng-Yan Loke

    Hi I tried using the simplepie WP plugin and istockphoto to display two items from my feed, but still unable to:
    1. hide the feed title
    2. hide the item title
    3. control the item date format
    4. display the item image

    Here's my function call:

    SimplePieWP('http://www.istockphoto.com/istock_myfiles_rss.php?ID=2279403', array('items'=>2, 'showdate'=>false, 'shortdesc'=>0, 'truncate_feed_title'=>0, 'truncate_item_title'=>0));

    Can you help? Thanks.

  6. 6 Bruce

    Hi Teng-Yan. I've not looked at this in some time, but seem to recall I had some problems with the iStockPhoto feed itself, hence created a FeedBurner feed for it thus:

    SimplePieWP('http://feeds.feedburner.com/istockphoto/uPvf', array('items' => 3, 'template' => mistock));

    As you can see I'm also using the template functionality for more flexibility. I created a file at:

    ../plugins/simplepie-plugin-for-wordpress/templates/mistock.tmpl

    The template contents I use are:

    <div class="simplepie">
    {IF_ERROR_BEGIN}<p class="error">{ERROR_MESSAGE}</p>{IF_ERROR_END}

    {ITEM_LOOP_BEGIN}
    <p style="text-align: center;"><a href="{ITEM_PERMALINK}">{ITEM_TITLE}</a></p>
    <p style="text-align: center;">{ITEM_DESCRIPTION}</p>
    {ITEM_LOOP_END}
    </div>

    Note that you can apply CSS to individual feed items, such as date, and you can control the format of feed items using template tags (see here). What is not in the template won't be shown (or you could apply display: none; in CSS to that item); you could control the date format in the function call using date_format and standard PHP syntax.

    Hope that helps resolve your issue.

Something to say?

Comments may be moderated, are subject to spam filtering, and should be inoffensive and relevant to this post. Please disclose commercial interests.

Usable tags include <a href=""> <blockquote> <em>.