WordPress — Relative Specification of Directory

19 August 2008

Amongst other things, I've been hacking at the theme for this 'blog, fixing bugs and simply tweaking things more to my liking. While I'm at this task, visitors are occasionally going to find the 'blog either cosmetically flawed or just plainly dysfunctional. (And, tragically, they may find it thus after I'm finished.)

In the course of my hacking, I wanted to use the PHP function file_exists() to check amongst files within the theme directory. One possibility would be to hard-code a relative specification of the theme directory (the theme slug appended to wp-content/themes/), but that approach isn't robust; it would increase the ways in which the theme could be broken by external change or by cloning.

While WordPress has a function call to return a specification of the directory, it does so in the form of an HTTP URL, notwithstanding that there is a call that explicitly requests the theme URL, to the same effect; meanwhile, PHP function file_exists() will choke on a URL. I went prowling around the WordPress documentation, and did some code-diving, but didn't find a function call or global variable for anything more like a relative specification of the theme directory.

However, there is a function call for the URL of the 'blog — get_bloginfo('template_directory'). My hack to get the relative specification, which could preface file-specs that could be usefully handled by file_exists(), was

substr(get_bloginfo('template_url'),strlen(get_bloginfo('url')) + 1)
The + 1 is to account for the fact that returns to queries to get_bloginfo() for directory URLs don't have a terminal slash.

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.