Support » Developing with WordPress » How Do I Include wp-load.php in a Plugin?

  • I am trying to get a plugin approved, but WordPress won’t allow me to use the following line of code:

    require_once(‘../../../wp-load.php’);

    WordPress says that I need to use hooks instead. How do I call wp-load.php using a hook?

    They sent me an email saying:

    ## Calling core loading files directly
    
    Including wp-config.php, wp-blog-header.php, wp-load.php directly via an include is not permitted.
    
    These calls are prone to failure as not all WordPress installs have the exact same file structure. In addition it opens your plugin to security issues, as WordPress can be easily tricked into running code in an unauthenticated manner.
    
    Your code should always exist in functions and be called by action hooks. This is true even if you need code to exist outside of WordPress. Code should only be accessible to people who are logged in and authorized, if it needs that kind of access. Your plugin's pages should be called via the dashboard like all the other settings panels, and in that way, they'll always have access to WordPress functions.
    •	https://developer.wordpress.org/plugins/hooks/
    
    If you need to have a ‘page’ accessed directly by an external service, you should use query_vars and/or rewrite rules to create a virtual page which calls a function. 
    •	https://developer.wordpress.org/reference/hooks/query_vars/
    •	https://codepen.io/the_ruther4d/post/custom-query-string-vars-in-wordpress
    
    If you're trying to use AJAX, please read this: 
    •	https://developer.wordpress.org/plugins/javascript/ajax/
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator t-p

    (@t-p)

    Use this short guide if this is your first plugin: https://developer.wordpress.org/plugins/

    Section 3. is about Hooks what make your plugin interact with WordPress.

    Thread Starter PayDelete

    (@paydelete)

    Thanks for the reply, but I am not looking for links to vague information about plugins and hooks in general.

    I am looking for a specific way to achieve the same functionality as including wp-load.php in a way that WordPress will allow in a plugin.

    Right now I’ve tried including items from the wp-includes folder but that has led to a new error following the inclusion of cache.php which says:

    Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH' (this will throw an Error in a future version of PHP) in C:\Bitnami\wordpress-5.7-2\apps\wordpress\htdocs\wp-includes\cache.php on line 12
    
    Warning: Use of undefined constant WPINC - assumed 'WPINC' (this will throw an Error in a future version of PHP) in C:\Bitnami\wordpress-5.7-2\apps\wordpress\htdocs\wp-includes\cache.php on line 12
    
    Warning: require_once(ABSPATHWPINC/class-wp-object-cache.php): failed to open stream: No such file or directory in C:\Bitnami\wordpress-5.7-2\apps\wordpress\htdocs\wp-includes\cache.php on line 12
    
    Fatal error: require_once(): Failed opening required 'ABSPATHWPINC/class-wp-object-cache.php' (include_path='.;C:\php\pear') in C:\Bitnami\wordpress-5.7-2\apps\wordpress\htdocs\wp-includes\cache.php on line 12
    Moderator bcworkz

    (@bcworkz)

    Your options are quite limited actually. You can use AJAX to make a server request through admin-ajax.php and have your code run as an AJAX handler. You can create a page template out of your code, then create a page post type entry based on that template. To run your code, simply request the page by its title slug. Not the best choice for plugins. Your only other option is to send your request through /wp-admin/admin-post.php. Similar to AJAX but no client side code required.

    Thread Starter PayDelete

    (@paydelete)

    So, you’re saying its best to keep using wp-load.php and require people that want to use my plugin to download it directly from my website instead of the plugin directory due to WordPress refusing to allow plugins that work just fine just for including that file?

    I mean, it doesn’t look like there is any way to include that file using add_action(). My next question is if you can call the functions in that file using a hook?

    Thread Starter PayDelete

    (@paydelete)

    I certainly have no interest in using AJAX for a plugin that simply creates randomized RSS feeds nor could I use AJAX in an XML file.

    Moderator bcworkz

    (@bcworkz)

    I forgot one other option, you could make REST API requests from the client. But if you don’t like Ajax, you probably won’t like the API either. But FWIW, you can even build your own API for whatever your plugin does. Including wp-load.php isn’t a good option because it needs a relative reference to be portable, but not all installations will use the the same relative reference because the plugins folder can be relocated.

    Going through admin-post.php may be your best option. Its drawback is output is not automatically themed, you’d have to manage that yourself. What you can do to that end is limited since you don’t know what theme is used. Most plugins provide shortcodes or editor blocks so users can decide how to manage output.

    Thread Starter PayDelete

    (@paydelete)

    I usually use APIs when I need to fetch information in response to use inputs using javascript. I don’t typically use them when writing server side code like PHP.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.