-5

I was wondering what way is the best to include wp-config.php into a plugin. I have read about relative paths that can fail if the plugin files are situated somewhere else.

  • Are you actually talking about a WordPress plugin or are you trying to load WordPress from an external file? – Andrew Bartel Aug 27 '14 at 22:08
  • 1
    If you modify your question to be more specific to your actual problem, then you might get better answers. – Otto Aug 27 '14 at 22:09
  • I had problems using $wpdb in my plugin, I saw an example that used include wp-config. I will try again with $wpdb – stijn.aerts Aug 27 '14 at 22:14
8

The best way is to not do that. There's no need for a plugin file to ever include the wp-config.php.

You're doing it backwards. WordPress loads plugins. Plugins don't load WordPress.

  • There are valid reasons to include wp-config.php. For example for plugins that receive notification from APIs, a callback file can include the config file to access the database. In this case, making the callback file be loaded from Wordpress is an awful practice. – ethmz Jun 25 '18 at 11:34
0

wp-config.php should always already be loaded, and should not need to be included.

In most cases, the best way to ensure $wbdp is included in your plugin and theme functions is to ensure your theme and plugin files are found in the correct location and your plugin or theme is active.

If global $wpdb is not yet set, then you shouldn't need to include any core WordPress files. It most likely means your function is just being fired too early, and you should place it inside an action, such as muplugins_loaded, plugins_loaded, after_setup_theme, init, or wp_loaded.

If you have a separate file that you use like an ajax request, then you should switch it out and use the WordPress ajax hooks instead.

The only valid time I've found to include wp-config.php is to expand upon the common if ( ! defined( 'ABSPATH' ) ) exit;, so that users are redirected home when they attempt to access any of my theme or plugin files / directories directly.

<?php
/**
 * Silence is golden, but I actually want people to view the website.
 */
if ( ! defined( 'ABSPATH' ) ) {
    // Call dirname() 4 times as this file is in '/wp-contents/plugins/plugin-dir/' and wp-config.php is in '/'
    require_once dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/wp-config.php';
    // Permanent redirect home.
    wp_safe_redirect( home_url(), 301 );
    die;
}
-5

If you only want access to wp-config.php without the rest of the WordPress functions.

Do the following steps:

  1. create a empty wp-settings.php file in your plugin directory

  2. ad this to your PHP file

    define( 'ABSPATH', $_SERVER['DOCUMENT_ROOT'] . '/wp-content/plugins/YOUR_PLUGIN_DIR/' ); 
    require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-config.php' );
    

This works perfect for WordPress 4.2.4 "down and up" and I tested it in our own plugin mywebapplets.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.