15 Useful WordPress Configuration Tricks That You May Not Know

If functions.php is the single most important file in your WordPress theme, then wp-config.php is the single most important file in your entire WordPress installation. In this article, we will share some of the most useful WordPress configuration tricks that you may not know yet.

The post 15 Useful WordPress Configuration Tricks That You May Not Know appeared first on WPBeginner.

WP-config is one of the most powerful files on your WordPress site, and it plays an important role in how WordPress works behind the scenes. There are some very useful WordPress configuration tricks that most beginners don’t know about. In this article, we will share some of the most useful WordPress configuration tricks that will help you troubleshoot, optimize, and secure your WordPress site.

Useful WordPress configuration tricks

How to Use these WordPress Configuration Tricks?

WordPress comes with a powerful configuration file called wp-config.php. It is located in the root folder of every WordPress site and contains important configuration settings.

To learn more, see our guide on how to edit wp-config.php file in WordPress.

All the best WordPress hosting companies come with 1-click WordPress installation which means you would never need to edit the wp-config.php file during the installation. This is the main reason why many users are not familiar with the power of this file.

You can use the wp-config file to troubleshoot, optimize, and secure your WordPress site.

The wp-config.php file is a powerful tool, and a tiny mistake in the code can make your website inaccessible. You should only edit this file when necessary and always create a complete WordPress backup before making any changes,

That being said, let’s take a look at some handy WordPress configuration tricks that you can use on your website.

1. The Basic WordPress Configuration Settings

By default, you just need to fill in the database settings during WordPress installation. If you don’t have a wp-config.php file present, then you will be asked to create one by filling in your database information.

Default WordPress configuration settings

WordPress will try to automatically save these settings by generating a wp-config.php file. However, if it fails, then you will need to add them manually.

To do that, you will need to connect with your website using an FTP client. Once connected, you will need to rename the wp-config-sample.php file to wp-config.php.

Rename wp-config-sample.php file

After that, you can go ahead and edit the newly created wp-config.php file. You will need to add your database information by changing the following lines:

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

Don’t forget to save your changes and upload the file back to the server.

2. Adding Security Keys in WordPress

The default WordPress installation automatically adds security keys to your configuration file. These security keys are used to add an extra security layer to your WordPress login and cookie authentication.

You can always regenerate security keys if you feel someone may be accessing your website without proper authentication. Changing security keys will log out all logged in users.

define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

For more information, see our article on WordPress security keys and how to use them.

3. Change WordPress Table Prefix

A typical default WordPress installation adds a wp_ prefix to all WordPress database table names. Some WordPress security experts believe that changing the table prefix can make your WordPress database more secure.

To do that you, need to change the following line in your WordPress configuration.

$table_prefix = 'wp_';

If you are doing this for an existing website, then you will also need to change the table prefix in your WordPress database. To do that, see our article on how to change the WordPress database prefix.

4. Turn on Debugging in WordPress

WordPress comes with a neat debugging feature that allows you to see or hide WordPress errors when in debug mode. To turn this on, you will need to add this rule in your WordPress configuration file.

define( 'WP_DEBUG', true );

You can also turn on debugging while hiding the errors on your website and saving them in a log file instead. To do that, add the following lines to your configuration settings.

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

This will create a debug.log file inside wp-content folder of your website and store all debugging errors and notices inside the log file.

5. Changing Your Site or WordPress Address

Normally, you can set your WordPress and Site URLs from Settings » General page. However, you may not be able to do that if you don’t have access to your WordPress site, seeing redirect errors, or have just moved your site.

In that case, you can change your site and WordPress URLs via wp-config.php file by adding the following lines:

define('WP_HOME', 'http://www.example.com');
define('WP_SITEURL', 'http://www.example.com');

Don’t forget to replace example.com with your own domain name.

6. Override File Permissions

WordPress allows you to override file permissions if your host has restrictive permissions for all user files. Most users do not need this, but it exists for those who need it.

define('FS_CHMOD_FILE', 0644);
define('FS_CHMOD_DIR', 0755);

To learn more about file permissions, see our article on how-to fix file and folder permissions error in WordPress.

7. Changing Post Revision Settings

WordPress has a very useful a post revisions feature which allows you to undo changes to your posts and pages by reverting back to a previous version or an autosave.

