{"id":497,"date":"2008-08-19T12:22:35","date_gmt":"2008-08-19T20:22:35","guid":{"rendered":"http:\/\/www.oeconomist.com\/blogs\/daniel\/?p=497"},"modified":"2009-09-25T21:12:26","modified_gmt":"2009-09-26T05:12:26","slug":"wordpress-relative-specification-of-directory","status":"publish","type":"post","link":"https:\/\/www.oeconomist.com\/blogs\/daniel\/?p=497","title":{"rendered":"WordPress &mdash; Relative Specification of Directory"},"content":{"rendered":"<p>Amongst other things, I've been hacking at the <a href=\"http:\/\/wordpress.org\/extend\/themes\/\">theme<\/a> for this &#39;blog, fixing bugs and simply tweaking things more to my liking.  While I'm at this task, visitors are occasionally going to find the &#39;blog either cosmetically flawed or just plainly dysfunctional. (And, tragically, they may find it thus after I'm finished.)<\/p> <p>In the course of my hacking, I wanted to use <a href=\"http:\/\/www.php.net\/file_exists\">the <abbr title=\"[gibberish abbreviation]\">PHP<\/abbr> function <code>file_exists()<\/code><\/a> to check amongst files <q>within<\/q> the theme directory.  One possibility would be to hard-code a relative specification of the theme directory (the theme slug appended to <q><code>wp-content\/themes\/<\/code><\/q>), but that approach isn't robust; it would increase the ways in which the theme could be broken by external change or by cloning.<\/p> <p>While WordPress has a <a href=\"http:\/\/codex.wordpress.org\/Template_Tags\/get_bloginfo#Template_Directory\">function call to return a specification of the directory<\/a>, it does so in the form of an <abbr title=\"HyperText Transfer Protocol\">HTTP<\/abbr> <abbr title=\"Uniform Resource Locator\">URL<\/abbr>, notwithstanding that <a href=\"http:\/\/codex.wordpress.org\/Template_Tags\/get_bloginfo#Example_output\">there is a call that explicitly requests the theme <abbr title=\"Uniform Resource Locator\">URL<\/abbr><\/a>, to the same effect; meanwhile, <abbr title=\"[gibberish abbreviation]\">PHP<\/abbr> function <code>file_exists()<\/code> will choke on a <abbr title=\"Uniform Resource Locator\">URL<\/abbr>.  I went prowling around <a href=\"http:\/\/codex.wordpress.org\/Main_Page\">the WordPress documentation<\/a>, 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.<\/p> <p>However, there is a function call for the <abbr title=\"Uniform Resource Locator\">URL<\/abbr> of the &#39;blog &mdash; <q><code>get_bloginfo('template_directory')<\/code><\/q>.  My hack to get the relative specification, which could preface file-specs that <em>could<\/em> be usefully handled by <a href=\"http:\/\/www.php.net\/file_exists\"><code>file_exists()<\/code><\/a>, was <blockquote style=\"width: 92%; overflow: auto;\"><code>substr(get_bloginfo('template_url'),strlen(get_bloginfo('url'))&nbsp;+&nbsp;1)<\/code><\/blockquote> The <q><code>+&nbsp;1<\/code><\/q> is to account for the fact that returns to queries to <code>get_bloginfo()<\/code> for directory <abbr title=\"Uniform Resource Locator\">URL<\/abbr>s don't have a terminal slash.<\/p>","protected":false},"excerpt":{"rendered":"Amongst other things, I've been hacking at the theme for this &#39;blog, fixing bugs and simply tweaking things more to my liking. While I'm at this task, visitors are occasionally going to find the &#39;blog either cosmetically flawed or just plainly dysfunctional. (And, tragically, they may find it thus after I'm finished.) In the course [&hellip;]","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[7,69,5,4],"tags":[380,115,13],"class_list":["post-497","post","type-post","status-publish","format-standard","hentry","category-blog-meta","category-information-technology","category-personal","category-public","tag-hacks","tag-php","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=\/wp\/v2\/posts\/497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=497"}],"version-history":[{"count":0,"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=\/wp\/v2\/posts\/497\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oeconomist.com\/blogs\/daniel\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}