In: , , ,
On: 2008 / 07 / 02 Viewed: 27494 times

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'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 a major change to your coding habits.

Plugin coders sometimes need their script to guess the location of their own directory (for example to require() files), or to include wp-config.php to make a file live alone in a WordPressized environment.

Guessing the path of wp-content

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.

What you used to do:

PHP:
  1. $plugin_path = ABSPATH . '/wp-content/plugins/' . plugin_basename(dirname(__FILE__));
  2. $plugin_url = get_option('siteurl') . '/wp-content/plugins/' . plugin_basename(dirname(__FILE__));

What you will have to do, now that this directory can hide anywhere:

PHP:
  1. // Pre-2.6 compatibility
  2. if ( !defined('WP_CONTENT_URL') )
  3.     define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
  4. if ( !defined('WP_CONTENT_DIR') )
  5.     define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  6.  
  7. // Guess the location
  8. $plugin_path = WP_CONTENT_DIR.'/plugins/'.plugin_basename(dirname(__FILE__));
  9. $plugin_url = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__));

In WordPress 2.6, constants WP_CONTENT_DIR and WP_CONTENT_URL are either user defined or set in wp-settings.php

Including wp-config.php

In WordPress 2.6 you can either leave wp-config.php 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's document root)

What you used to do:

PHP:
  1. require_once('../../../wp-config.php');

What you must do now:

PHP:
  1. $root = dirname(dirname(dirname(dirname(__FILE__))));
  2. if (file_exists($root.'/wp-load.php')) {
  3.     // WP 2.6
  4.     require_once($root.'/wp-load.php');
  5. } else {
  6.     // Before 2.6
  7.     require_once($root.'/wp-config.php');
  8. }

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.

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 wp-content might have been moved too.

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 "standalone" 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 $root variable in the previous example)

Summary

Coders, revisit your old plugins to make sure they won't eventually break on WordPress 2.6 :)

Related posts

Metastuff

This entry "What Plugin Coders Must Know About WordPress 2.6" was posted on 02/07/2008 at 12:10 am and is tagged with , , ,
Watch this discussion : Comments RSS 2.0. You can trackback this post from your own site

96 Blablas

    Pages: « 1 2 3 4 [5] Show All

  1. 81
      WordPress 2.6 rockt by ... Germany »
    pingback on 19/Jul/08 at 12:03 am # :

    [...] mit WordPress Plugins zu tun hat, sollte sich den Artikel von planetozh dazu anschauen. Außerdem bin ich gespannt auf das iPhone App mit dem man wohl WordPress [...]

  2. 82
    links for 2008-07-18 | hansi.unblogged Germany »
    pingback on 19/Jul/08 at 1:35 am # :

    [...] What Plugin Coders Must Know About WordPress 2.6 « planetOzh 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’t get overwritten when you upgrade your blog. In WordPress 2.6, they get so personal that yo (tags: wordpress plugins plugin php 2.6 coding programming howto code) [...]

  3. 83
    Welcome to Paradise Australia »
    wrote, on 19/Jul/08 at 8:50 am # :

    I also recently updated my wordpress to 2.6, so this article has also helped me to better understand this newer version of wordpress.

  4. 84
    Rahul Bansal India »
    commented, on 23/Jul/08 at 4:32 pm # :

    Hi Ozh,
    I will just say your code come to my rescue as god.
    I was looking for a way out from few days.

    Thank you very much... :-)

  5. 85
    Roundup - Work, Plugin News, Theme Updat... United States »
    pingback on 26/Jul/08 at 3:18 pm # :

    [...] There is one very small issue with most of my plugins. WordPress 2.6 may break the Check Version function in the Options page. This does not affect the core functionality of the plugin and is only likely to occur if you have moved the wp-content folder from its default location. [...]

  6. 86
    Schweizer WordPress Magazin | Plugins fÃ... Switzerland »
    pingback on 31/Jul/08 at 7:33 pm # :

    [...] bei planetOzh Tags: Pfad, Plugin, WordPress, wp-config, [...]

  7. 87
    Updated Blogroll to Google CSE Plugin &l... United States »
    pingback on 01/Aug/08 at 7:52 am # :

    [...] detects the appropriate location of core WordPress files and URLs using the technique outlined in this post by [...]

  8. 88
    The hassle with WP_CONTENT_URL and WP_PL... Europe »
    pingback on 06/Aug/08 at 1:53 pm # :

    [...] this has changed and Ozh show up the new way in his article. In his discussion with GamerZ he added the note to the article : However, as pointed out by GamerZ [...]

  9. 89
    2008 Plugin Competition Review, Part One... United States »
    pingback on 07/Aug/08 at 11:25 am # :

    [...] the internals of TinyMCE, but beware of the wp-config.php include that is deprecated on WP 2.6 (wp-config.php can be moved) So, basically: very cool idea, I hope the plugin improves in the [...]

  10. 90
    Coses que han de saber els desenvolupado... United States »
    pingback on 10/Aug/08 at 12:25 pm # :

    [...] ha escrit una entrada en la que explica les coses que els desenvolupadors de plugins han de saber sobre WordPress 2.6; en aquesta entrada mostra el codi que s’hauria de introduïr-se en els [...]

  11. 91
    Brimosoft » Blog Archive » L... United States »
    pingback on 15/Aug/08 at 11:40 am # :

    [...] WordPress 2.6 allows advanced users to specify the location (physical and URL) of the wp-content directory with a constant define, so the directory might not be where it used to be. I have changed the code of Lazyest Gallery in the way Ozh suggested in his post What Plugin Coders Must Know About WordPress 2.6 [...]

  12. 92
    wesley Poland »
    said, on 18/Aug/08 at 10:19 pm # :

    Shouldn't you also check if WP_PLUGIN_DIR & WP_PLUGIN_URL are already defined? Since they too could be at a different location? Here you assume it's "plugins"?

  13. 93
    Ozh France »
    commented, on 19/Aug/08 at 8:59 am # :

    wesley » actually yes, you're right. I just didn't want to look like I encourage doing this :P

  14. 94
    wesley Poland »
    commented, on 19/Aug/08 at 4:19 pm # :

    Also, doesn't __FILE__ include all you need to know? If you remove the .php file at the end

  15. 95
    Ozh France »
    thought, on 19/Aug/08 at 4:41 pm # :

    wesley » dirname(__FILE__) is fine to guess where physically the plugin is. But you cannot guess a URL from it and it's not aware of where the rest of WP is.

  16. 96
    wesley Poland »
    thought, on 19/Aug/08 at 4:50 pm # :

    You're right, sorry :)

    Btw, i sent you an email via your contact page.

Pages: « 1 2 3 4 [5] Show All

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?

Close
E-mail It