Conquering the wp-config.php File – 11 Good Practices

Conquering the wp-config.php File – 11 Good Practices

Tutorial Details
  • Program: WordPress
  • Version (if applicable): 3.0+
  • Difficulty: Varies from easy to medium
  • Estimated Completion Time: 11 × 3 minutes

There are 981 files and 95 folders which come with the WordPress (v3.4.1) package. None of these files need manual modification, except the wp-config.php file. Of course, we don’t have to edit the file if we’re fine with the default WordPress configuration but it’s essential that we learn how to conquer the file in order to apply security precautions, speed tricks and other stuff which we will be studying in this article.


First Things First: Back Up!

Better safe than sorry: Back your content up, right now! Either use the built-in export page or use a plugin or back up from phpMyAdmin, but always have the power of undoing what you did while tweaking your website.

The operations could affect the database but they will not do anything with any of the files except the file we’re going to work with, so backing the wp-config.php file up is adequate… but if you haven’t backed up your files for more than a month, I suggest doing that too. Frequent backups are always good.

Ready? OK, here we go!


Speed: Disable the Revisions… Now!

The revisions feature for posts is enabled by default, but can lead to significant database bloat. Revisions are there so you can revert to a previous version of a post if you need to. If you don’t plan on using revisions to check the “earlier versions” of your posts, you definitely should disable this feature by adding the following line to the wp-config.php file:

define('WP_POST_REVISIONS', false );

However, if you’re fine with revisions but you’re not going to benefit from unlimited copies of your edited posts, you can limit the maximum number of revisions for each posts with this line of code:

define('WP_POST_REVISIONS', 2 );

Speed: Set a Cookie Domain

If you serve static content (i.e. your media uploads) from a subdomain, it’s a good idea to set a “cookie domain”. By doing that, cookies won’t be sent each time static content is requested.

define('COOKIE_DOMAIN', 'www.yourwebsite.com');

Quick Tip: To serve your media uploads from a subdomain, simply point the last two text fields on the Media Options page to the path (for example /home/myblog/public_html/mysubdomain) and URL (for example http://mysubdomain.myblog.com/) of your subdomain.


Speed: Change the Filesystem Method

If you install, update or delete your plugins and themes frequently, chances are you kind of hate entering your FTP password every time you deal with them. The code below makes it easier for you by forcing the filesystem to use direct file I/O request from within PHP – in other words, you won’t need to enter FTP credentials anymore.

define('FS_METHOD', 'direct');

Please note that this one might not work with every hosting provider and even if it works, it might cause security issues with poorly configured hosts. So make sure that you’re using it on a decent server.


Security: Restrict Access to the wp-config.php File

This tip requires you to edit the .htaccess file in your root directory, not the wp-config.php file. It basically prevents evil minded people from loading yourblog.com/wp-config.php directly with a browser:

# protect wpconfig.php
<files wp-config.php>
	order allow,deny
	deny from all
</files>

Just add this to your .htaccess file and you’re good to go!


Security: Force SSL on the Admin Panel

Is SSL enabled on your server? Great! You can force WordPress to use a secure connection while you’re logging in with this line of code:

define('FORCE_SSL_LOGIN', true);

And if you’re extra paranoid about security (which is a good thing, really), you can make WordPress use SSL on every admin page so everything you do in there is done with an encrypted connection:

define('FORCE_SSL_ADMIN', true);

You can find additional information about setting up SSL in the WordPress Codex on the Administration Over SSL page.


Security: Change the Database Prefix

If WordPress had a security flaw which allowed evil minded people to use the hacking method known as “SQL injection“, they would easily use the default prefixes on your WordPress database tables to delete them. However, if you have a different table prefix than the default (wp_), they wouldn’t be able to guess that, would they?

So, while setting up a new WordPress website, either change the default value on the installation page or in the wp-config.php file, change the line below:

$table_prefix  = 'wooh00yeah_';

Beware: If you want to make this work in an existing WordPress site, you can’t just change the prefix on the wp-config.php file – you’ll get database connection errors. You should use a plugin for that to change the wp-config.php file AND the database tables AND some specific values inside those tables. I recommend the DB Prefix Change plugin.


Security: Add Security Keys… Now!

Let’s just read from the WordPress Codex:

In simple terms, a secret key is a password with elements that make it harder to generate enough options to break through your security barriers. A password like “password” or “test” is simple and easily broken. A random, unpredictable password such as “88a7da62429ba6ad3cb3c76a09641fc” takes years to come up with the right combination. A ‘salt is used to further enhance the security of the generated result.

This is one of the most essential security precautions for WordPress – and it’s easy as copying and pasting the randomly generated content of this page to your wp-config.php file. The hardest part is finding the default, empty values of these constants and deleting them! :)


