Why do users with high reputations suggest loading wp-load.php with a full path?

My question is simple and was originally intended as an answer to this question. However I do not have a high enough reputation to answer or comment to that question. Rant over - sorry.

Surely loading wp-load.php with a hard coded path is bad practice. If the installation moves or your plugin is loaded into an installation where wp-load.php is in a different location, it will fail.

Browsing around I have found a few alternatives and others are welcome. The best I can tell is using the absolute path variable provided by WordPress.

   require_once( ABSPATH . 'wp-load.php' );
   global $wpdb;

Personally I have had no trouble initializing the $wpdb object, but I am sure this time will come. Nonetheless I found the linked question above as the top answer from Google and thought I should add in something else to help the ol' community.

  • 1
    I'd probably answer that it's best practice to entirely avoid having to include wp-load.php, but the context of your code isn't clear.
    – Milo
    Jan 13 '15 at 4:20
  • 1
    ABSPATH defined by WordPress is not available until you load wp-load.php, require_once( ABSPATH . 'wp-load.php' ); won't work.
    – cybmeta
    Jan 13 '15 at 8:43
  • Thanks guys for replying. @Milo: I agree completely. As mentioned in my question, this was originally intended as a response to another question where loading wp-load.php was given as an answer.
    – Kitt
    Jan 15 '15 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.