using wordpress custom pages for service templates

I was reading a discussion elsewhere on a rest service plugin for wordpress. Problem seemed that wordpress kept rerouting all requests to index.php. Most wordpress installs use server modules for url rewriting, where .htaccess contains a rule that checks first if a file exists on the server that matches the request. If it doesn’t, wordpress handles it. Unless you write an extra .htaccess rule to direct requests to the service endpoint, you have to have all endpoint files present under the request name. That’s awkward.

using custom pages

You can solve that by masking your service as a page. WordPress tests if the file exists as ‘single’ (page or post), and if a page exists with that permalink, wordpress generates the page.

I can write custom templates for pages. That template can be any file, so it can also be the main request handler of the REST service.

If I place my request handler in the template directory with a remark

/* Template Name: XMLServices

then wordpress adds it to the custom pages list, that I can use on the backend when I add a new page.

custom content-type headers

Services usually reply with json or xml data, and not with wordpress content pages. I need a custom file header.
black magic

No problem, if I leave get_header(); out of the page template, I can specifiy my own header and use an text/xml content type or vcard text/directory content type.

I made a small example by adding my twitter friends timeline as xml output to the blog menu, by placing a file xmlservices.php in the template directory :

/* Template Name: XMLServices

$x = simplexml_load_file('');
if($x) {

//make the XML-header :
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/xml");

//make some xml :
$xml = "< ?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "";
$xml .= "1";
$xml .= "15";
foreach($x->status as $s) {
	$xml .= "";
	$xml .= "< ![CDATA[".$s->source."]]>";		
	$xml .= "< ![CDATA[".$s->text."]]>";
	//$xml .= "< ![CDATA[".$s->user->screen_name."]]>";				
	$xml .= "< ![CDATA[".$s->user->name."]]>";		
	$xml .= "< ![CDATA[".$s->user->screen_name."]]>";		
	$xml .= "< ![CDATA[".$s->user->followers_count."]]>";		
	$xml .= "";		

$xml .= "";
echo $xml;
} else {
//no data, relocate to the blog index or something

Then added a page services with xmlservices as template. I can make a whole tree of xml service pages, as long as I use my XMLServices template when I make the page. You can use both http-get-queries and http-post.

Adding a vCard

wordpress custom page You can also add your  vcard, if you work business to business that comes in very handy.

I picked Troy Wolf’s vcard class of the net, made a file vcard.php with vcard as Template Name and put it in the template directory.

/* Template Name: vcard

//include the class file
//here we go : add my personal data
$vc = new vcard;
$vc->display_name ='Juust';//....

I added a empty new page with ‘about’ as parent and vcard as custom template, published it and now anyone can download my vCard straight from the ‘about’ menu.