Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

WordPress plugin to handle XML remote feed.

Hello there,

I have an URL link to a sport ranking feed that I want to embed in a table inside my WordPress site. However, the feed link contains an auto-generated XML, and I was looking quite a lot for plugin that could let me handle this type of format, because It is generated from a third party and I can not modify the XML, so I need to fit my site to it. There are a few that do something similar (i.e. TablePress), but they don't work with XML files.

It seems that I need to code my own plugin for that. Could anyone please, suggest me how to start doing it and what tools and knowledge do I need?

I started to watch the videos about writing a plugin in wordpress. But I think I will need something else in addition.

Thanks in advance....

The XML looks like:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <item>
            <title>PlayerName01</title>
            <R2_UID>49373</R2_UID>
            <city_state_country>Cd. Juarez, CH MEX</city_state_country>
            <gender>Male</gender>
            <player_image></player_image>
            <rank>1</rank>
            <ranking_points>13220</ranking_points>
            <advantage>3495</advantage>
            <events_played>11</events_played>
        </item>
        <item>
            <title>PlayerName02</title>
            <R2_UID>32507</R2_UID>
            <city_state_country>Marine, MN USA</city_state_country>
            <gender>Male</gender>
            <player_image></player_image>
            <rank>2</rank>
            <ranking_points>9725</ranking_points>
            <advantage>2145</advantage>
            <events_played>11</events_played>
        </item>
        ...
        ...
        ...
        ...
    </channel>
</rss>

1 Answer

Shane Oliver
Shane Oliver
19,977 Points

PHP has a parser for XML inbuilt so you could do something like

<?php
    $xml = simplexml_load_file( 'URL of the awesome API that returns XML instead of JSON' ) or die ( 'Error: Your object was not created' );
    // add your code to extract that data from your xml object
?>

You're right! I think using JSON would be a much better way to feed this kind of information to external websites, specially to embed it in a table. However, as I said, this feed is being generated from other source that I can't access or change.

How about if I threat this as a RSS feed? Do you think it will be easier that way? Although I wanted to show it inside a table, I think if I achieve to show it as a chronological feed will be fine, too.

In that case, when I use a RSS reader, how can I do to get the information inside those custom tags like: <city_state_country>, <gender>, <rank>, etc...? The regular readers are just recognising the <title> tag.

Thanks...