Cool Snippets for wp-config.php

by Apr 25, 2023WordPress, WordPress Maintenance0 comments

As a WordPress developer, you may already be familiar with the wp-config.php file. This file is an essential part of your WordPress installation and contains important configuration information.

But did you know that you can enhance your WordPress site’s functionality and performance by adding some cool and useful snippets to this file?

In this blog post, we’ll explore 12 snippets that you can add to your wp-config.php file to improve your WordPress site.

1. Disable Post Revisions

By default, WordPress saves a new revision for every change made to a post or page. While this can be helpful, it can also lead to database bloat and slow down your site’s performance. To disable post revisions, add the following code to your wp-config.php file:

define( 'WP_POST_REVISIONS', false );

2. Limit the Number of Post Revisions

If you don’t want to completely disable post revisions, you can limit the number of revisions that are saved. To do this, add the following code to your wp-config.php file:

define( 'WP_POST_REVISIONS', 3 );

This will limit the number of post revisions to three.

3. Increase PHP Memory Limit

If your WordPress site is running out of memory, you can increase the PHP memory limit by adding the following code to your wp-config.php file:

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

This will increase the PHP memory limit to 256MB.

4. Enable Debugging Mode

Debugging is an essential part of developing a WordPress site. You can enable debugging mode by adding the following code to your wp-config.php file:

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

This will enable debugging mode and log all errors to a file. You can find the log file in the wp-content directory, it will be called debug.log.

5. Disable Automatic Updates

By default, WordPress will automatically update itself, themes, and plugins. While this can be helpful, it can also cause compatibility issues. To disable automatic updates, add the following code to your wp-config.php file:

define( 'AUTOMATIC_UPDATER_DISABLED', true );  

6. Change the Database Table Prefix

By default, WordPress uses the “wp_” prefix for all database tables. This can make your site vulnerable to SQL injection attacks. You can change the database table prefix by adding the following code to your wp-config.php file:

$table_prefix = 'wpnew_';

Replace “wpnew_” with your preferred prefix.

7. Limit Post Revisions per Post Type

You may want to limit post revisions for certain post types only. To do this, add the following code to your wp-config.php file:

function custom_limit_revisions( $num, $post ) {
    if ( $post->post_type == 'my_post_type' ) {
        $num = 5;
    }
    return $num;
}
add_filter( 'wp_revisions_to_keep', 'custom_limit_revisions', 10, 2 );

Replace “my_post_type” with your post type name, and “5” with the desired number of revisions.

8. Disable the Plugin and Theme Editor

The WordPress dashboard allows you to edit plugin and theme files directly, but this can be a security risk. To disable the plugin and theme editor, add the following code to your wp-config.php file:

define( 'DISALLOW_FILE_EDIT', true );

9. Change the Site URL

If you need to change the site URL, you can do so by adding the following code to your wp-config.php file:

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

Replace “http://example.com” with your new site URL.

10. Change the Uploads Folder Location

By default, WordPress stores uploaded media files in the “wp-content/uploads” directory. You can change the location of this folder by adding the following code to your wp-config.php file:

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

Replace “wp-content/myuploads” with your desired uploads folder location.

11. Force HTTPS

If you want to force HTTPS on your WordPress site, you can add the following code to your wp-config.php file:

define( 'FORCE_SSL_ADMIN', true );
if ( strpos( $_SERVER['HTTP_X_FORWARDED_PROTO'], 'https' ) !== false ) { $_SERVER['HTTPS'] = 'on'; }

This will force HTTPS on the WordPress admin area and frontend.

12. Disable File Editing

By default, WordPress allows you to edit PHP files in the theme and plugin editor. This can be a security risk. To disable file editing, add the following code to your wp-config.php file:

define( 'DISALLOW_FILE_EDIT', true );

This will disable file editing for both plugins and themes.

In conclusion, the wp-config.php file is an incredibly powerful tool for managing and customizing your WordPress site. By adding these 12 snippets, you can enhance your site’s functionality, performance, and security.

Just remember to always make a backup of your wp-config.php file before making any changes, as a small mistake can cause your site to break.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments