Ever find yourself wanting to create a custom feed of your WordPress managed website? Lucky for you they have the handy add_feed hook that allows you to define as many feeds as you want, Just be careful not to duplicate any existing feeds. I personally find myself using this feature to output xml of data that I then utilize in Flash presentations. The add_feed function works like all other WordPress hooks in that it calls the user defined function when the named feed is to be rendered.

For illustration purposes lets walk through the basic steps for creating a Custom Feed.

Define a function to output the xml

Before adding the feed you first need to define the function that will render the xml to be returned when the feed is called. Depending on your needs you can define this in a custom plugin or in the functions file of your theme. The below snippet outputs your posts in a basic xml structure.

function outputXMLFeed()
{
    $posts = query_posts('showposts=5');
 
    echo '<?xml version="1.0"?>';
    echo '<items>';
    foreach ($posts as $post) {
        echo '<item>';
        echo '<title>' . get_the_title($post->ID) . '</title>';
        echo '<link>' . permalink($post->ID) . '</link>';
        echo '<guid>' . get_permalink($post->ID) . '</guid>';
        echo '</item>';
    }
    echo '</items>';
}

Add the feed to WordPress

Now to add the feed we simply need to call add_feed where we pass in the name of the feed and the function that will output the feed contents (xml). One caveat exists however in that WordPress needs to be fully initialized before the add_feed function can be called. This forces us to wrap up the add_feed call behind another function that gets called on the WordPress init hook.

add_action('init', 'add_my_feed');
 
function add_my_feed(  ) {
  add_feed("myFeed", "outputXMLFeed");
}

Calling your Custom Feed

To access your feed you simply need to append ?feed=Name of your Feed to your site’s url. In this case the final url would be http://www.example.com/?feed=myFeed

That is all there is to creating your custom feeds. Depending on your needs you can create them in your theme functions or in a standalone plugin. If you are using additional plugins like Pods CMS then feeds can be extremely useful in rendering pods contents into xml for use by Flash, JavaScript or other applications.

, , , ,

XSLT  (EXtensible Sytlesheet Language Transformations) is a tool that can be used to transform XML documents into other formats most commonly of which is XHTML. XSLT is a stylesheet that defines how to display or output the data from the XML. Using XSLT you can sort, filter, and manipulate the data as needed iterating of data elements and their child elements. Below is a short example of how to sort an XML data file based on 2 elements.

To start things off we need an xml data file. In our example we are going to use books. You will notice that in addition to the xml data we specify the xml-stylesheet to utilize which we will create next.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sorted_books.xsl"?>
<books>
   <book>  
	<author>Patrick Rothfuss</author>
	<title>The Name of the Wind</title>		
	<publisher>DAW</publisher>
	<year>2007</year>
   </book>
   <book>  
	<author>Trudi Canavan</author>
	<title>The Magicain's Guild</title>		
	<publisher>EOS</publisher>
	<year>2004</year>
   </book>
   <book>  
	<author>Trudi Canavan</author>
	<title>The Magicain's Apprentice</title>		
	<publisher>Orbit</publisher>
	<year>2009</year>
   </book>
   <book>  
	<author>Jim Butcher</author>
	<title>First Lord's Fury</title>		
	<publisher>ACE</publisher>
	<year>2090</year>
   </book>
   <book>  
	<author>Jim Butcher</author>
	<title>Changes</title>		
	<publisher>ROC</publisher>
	<year>2010</year>
   </book>
   <book>  
	<author>Brandon Sanderson</author>
	<title>Warbreaker</title>		
	<publisher>TOR</publisher>
	<year>2009</year>
   </book>
</books>

Next we will create an XSL Stylesheet that will be used to transform the XML Data into a XHTML page that displays the information in a table sorted by author followed by the title.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head><title>My Books</title>
</head>
<body>
<table width="100%" border="1">
  <THEAD>
   <TR>
       <TD width="50%"><B>Title</B></TD>
       <TD width="50%"><B>Author</B></TD>
       <TD width="50%"><B>Publisher</B></TD>
       <TD width="50%"><B>Year</B></TD>
   </TR>
  </THEAD> 
  <TBODY>
   <xsl:for-each select="books/book">
    <xsl:sort select="author" />
    <xsl:sort select="title" />
    <TR>	  
       <TD width="25%"><xsl:value-of select="title" /></TD>
       <TD width="25%"><xsl:value-of select="author" /></TD>
       <TD width="25%"><xsl:value-of select="publisher" /></TD>
       <TD width="25%"><xsl:value-of select="year" /></TD>
    </TR>
   </xsl:for-each>
  </TBODY>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

