Loading wp-config.php in Main PluginFile

Found this in a plugin submitted to WordPress.org/extend/plugins tonight:

require_once(dirname(__FILE__).'/../../../wp-config.php');

If you need to access information from wp-config.php, there are several ways to do it.

This is mostly bad for two reaons:

1. You are assuming the path to wp-config.php will never change (it can and will)

2. You are reloading a file that is already loaded

Advertisements

7 thoughts on “Loading wp-config.php in Main PluginFile

  1. Perhaps this isn’t the best place to teach, but I think it would be helpful to have what the appropriate code should be, especially if it is a one liner like this. I can tell that there should be a reference made to find out where WordPress is installed and then include the file based on that… and for that matter, not sure why they would think the file wouldn’t be included to start with. So maybe this isn’t the best place to start with a lesson. It certainly helps me to see good code snippets though that I can use in the future.

    • This site is meant more for fun than anything. I do plan to link to relevant tutorials when appropriate, so no worries there 😉

      I don’t plan to use this for publishing tutorials, though when appropriate I will most certainly make notes about why it’s bad.

  2. Brilliant that you’re doing this site Pippin. But I gotto agree with Scott. It would be fantastic if you could include the revamped code here in the post, but then link to any tutorial to provide in depth explanations as to why the new code is better. Even if there’s not tutorial, adding the code here would mean a little additional work to what you’ve already done, but would make each microblog post so much more valuable and educative resource. Not to mention that n00bs like me can understand why we should be revelling at the crappy code in question!

    Either way, thanks for a(nother) great thing you’re doing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s