You can disable or change post revision settings through the configuration file. Here are different post revision settings that you can use.

You can change how often WordPress stores an autosave as a revision by adding the following line:

define('AUTOSAVE_INTERVAL', 120); // in seconds

Some articles on your site may have dozens of post revisions depending on how long it took to write them. If you think that feature annoys you, then you can limit the number of revisions per post.

define('WP_POST_REVISIONS', 10);

If for some reason, you want to disable the post revisions feature altogether (not recommended at all), then you can use the following code to disable post revisions.

define( 'WP_POST_REVISIONS', false );

8. Changing WordPress Trash Settings

WordPress comes with a recycle bin feature called Trash. When a user sends a post to trash, it is still stored on your website for next 30 days as trash. After that time, WordPress automatically deletes them forever.

You can change this behavior by changing the number of days you want to keep the trash.

define( 'EMPTY_TRASH_DAYS', 15 ); // 15 days

If you do not like this feature, then you can disable it by adding the function below:

define('EMPTY_TRASH_DAYS', 0 );

Note: Using zero means your posts will be deleted permanently. WordPress would not ask for confirmation when you click on Delete Permanently. Any accidental click could cost you…

To learn more, see our article on how to limit or disable automatic empty trash feature in WordPress.

9. Adding FTP/SSH Constants to WordPress Configuration

By default, WordPress allows you to upgrade WordPress core, themes, and plugins from the admin dashboard. There are some hosts that require an FTP or SSH connection everytime you try to upgrade, or install a new plugin.

WordPress asking for FTP information

By using the codes, you can set the FTP or SSH constants and never have to worry about it again.

// forces the filesystem method: "direct", "ssh", "ftpext", or "ftpsockets"
define('FS_METHOD', 'ftpext');
// absolute path to root installation directory
define('FTP_BASE', '/path/to/wordpress/');
// absolute path to "wp-content" directory
define('FTP_CONTENT_DIR', '/path/to/wordpress/wp-content/');
// absolute path to "wp-plugins" directory
define('FTP_PLUGIN_DIR ', '/path/to/wordpress/wp-content/plugins/');
// absolute path to your SSH public key
define('FTP_PUBKEY', '/home/username/.ssh/id_rsa.pub');
// absolute path to your SSH private key
define('FTP_PRIVKEY', '/home/username/.ssh/id_rsa');
// either your FTP or SSH username
define('FTP_USER', 'username');
// password for FTP_USER username
define('FTP_PASS', 'password');
// hostname:port combo for your SSH/FTP server
define('FTP_HOST', 'ftp.example.org:21'); 

Note: Don’t forget to replace the WordPress path and ftp.example.com with your own FTP Host information.

10. Allow Automatic Database Repair

WordPress comes with a built-in feature to automatically optimize and repair WordPress database. However, this feature is turned off by default.

To enable this feature you need to add the following line to your WordPress configuration file.

define('WP_ALLOW_REPAIR', true);

After adding this, you need to visit the following URL to optimize and repair WordPress database

http://example.com/wp-admin/maint/repair.php

Don’t forget to replace example.com with your own domain name. You will see a simple page with the options to repair or repair and optimize the database. You don’t need to be logged in to access this page.

Optimize and repair WordPress database

11. Increase PHP Memory Limit

Some of the most common WordPress errors are caused by PHP memory exhausted. You can increase the PHP memory limit through wp-config.php file. Simply paste the code below:

define('WP_MEMORY_LIMIT', '128M');

12. Moving wp-content Directory

WordPress allows you to move your wp-content directory. Some experts believe that it can help strengthen WordPress security.

You will need to add the following code to your wp-config.php file:

define( 'WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content' );
define( 'WP_CONTENT_URL', 'http://example/blog/wp-content');
define( 'WP_PLUGIN_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content/plugins' );
define( 'WP_PLUGIN_URL', 'http://example/blog/wp-content/plugins');

Don’t forget to replace example.com with your own domain name.

13. Use Custom User Tables

By default, WordPress saves all user data in the tables wp_users and wp_usermeta. By using the function below, you can specify the table where you want your user information stored.

define('CUSTOM_USER_TABLE', $table_prefix.'my_users');
define('CUSTOM_USER_META_TABLE', $table_prefix.'my_usermeta');

14. Enable Multi-Site Network