In the above example we define the style sheet to iterate over each book element in the xml using the xsl:for-each tag. Then we immediately tell it to sort on the author field and secondly by the title field by use of the xsl:sort tag.

<xsl:sort select="author" />
<xsl:sort select="title" />

The end result is an html table as shown below:

Title Author Publisher Year
Warbreaker Brandon Sanderson TOR 2009
Changes Jim Butcher ROC 2010
First Lord’s Fury Jim Butcher ACE 2090
The Name of the Wind Patrick Rothfuss DAW 2007
The Magicain’s Apprentice Trudi Canavan Orbit 2009
The Magicain’s Guild Trudi Canavan EOS 2004

You can sort on as many elements as you want simply adding addtional xsl:sort tags. Full information on the available properties like order can be found at w3schools

, , , , ,

YahooFinanceAPI is an open source php library for interacting with the Yahoo Finance Stock API. For those of you unfamiliar with Yahoo’s Stock API please review my previous post Yahoo Finance Stock API. This library contains an easy interface for obtaining information on one or more stocks. For a quick demo of the library feel free to go to http://mdbitz.com/testing/PHPYahooFinance/finance.php?symbol=DELL which will return JSON coded response of some of the basic information that can be obtained via the library. Also feel free to swap out the symbol in the url with any of your choice, if it is an invalid symbol you will simply not get any information.

Updates in Release 1.0

  • isSuccess method added to YahooFinance_Result object to check if request was successful.
  • Bug fixes for typo errors in YahooFinance_Options
  • toXML method added to YahooFinance_Result and YahooFinance_Quote
  • STRICT and LOOSE mode added to YahooFinanceAPI to throw or supress exceptions

Online Documentation

The YahooFinanceAPI PHP Library is fully documented using phpDocumentor. The online documentation contains a full code example as well as class and method descriiptions and parameters.

How do I use the YahooFinanceAPI library?

The YahooFinanceAPI was constructed so that users need to simply follow a basic 6 set process to get any stock information they are looking for.

Step 1: Obtain the main YahooFinanceAPI class

require_once(dirname(__FILE__) . '/YahooFinanceAPI.php');

Step 2: Register the autoloader

spl_autoload_register(array('YahooFinanceAPI', 'autoload'));

Step 3: Identify the information to be returned

Now that the library is properly configured in your script lets define the information that is to be returned for each of the symbols or stocks being looked up. This is done by using the addOption function of the YahooFinanceAPI instance. For a complete list of options please view the YahooFinance_Options class.

$api = new YahooFinanceAPI();
 
// set options
$api->addOption("symbol");
$api->addOption("previousClose");
$api->addOption("open");
$api->addOption("lastTrade");
$api->addOption("lastTradeTime");
$api->addOption("change" );
$api->addOption("daysLow" );
$api->addOption("daysHigh" );
$api->addOption("volume" );

Step 4: Identity the symbols (stocks) to obtain the information for

After defining the information to be returned we define they symbols (stocks) to be returned. This is done by the addSymbol function. To find symbols you can search Yahoo Finance for the company you are interested in.

$api->addSymbol("DELL" );

Step 5: Query for the Information

$result = $api->getQuotes();

Step 6: Use the Stock Information

if( $result->isSuccess() ) {
    $quotes = $result->data;
    foreach( $quotes as $quote ) {
        $symbol = $quote->symbol;
        $lastTrade = $quote->lastTrade;
    }
}

Download the YahooFinanceAPI Library

The YahooFinanceAPI is copyright MDBitz – Matthew John Denton under the GNU Public License. The current release is available for download at http://mdbitz.com/downloads/YahooFinanceAPI-1.0.zip. The archive contains the php classes as well as a test.php file that demonstrates how to use the library.

Feedback Appreciated

Please leave your comments, suggestions, and/or questions below. And if you need any additional support please feel free to send me an email at info@mdbitz.com.

, , , , ,