Other: Change the Autosave Interval

If you sometimes work on your post for 4 hours, you might find it annoying that WordPress automatically saves the state of your post every 60 seconds. I’ll give credit that it’s not a bad thing but sometimes it’s really, really annoying. Anyways, if you want to set the autosave interval to a higher value, you can do it by defining it in the wp-config.php file like this:

define('AUTOSAVE_INTERVAL', 240 ); // the value should be in seconds!

Other: Easily Move Your WordPress Website

WordPress is full of surprises, and this is one of them. If you ever need to move your website to a new domain (or a new subdomain, or a new folder), define this constant on your wp-config.php file before moving your files and database:

define('RELOCATE',true); // We're not done yet!

After setting this and moving your FTP and database, log in with your WP credentials on yournewwebsite.com/login.php and after that, check if the home URL has changed on the General Options page. After confirming that it has changed, delete the constant in your wp-config.php file. This little trick of WordPress’ saves you the burden of editing the database manually.

Tip: While this literally “moves” your website, it doesn’t affect the hard-coded links in your content. To replace them, you should use a plugin like Search Regex and change the old links with new ones.


Other: Disable Editing of Plugin & Theme Files

If you’re a web designer and using WordPress with your clients’ websites, you might want to disable the editing of theme and plugin files by adding the constant below:

define('DISALLOW_FILE_EDIT',true);

Better yet, you can also disable installing new themes and plugins, and updating them:

define('DISALLOW_FILE_MODS',true);

Just remember that theme and plugin updates are sometimes very important when they fix security flaws. So if you’re going to disable updating and installing new plugins/themes, you’re going to have to track the updates in a different way.


Other: Enable WP_DEBUG While Developing

This is an easy one: If you’re developing a plugin or a theme, it’s good practice to enable the debug feature of WordPress to see what kinds of notices and warnings you’re getting:

define('WP_DEBUG',true);

Sometimes it’s amazing to see how easy mistakes you can make while developing! :)


Conclusion

