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
  1. */

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
  1. */
  2.  
  3. $x = simplexml_load_file('http://www.juust.org/friends_timeline.xml');
  4. if($x) {
  5.  
  6. //make the XML-header :
  7. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  8. header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
  9. header("Cache-Control: no-cache, must-revalidate" );
  10. header("Pragma: no-cache" );
  11. header("Content-type: text/xml");
  12.  
  13. //make some xml :
  14. $xml = "< ?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  15. $xml .= "<rows>";
  16. $xml .= "<page>1</page>";
  17. $xml .= "<total>15</total>";
  18. foreach($x->status as $s) {
  19.  $xml .= "<row id='".$s->user->id."'>";
  20.  $xml .= "<cell>< ![CDATA[".$s->source."]]></cell>";  
  21.  $xml .= "<cell>< ![CDATA[".$s->text."]]></cell>";
  22.  //$xml .= "<cell>< ![CDATA[".$s->user->screen_name."]]></cell>";    
  23.  $xml .= "<cell>< ![CDATA[".$s->user->name."]]></cell>";  
  24.  $xml .= "<cell>< ![CDATA[".$s->user->screen_name."]]></cell>";  
  25.  $xml .= "<cell>< ![CDATA[".$s->user->followers_count."]]></cell>";  
  26.  $xml .= "</row>";  
  27. }
  28.  
  29. $xml .= "</rows>";
  30. echo $xml;
  31. } else {
  32. //no data, relocate to the blog index or something
  33. }

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
  1. */
  2.  
  3. //include the class file
  4. include('../../../classes/vcard.class.php');
  5. //here we go : add my personal data
  6. $vc = new vcard;
  7. $vc-&gt;display_name ='Juust';//….
  8. $vc-&gt;download();

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.

masked slideshow – jquery & wordpress

I agreed to make a wordpress template for a friends’ site. As extra feature they wanted a slide show in the sidebar with a half transparent mask.

I trolloped around the net a little and this seems the most basic sidebar slideshow :

 
 

I make a new 240×240 image in photoshop and use a radial gradient from transparent to white, making the center of the picture transparent, and export it as ‘mask.png’, 24bit.

In my sidebar_left.php I make three divs, one is the container dia_frame,

the other two are the overlay and the slideshow div.

<div id="dia_frame">
      <div id="dia_show"></div>
       <div id="dia_overlay"></div>
</div>

I give he frame the z-order 90, the mask the z-order 92, the slideshow is z-order 91. That makes sure the overlay is rendered on top of the slideshow.

Both the overlay and show div are relative positioned. 

 

  1. #sidebar_left #dia_frame {
  2.   width: 240px;
  3.   minheight : 240px;
  4.   _height : 240px;
  5.   clear: both;
  6.   zindex: 90;
  7. }
  8.  
  9. #sidebar_left #dia_frame #dia_show {
  10.  width: 240px;
  11.  minheight: 240px;
  12.  _height: 240px;
  13.  position: relative;
  14.  float: right;
  15.         zindex: 91;
  16. }
  17.  
  18. #sidebar_left #dia_frame #dia_overlay {
  19.  width: 240px;
  20.  position : relative;
  21.  background: url(images/mask.png) norepeat;
  22.    minheight : 240px;
  23.   _height : 240px;
  24.  zindex: 92;
  25. }

That sets up the xhtml and css frame in the side bar. Then I go make the slideshow. I grab some graphics of flowers and store them in the themes images folder.

With jQuery in header.php I make an array containing the image urls, and with a timer, cycle the slideshow background graphics :

  1. jQuery(document).ready(function() {
  2. var allImgs = [];
  3.  
  4.  allImgs[0] = "/images/one.png";
  5.  allImgs[1] = "/images/two.png";
  6.  allImgs[2] = "/images/three.png";
  7.  allImgs[3] = "/images/four.png";
  8.  allImgs[4] = "/images/five.png";
  9.  
  10.     var currentSlide = 0;
  11.  
  12.   jQuery.mynamespace = {};
  13.  jQuery.mynamespace.counter = currentSlide;
  14.  jQuery.mynamespace.imgs= allImgs;
  15.  
  16.     function imgRotate() {
  17.  
  18.         jQuery('#dia_show').css('background', 'url(' + jQuery.mynamespace.imgs[jQuery.mynamespace.counter] +  ') no-repeat right');
  19.   jQuery.mynamespace.counter++;
  20.   if(jQuery.mynamespace.counter > 4) {
  21.    jQuery.mynamespace.counter = 0;
  22.   };
  23.     }
  24.  
  25.     setInterval(imgRotate, 2000);
  26.  
  27. });

 

That’s the basic setup

IE5 and IE6 dont support PNG transparency, so for these there are a number of workarounds. supersleight by drew mclellan is reprogrammed to work with jQuery, for wordpress that seems the best option.