0

I use Wordpress' own rss.php to pull in the last 10 items from an external RSS feed. Wordpress automatically adds campaign info to the link like this:

http://domainname.com/postname?utm_source=rss&utm_medium=rss&utm_campaign=postname

Because of the ampersands, the page doesn't validate. Was wondering if I can add anything to the code below to solve it.


<!-- if Blog Feed exists-->
<?php $blogfeed = get_post_meta($post->ID, 'blog_feed', true);
    if($blogfeed) : ?>

<div id="slideshow">
<ul>                
    <?php
        include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
        $feed = fetch_rss($blogfeed); // specify feed url
        $items = array_slice($feed->items, 0, 10); // specify first and last item
        ?>

        <?php if (!empty($items)) : ?>
            <?php foreach ($items as $item) : ?>
            <li class="feed-item">
                <h4><a href="<?php echo $item['link']; ?>" target="_blank"><?php echo $item['title']; ?></a></h4>
                <p><?php echo $item['atom_content']; ?></p>
            </li>
        <?php endforeach; ?>

        <?php endif; ?>
</ul>
</div><!-- / #slideshow -->

<?php else : ?>

<?php endif; ?>

1 Answer 1

3

You can escape the HTML, so instead of echo $item['link']; you would write:

echo esc_html($item['link']); // This is a WP function
//or 
echo htmlspecialchars($item['link']); //PHP equivalent 

Also important to note your code is depreciated, fetch_rss has been replaced by fetch_feed.

http://codex.wordpress.org/Function_Reference/fetch_feed

ps. You can also use esc_attr which is identical to esc_html in this case.

2
  • Thank you, esc_html worked great! Re: fetch_feed with feed.php give me an error "Warning: array_slice() expects parameter 1 to be array, null given in..."
    – Phantasmix
    Commented Aug 10, 2013 at 7:20
  • Nevermind, got it to work. Thanks for pointing that out.
    – Phantasmix
    Commented Aug 10, 2013 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.