We chose 11 great tips and tricks for your WordPress websites but the tricks for the wp-config.php file are, of course, not limited to these ones. Do you have any good tricks to share? Your comments are always welcome!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Tamil Selvan

    Nice post……

    • Barış Ünver
      Author

      Thanks, Tamil! :)

      • http://www.lotusmarketing.ca/conception/ Conception Sherbrooke

        Yeah Good post. I’ve been using WordPress alot lately, I am amazed at how easy it is to modify. The codex library is complete with examples.

        I do not understand why some web agencies are creating there custom CMS. Just use wordpress.

  • Pingback: Conquering the wp-config.php File – 11 Good Practices | Qtiva

  • Gijs Jorissen

    Useful post!!

    • Barış Ünver
      Author

      Thank you :)

  • http://cloudofthemes.com Connor Crosby

    Wow, there are some really good tips! Regarding changing the database prefix – I read somewhere that it’s better to keep wp_ as part of the prefix. So, an example would look like this blahblah_wp_. Anyways, great tips, and definitely bookmarked! :)

    • Barış Ünver
      Author

      Of course, it’s a good idea to include the “wp” if you’re using the database for other applications. Thanks for the tip!

  • Pingback: wpconfig.php tips from WPTuts | Everchanging Media

  • http://aotearoawebdesign.co.nz Jason

    Excellent – thank you!

    • Barış Ünver
      Author

      Glad you like it! :)

  • Pingback: Conquering the wp-config.php File « Tech Snippets

  • http://imdev.in Devin Walker

    Nice article. You should definitely include how to increase WordPress’ memory limit from 32M:

    define(‘WP_MEMORY_LIMIT’, ’64M’);

    This is something I see all the time as people love to load a ton of plugins and quickly run out on their more-often-than-not shared hosting environments (GoDaddy, I’m looking at you).

    • http://socialmedia101.artizondigital.com Sue Surdam

      Thanks for adding that. I have had to do that on more than one site – yes GD eyes are on you!

  • http://www.whatsthebigidea.com David Radovanovic

    Great article for the sometimes forgotten power of the wp-config.php file.

  • http://www.voxwebdesign.com Federico Schafer

    Great Article. Very Helpfull. In my case I never needed to Change the Filesystem Method. WP has never asked for FTP Credentials, neither in my local development server nor in my shared hostings.

    • Barış Ünver
      Author

      Correct, some hosting providers doesn’t require this method.

  • http://www.mojowill.com MojoWill

    Actually found a couple of these useful. Wouldn’t recommend the direct file method unless working on a local development server though, think it generally removes a layer of security for updates etc

  • http://twosevenzero.com Dave West

    Excellent post. One of the best on WPTuts. Didn’t know about RELOCATE and I am constantly migrating wp sites. Thanks!

    • http://socialmedia101.artizondigital.com Sue Surdam

      Relocate was new to me too. I am definitely going to use it on the next site I migrate.

  • http://www.shadowfaxdigital.com Chris

    Regarding moving a WP site, I have found it easiest to take the exported SQL file, open it in my favorite editor–which happens to be Espresso–and do a search and replace for the original domain and the new domain. For example if I’m developing locally I would change

    localhost:8888/site to newsite.com and then import that into the new database that I’ve created for the new site.

    Seems to work for me with no issues. I’m not saying this is the bulletproof, right way, but it seems to be simple and straight forward without using any plugins. Anyone have an objection to this method?

    • http://www.damiencarbery.com Damien

      Be very careful doing this – some references are in serialized strings where the length of the string is stored so if your local and new urls are different lengths you will need to change the length.

      See: http://ie.php.net/manual/en/function.serialize.php

    • http://wp.envato.com/ Japh Thomson
      Staff

      Damien is quite right here, Chris. Better off using a tool like interconnect|it’s Search Replace DB script which unserializes and re-serializes values: http://interconnectit.com/124/search-and-replace-for-wordpress-databases/

    • http://prop-14.com Randy

      @chris – I have actually used this find/replace method also well over 100 times and I have never had a major issue. The only data that ever gets lost for me is a few serialized widgets data. Usually that is all I need to “manually” change after moving.

      Although it is a new tip to me, I see no point in using RELOCATE because using the find/replace takes care of that. Plus, I am always keeping the local copy so I’d rather not change anything at all on that version, even if it’s just temporary. That’s just my opinion though, maybe someone has had a worse experience using this method?

    • http://www.chidgey.me/ Gareth Chidgey

      Many plugins also store file locations relative to the server root, ie /my-server/mysite/public_html/, this is the one that has caught me a out a few times.

      Fortunately, there is a great free plugin http://wordpress.org/extend/plugins/wp-migrate-db/

      All you do is enter the new domain and root to the website on the server and it exports the database as an SQL in the exact same way as phpmyadmin. You don’t have to mess about with find and replace or anything as it’s all done in the export.

      12 happy, quick migrations with it now, not a single problem caused by the plugin, just one caused by me leaving in a trailing slash.

  • http://blog.mattsatorius.com/ Sators

    Great stuff here!

  • http://freakify.com/ Ahmad Awais

    Wonderful post Barış Ünver !
    Loved the tips.

    • Barış Ünver
      Author

      Thank you, Ahmad! :)

  • Pingback: 11 tips for the wp-config from wordpress | Local Wisdom

  • Subash Pathak

    W00h Yeh.

  • http://helpfolder.com Mahesh

    The tip for FS_Method was handy. I usually turnoff autosave because I had issue with half post published with that trick. So I have that disabled on my end. Thanks a lot for these tips.

  • http://www.sanjaykhemlani.com/ sanjay

    Good post about wp-config.php, I only know about the debug mode when working on a client site. That .htaccess snippet is a must for every website!

  • wayno

    Very useful information, especially about relocations. Thanks!

  • http://seer.ws Ersan Seer

    The DB Prefix Change plugin you recommended messed with my credentials.

    I can’t login to WordPress. What’s the fix?

  • http://seer.ws Ersan Seer

    This fixed the admin privileges problem I had with your recommended DP Prefix Change plugin:

    https://ericwijaya.wordpress.com/2012/04/13/you-do-not-have-sufficient-permissions-to-access-this-page-after-database-prefix-change/

    Just goes to show that you have to be careful installing anything, even from (apparently) trusted sources.

    • Barış Ünver
      Author

      Sorry to hear that, Ersan. Before recommending the plugin, I tested it with a WordPress blog that I have and it was the latest version of WP. Do you have the latest version of WordPress?

  • Evan

    Regarding the portion about moving wordpress using the relocate method, the url given (yournewwebsite.com/login.php) should be yournewwebsite.com/wp-login.php.

    The codex page for this is here: http://codex.wordpress.org/Changing_The_Site_URL#Relocate_method which also outlines some other ways to change the site url however the relocate method is probably the best/easiest in my opinion.

  • http://routinechaos.com Josh T

    This is one of the best articles I have read that talks about the most vulnerable security windows, more often than not I find partially answered questions that I always have and this is top notch. Thanks for writing this, I can’t wait to start to implement some of these techniques!

  • Evert

    I have something like the following in all my config files which enables me to keep the same config-file on both development and live servers:

    if ($_SERVER['SERVER_ADDR'] == “127.0.0.1″) {
    define(‘WP_DEBUG’, true);

    define(‘DB_NAME’, ‘localdb’);
    define(‘DB_USER’, ‘xx’);
    define(‘DB_PASSWORD’, ‘xx’);
    define(‘DB_HOST’, ‘localhost’);

    define(‘WP_SITEURL’, ‘http://localhost/xxx/’);
    define(‘WP_HOME’, ‘http://localhost/xxx/’);
    }
    else {
    ini_set(‘display_errors’, ‘off’);
    define(‘WP_DEBUG’, false);

    define(‘DB_NAME’, ‘xx’);
    define(‘DB_USER’, ‘xx’);
    define(‘DB_PASSWORD’, ‘xx’);
    define(‘DB_HOST’, ‘xx’);

    define(‘FTP_HOST’, ‘xx’);
    define(‘FTP_USER’, ‘xx’);
    define(‘FTP_PASS’, ‘xx’);
    }

    • http://www.facebook.com/Jamesbrunwin James Brunwin

      Nice idea here, Might have to try this out myself.

  • http://tomdurkin.me Tom Durkin

    Amazing tips thanks! Love the security related stuff.

  • Frank

    How is one able to stop access of xyz.com/wp-admin.php
    for other people who are browsing through the site?

  • George

    Great post! I shall surely be using some of these tips on my projects!

    Thank you!

    George

  • http://irie-design.fr dready

    Great tips thanks!

  • http://gonzalo.seriche.cl gseriche

    Nice Post!
    a lot of tips than I didn’t know!

    Regards!

  • http://memeLab.com.au/ Tim Osborn

    Great list!

    Here’s one that has caught me when migrating – I believe the Installatron script adds the line below _directly_ under the Salts, so that its camouflaged to the tired eye :)

    define(‘WP_TEMP_DIR’, ‘/home/oldsite/public_html/wp-content/uploads’);

    If the site is migrated with this line (unchanged) in wp-config, then _most_ of WP will function, but uploads, plugin installs and updates will fail! Removing the line solves the issue.

    • http://wp.envato.com/ Japh Thomson
      Staff

      Nicely spotted, Tim! Though, you shouldn’t remove the line, instead you should update it to have the correct path to the new location of your WordPress install. Removing it altogether could create unforeseen problems that you may not come across immediately.

  • Pingback: How to prevent WordPress from asking FTP credentials

  • Pingback: How to prevent WordPress from asking FTP credentials | Wordpress Webdesigner

  • Pingback: How to prevent WordPress from asking FTP credentials | Best Web Consulting company in Nashik, India with Creative and Professional Website Design, Content Management Systems, Wordpress Experts, Ecommerce SEO, and more..

  • http://www.planetmike.com Michael Clark

    Another way to protect your wp-config.php is to move it one directory up, out of your public web server. Some hosts or server setups may not be able to handle that, but it is a very simple change to make.

  • Pingback: How to prevent WordPress from asking FTP credentials

  • Pingback: 10+ wp-config tricks to boost your WordPress site | CatsWhoCode.com

  • Pingback: 10+ wp-config tricks to boost your WordPress site | Easy jQuery | Free Popular Tips Tricks Plugins API Javascript and Themes

  • Pingback: 10+ wp-config tricks to boost your WordPress site « The Blog of Jordan Rynard

  • Pingback: My Favorite wp-config.php Tips and Tricks - ManageWP

  • Pingback: My Favorite wp-config.php Tips and Tricks | Talking Wordpress

  • Pingback: Boas práticas para seu Wordpress

  • http://twitter.com/ShovanSargunam Shovan Sargunam

    Thanks you should start using

    define(‘RELOCATE’,true);

  • Pingback: Publishing, Optimizing and Coding: Top WordPress Resources for November 2012 - ManageWP

  • http://tamanmerah.tumblr.com Day Milovich

    enable debugging sometimes is not good if you by default set to display error message.

  • Karen

    for the section “Security: Restrict Access to the wp-config.php File”, where in your htaccess file are you supposed to add it? Do I add it after the “# BEGIN WordPress” or before it, or doesn’t it matter?

    Thanks

  • CL75

    Regarding revisions disabling, it doesn’t seem to work. Does the code need to be inserted at a certain line?

  • Pingback: 34 of the Best WordPress Posts of 2012 - ManageWP

  • Pingback: 10+ wp-config tricks to boost your WordPress site | CMS Radar

  • Pingback: wp-config tip and trick

  • Pingback: 10 نکته برای نصب ایمن وردپرس - بلاگ شرکت طراحی سایت WEB RGB

  • Pingback: 10+ wp-config tricks to boost your WordPress site | Artsinweb