• battleooze

    (@battleooze)


    Hello, I’m coding first plugin (and probably doing this wrong!). My current directory structure goes something like this:

    -index.php has plugin declaration in comments at start of file
    -standard.php has things like constant declarations & my way of including wp-load into other files which goes like this:

    function iam_require_load()
    {
    //suppress warnings of constant
    if(@constant(‘ABSPATH’)!=null){
    require_once ABSPATH.’wp-load.php’;
    } else {
    require_once realpath(‘../../../’).’/wp-load.php’;
    }
    }

    Which I’m assuming is a bad way to do that. Basically I made this with because I need to use things like “get_site_url()” and $wpdb in various files both in the same directory and in my templates sub directory.

    I read a couple blogs which said I should not be including wp-load in a production wordpress and so I exclaimed “But then how do I include these things I need!?!?” Which lead me to the assumption that I’m doing something horribly wrong.

    Any help is much appreciated! : D

Viewing 4 replies - 1 through 4 (of 4 total)
  • Joe Ponzio

    (@new-nine)

    You don’t include it in your plugin because it will already exist. The plugin is loaded when WordPress is loaded; so, you don’t need to call wp-load.php.

    So basically any time your plugin loads, functions like site_url should already be available to your code when it executes.

    The only time you would need to call wp-load.php is if you need to load WordPress in an external script and WordPress would not otherwise be loaded. But again – you’re building a WordPress plugin which only runs when the site is loaded up, so you don’t need to include wp-load.php.

    Moderator bcworkz

    (@bcworkz)

    The only time you would need to call wp-load.php is if you need to load WordPress in an external script

    And even then you do not actually call wp-load.php. If you want anyone to be able to use your plugin you must never include or require wp-load.php, never ever.

    So what to do with an external script that needs to access WP functions? You have 3 choices. 1) Use AJAX. 2) Make your external script into a page template, then add a page based on it, then load that page. 3) Run your requests through admin-post.php, which is sorta like AJAX callbacks except you don’t necessarily need javascript client side.

    Dion

    (@diondesigns)

    And even then you do not actually call wp-load.php. If you want anyone to be able to use your plugin you must never include or require wp-load.php, never ever.

    While true from within WordPress, one must include wp-load.php to access WordPress from an external-to-WordPress PHP script.

    Joe Ponzio

    (@new-nine)

    Great discussion! We’ve built some very large, complex websites using WordPress, and have on many occasions brought in tons of third party libraries and code via custom themes and plugins, and I can’t remember the last time we’ve needed to call wp-load.php.

    DionDesigns is correct – if you’re trying to access WordPress from another application, you’d have to call it. But if you’re building a WP theme or plugin, or otherwise using WP as your main framework, and you find yourself calling wp-load.php, there’s almost certainly a better way to do what you’re trying to achieve.

    wp-load.php is used when WordPress isn’t loaded and you need it to do so. If you’re working in WordPress…it’s loaded ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Including wordpress functions’ is closed to new replies.