Each WordPress site comes with a built-in multisite feature which allows you to create multiple WordPress sites using the same installation. To learn more, see our complete guide on how to install and setup WordPress multisite network.

You can enable multisite functionality by adding the following line to your WordPress configuration file:

define('WP_ALLOW_MULTISITE', true);

15. Securing Your WordPress Configuration File

As you can see, the wp-config.php file contains really important WordPress settings. By default it is located in the root WordPress folder, but you can move it. It can be moved outside your public_html directory, so users cannot access it. WordPress knows by default to look in other directories if the files is not found in the WordPress root folder.

You can also add the following code to your .htaccess file to limit access to this file.

# Protect wp-config.php
<Files wp-config.php>
    order allow,deny
    deny from all
</Files>

We hope this article helped you learn some useful WordPress configuration tricks that you didn’t know. You may also want to see our mega list of 55+ most wanted WordPress tips, tricks, and hacks that you can use on your site.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

The post 15 Useful WordPress Configuration Tricks That You May Not Know appeared first on WPBeginner.

How to Edit wp-config.php File in WordPress

Did you read a tutorial that asks you to edit your wp-config file, and you have no idea what it is? Well we’ve got you covered. In this article, we will show you how to properly edit the wp-config.php file in WordPress. What is wp-config.php… Read More »

To leave a comment please visit How to Edit wp-config.php File in WordPress on WPBeginner.

Did you read a tutorial that asks you to edit your wp-config file, and you have no idea what it is? Well we’ve got you covered. In this article, we will show you how to properly edit the wp-config.php file in WordPress.

What is wp-config.php File?

As the name suggests, it is a configuration file that is part of all self-hosted WordPress sites.

Unlike other files, wp-config.php file does not come built-in with WordPress rather it’s generated specifically for your site during the installation process.

WordPress creating wp-config.php file during the installation

WordPress stores your database information in the wp-config.php file. Without this information your WordPress website will not work, and you will get the ‘error establishing database connection‘ error.

Apart from database information, wp-config.php file also contains several other high-level settings. We will explain them later in this article.

Since this file contains a lot of sensitive information, it is recommended that you don’t mess with this file unless you have absolutely no other choice.

But since you’re reading this article, it means that you have to edit wp-config.php file. Below are the steps to do it without messing things up.

Getting Started

First thing you need to do is to create a complete WordPress backup. The wp-config.php file is so crucial to a WordPress site that a tiny mistake will make your site inaccessible.

You will need an FTP client to connect to your website. Windows users can install WinSCP or SmartFTP and Mac users can try Transmit or CyberDuck. An FTP client allows you to transfer files between a server and your computer.

Connect to your website using the FTP client. You will need FTP login information which you can get from your web host. If you don’t know your FTP login information, then you can ask your web host for support.

The wp-config.php file is usually located in the root folder of your website with other folders like /wp-content/.

wp-config file is located in the root directory of your WordPress site

Simply right click on the file and then select download from the menu. Your FTP client will now download wp-config.php file to your computer. You can open and edit it using a plain text editor program like Notepad or Text Edit.

Understanding wp-config.php file

Before you start, let’s take a look at the full code of the default wp-config.php file. You can also see a sample of this file here.

<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the
 * installation. You don't have to use the web site, you can
 * copy this file to "wp-config.php" and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * MySQL settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://codex.wordpress.org/Editing_wp-config.php
 *
 * @package WordPress
 */

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/**#@-*/

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define('WP_DEBUG', false);

/* That's all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Each section of wp-config.php file is well documented in the file itself. Almost all settings here are defined using PHP Constants.

define( 'constant_name' , 'value'); 

Let’s take a closer look at each section in wp-config.php file.

MySQL Settings in wp-config.php File

Your WordPress database connection settings appear under ‘MySQL Settings’ section of the wp-config.php file. You will need your MySQL host, database name, database username and password to fill in this section.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');

/** MySQL database username */
define('DB_USER', 'username_here');

/** MySQL database password */
define('DB_PASSWORD', 'password_here');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

You can get your database information from your web hosting account’s cPanel under the section labeled databases.

MySQL databases in cPanel

If you cannot find your WordPress database or MySQL username and password, then you need to contact your web host.

Authentication Keys and Salts

