{"id":865,"date":"2008-07-02T00:10:10","date_gmt":"2008-07-01T22:10:10","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=865"},"modified":"2008-07-02T08:32:19","modified_gmt":"2008-07-02T06:32:19","slug":"what-plugin-coders-must-know-about-wordpress-26","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2008\/07\/what-plugin-coders-must-know-about-wordpress-26\/","title":{"rendered":"What Plugin Coders Must Know About WordPress 2.6"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/07\/hacking-wordpress.png\" alt=\"\" title=\"hacking-wordpress\" width=\"200\" height=\"201\" class=\"alignright size-full wp-image-866\" srcset=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/07\/hacking-wordpress.png 200w, https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/07\/hacking-wordpress-150x150.png 150w\" sizes=\"auto, (max-width: 200px) 100vw, 200px\" \/>A WordPress install is a bunch of directories and files, but two of them are particular: the file <tt>wp-config.php<\/tt> and the directory <tt>wp-content\/<\/tt> are personal and don&#39;t get overwritten when you upgrade your blog. In WordPress 2.6, they get <em>so<\/em> personal that you can even move them out of the WordPress root. This must bring a major change to your coding habits.<\/p>\n<p>Plugin coders sometimes need their script to guess the location of their own directory (for example to require() files), or to include <tt>wp-config.php<\/tt> to make a file live alone in a WordPressized environment.<\/p>\n<h2>Guessing the path of <tt>wp-content<\/tt><\/h2>\n<p>WordPress 2.6 allows advanced users to specify the location (physical and URL) of this directory with a constant define, so the directory might not be where it used to be.<\/p>\n<p>What you used to do:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$plugin_path<\/span> <span style=\"color: #339933\">=<\/span> ABSPATH <span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'\/wp-content\/plugins\/'<\/span> <span style=\"color: #339933\">.<\/span> plugin_basename<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900;font-weight: bold\">__FILE__<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$plugin_url<\/span> <span style=\"color: #339933\">=<\/span> get_option<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'siteurl'<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'\/wp-content\/plugins\/'<\/span> <span style=\"color: #339933\">.<\/span> plugin_basename<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900;font-weight: bold\">__FILE__<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>What you will have to do, now that this directory can hide anywhere:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Pre-2.6 compatibility<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #990000\">defined<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'WP_CONTENT_URL'<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'WP_CONTENT_URL'<\/span><span style=\"color: #339933\">,<\/span> get_option<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'siteurl'<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'\/wp-content'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #990000\">defined<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'WP_CONTENT_DIR'<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #990000\">define<\/span><span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'WP_CONTENT_DIR'<\/span><span style=\"color: #339933\">,<\/span> ABSPATH <span style=\"color: #339933\">.<\/span> <span style=\"color: #0000ff\">'wp-content'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Guess the location<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$plugin_path<\/span> <span style=\"color: #339933\">=<\/span> WP_CONTENT_DIR<span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'\/plugins\/'<\/span><span style=\"color: #339933\">.<\/span>plugin_basename<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900;font-weight: bold\">__FILE__<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$plugin_url<\/span> <span style=\"color: #339933\">=<\/span> WP_CONTENT_URL<span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'\/plugins\/'<\/span><span style=\"color: #339933\">.<\/span>plugin_basename<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900;font-weight: bold\">__FILE__<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>In WordPress 2.6, constants WP_CONTENT_DIR and WP_CONTENT_URL are either user defined or set in <tt>wp-settings.php<\/tt><\/p>\n<h2>Including wp-config.php<\/h2>\n<p>In WordPress 2.6 you can either leave <tt>wp-config.php<\/tt> in the blog root directory, or move it to the parent folder (which makes sense if this takes this critical file off of the webserver&#39;s document root)<\/p>\n<p>What you used to do:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">require_once<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'..\/..\/..\/wp-config.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>What you must do now:<\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000088\">$root<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">dirname<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900;font-weight: bold\">__FILE__<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">if<\/span> <span style=\"color: #009900\">&#040;<\/span><span style=\"color: #990000\">file_exists<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$root<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'\/wp-load.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ WP 2.6<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">require_once<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$root<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'\/wp-load.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span> <span style=\"color: #b1b100\">else<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Before 2.6<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">require_once<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$root<\/span><span style=\"color: #339933\">.<\/span><span style=\"color: #0000ff\">'\/wp-config.php'<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Basically in WordPress 2.6 the file responsible for loading the environment is, in first instance, wp-load.php in place of the good old wp-config.php.<\/p>\n<p>However, as pointed out by GamerZ in the comments, this still may not work. Why? Because not only the config file may not be there anymore, but the relative path to it may have changed since <tt>wp-content<\/tt> might have been moved too.<\/p>\n<p>At this point though, there is no way I can think of to guess the locations of both wp-config and wp-content. To be 100% foolproof, such a &quot;standalone&quot; file needing to include wp-config should be editable so that advanced users moving their wp-content directory could manually edit a location path (the <tt>$root<\/tt> variable in the previous example)<\/p>\n<h2>Summary<\/h2>\n<p>Coders, revisit your old plugins to make sure they won&#39;t eventually break on WordPress 2.6 :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A WordPress install is a bunch of directories and files, but two of them are particular: the file wp-config.php and the directory wp-content\/ are personal and don&#39;t get overwritten when you upgrade your blog. In WordPress 2.6, they get so personal that you can even move them out of the WordPress root. This must bring [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,85,76,245],"class_list":["post-865","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-plugins","tag-tips","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=865"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/865\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}