My favourite functions - onpage
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Being the generous and kind-hearted person that I am, I thought that I would share some of the little tips, tricks and functions that I use or have found useful whilst developing WordPress plugins.
Most of these functions or code snippets have made it into a number of my sites or plugins - a few of them are even in the core library I include with all my projects now. That doesn’t mean, however, that they are completely error free or of any use to anyone other than me.
onpage - hijacking WordPress permalinks for your own purposes
function onpage($page) {
$path = $_SERVER['REQUEST_URI'];
if((stristr($path,$page.”/”) || stristr($path,$page.”?”)) && (stristr($path,get_option(’upload_path’)) === False)) {
return True;
} else {
return False;
}
}
Admittedly it’s not the most stylish of functions and when I get around to it I will probably change that rather long if statement to something neater, more robust and regular expessiony, but the main reason that it has found it’s way into most of my plugins is the flexibility it provides.
Some examples of using onpage
Most of these examples require the use of fancy permalinks on your blog, in fact the onpage function is usually incorporated in an if statement that also checks for standard query strings in case a blog isn’t using fancy permalinks.
Example 1.
Suppose you wrote a plugin that allowed the user to use their blog to host a podcast and want to output a podcast feed in a specific format when a certain URL is entered.
To perform an operation when the URL entered is: http://www.myblog.com/podcast/itunes/
if(onpage("podcast/itunes")) {
// Add the code to output in itunes format here
...
// Finally we don't want the blog to load after our feed is output so exit
exit();
}
Example 2.
You want, for some reason to output your blog posts in another feed format (say for example iCal) or you want to be able to specify the feed format directly in the URL of the page.
if(onpage("/ical")) {
// Add the code to output in ical format here
...
// Finally we don't want the blog to load after our feed is output so exit
exit();
}
For both of the examples above the pages (podcast/itunes and ical) do not need to exist in WordPress, if the onpage function is incorporated in a plugin that is called using the WordPress init action, then the plugin will be able to check the URL prior to WordPress displaying the 404 error page and perform any actions it needs, hence the importance of the exit() function to stop WordPress from continuing it’s processing after we have handled things.
An example of a non existent page
Have a look at the following web page (warning this is a LIVE site):
http://www.propertyhounds.com/property/PH2
What do you notice about it? Nothing much? Well the page /property/ph2 doesn’t actually exist in the WordPress system that is running the Property Hounds website (infact neither does the page /property).
We simply check if we are on the property page using the onpage function, get the reference for the property we are interested in, display a page with the property details (the function for which I will highlight at a later date) and then stop WordPress from displaying the 404 page with the exit() function.
Hopefully now you can see what can be accomplished and why I love that function so much.