Authentication unique keys and salts are security keys that help improve security of your WordPress site. These keys provide a strong encryption for user sessions and cookies generated by WordPress. See our guide on WordPress Security Keys for more information.

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/**#@-*/

You can generate WordPress security keys and paste them here. This is particularly useful if you suspect your WordPress site may have been compromised. Changing security keys will logout all currently logged in users on your WordPress site forcing them to login again.

WordPress Database Table Prefix

By default WordPress adds wp_ prefix to all the tables created by WordPress. It is recommended that you change your WordPress database table prefix to something random. This will make it difficult for hackers to guess your WordPress tables and will save you from some common SQL injection attacks.

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

Please note that you cannot change this value for an existing WordPress site. Follow the instructions in our how to change the WordPress database prefix article to change these settings on an existing WordPress site.

WordPress Debugging Mode

This setting is particularly useful for users trying to learn WordPress development, and users trying experimental features. By default WordPress hides notices generated by PHP when executing code. Simply setting the debug mode to true will show you these notices. This provides crucial information to developers to find bugs.

define('WP_DEBUG', false);

Absolute Path Settings

The last part of wp-config file defines the absolute path which is then used to setup WordPress vars and included files. You don’t need to change anything here at all.

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
	define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Useful wp-config.php Hacks and Settings

There are some other wp-config.php settings that can help you troubleshoot errors and solve many common WordPress errors.

Changing MySQL Port and Sockets in WordPress

If your WordPress hosting provider uses alternate ports for MySQL host, then you will need to change your DB_HOST value to include the port number. Note, that this is not a new line but you need to edit the existing DB_HOST value.

define( 'DB_HOST', 'localhost:5067' );

Don’t forget to change the port number 5067 to whatever port number is provided by your web host.

If your host uses sockets and pipes for MySQL, then you will need to add it like this:

define( 'DB_HOST', 'localhost:/var/run/mysqld/mysqld.sock' );

Changing WordPress URLs Using wp-config.php File

You may need to change WordPress URLs when moving a WordPress site to a new domain name or a new web host. You can change these URLs by visiting Settings » General page.

WordPress Address and Site Address settings

You can also change these URLs using wp-config.php file. This comes handy if you are unable to access the WordPress admin area due to error too many directs issue. Simply add these two lines to your wp-config.php file:

define('WP_HOME','http://example.com');
define('WP_SITEURL','http://example.com');

Don’t forget to replace example.com with your own domain name. You also need to keep in mind that search engines treat www.example.com and example.com as two different locations (See www vs non-www – Which one is better for SEO?). If your site is indexed with www prefix then you need to add your domain name accordingly.

Change Uploads Directory Using wp-config.php

By default WordPress stores all your media uploads in /wp-content/uploads/ directory. If you want to store your media files in someother location then you can do so by adding this line of code in your wp-config.php file.

define( 'UPLOADS', 'wp-content/media' );

Note that the uploads directory path is relative to the ABSPATH automatically set in WordPress. Adding an absolute path here will not work. See out detailed guide on how to change default media upload location in WordPress for more information.

Disable Automatic Updates in WordPress

WordPress introduced automatic updates in WordPress 3.7. It allowed WordPress sites to automatically update when there is a minor update available. While automatic updates are great for security, but in some cases they can break a WordPress site making it inaccessible.

Adding this single line of code to your wp-config.php file will disable all automatic updates on your WordPress site.

define( 'WP_AUTO_UPDATE_CORE', false );

See our tutorial on how to disable automatic updates in WordPress for more information.

Limit Post Revisions in WordPress

WordPress comes with built-in autosave and revisions. See our tutorial on how to undo changes in WordPress with post revisions. However, if you run a large site revisions can increase your WordPress database backup size.

Add this line of code to your wp-config.php file to limit the number of revisions stored for a post.

define( 'WP_POST_REVISIONS', 3 );

Replace 3 with the number of revisions you want to store. WordPress will now automatically discard older revisions. However, your older post revisions are still stored in your database. See our tutorial on how to delete old post revisions in WordPress.

We hope this article helped you learn how to edit wp-config.php file in WordPress and all the cool things you can do with it. You may also want to see our article on 25+ extremely useful tricks for WordPress functions file.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

To leave a comment please visit How to Edit wp-config.php File in WordPress on WPBeginner.