There are few WordPress websites online that have not been modified in some way or another. The most common way of extending or modifying the functionality of a website is to install a WordPress plugin, however functions can also be added to theme files. In fact, the majority of WordPress themes contain functions that modify WordPress in some way.
Code snippets are little pieces of code that can be inserted directly into your theme files. Sometimes they contain full functions, other times they simply modify an existing function.
In this article, I would like to show you eight useful code snippets that will enhance WordPress. I have tested that all snippets are working using the current default theme Twenty Fourteen, however some functions may not work correctly if your theme has been modified a lot (particularly if it is a framework).

1. Empty Your Trash

As a failsafe, WordPress will keep a copy of all posts, pages and comments you delete; unless you specifically go into your trash folder and delete items permanently. The trash works in the same way as the Recycle Bin on the Windows operating system.
WordPress will automatically clear out your trash every thirty days, however this can be reduced by adding the following line of code to your wp-config.php file (this file is located in the root of your WordPress installation):
   define ('EMPTY_TRASH_DAYS', 7);
If you want to optimize your database further so that no unnecessary items are stored in your database, you can disable the trash system altogether by adding this line of code to your wp-config.php file:
   define ('EMPTY_TRASH_DAYS', 0);

2. Reduce Post Revisions

The WordPress revision system saves a draft of your posts and pages each time you save an article. This feature is important to bloggers as it allows them to refer to earlier drafts and stops any work being lost in the event of a lost connection.
Unfortunately, these drafts take up a lot of room in your database as the default version of WordPress defines no limit on the number of drafts which are saved. This means that a large post that has been saved one hundred times would take up one hundred rows in your database table.
To address this issue, you can reduce the number of post revisions to a more sensible number by adding the following code to your wp-config.php file:
define( 'WP_POST_REVISIONS', 3 );
If you would prefer to disable the post revision system altogether, simply add this code to your wp-config.php file:
define( 'WP_POST_REVISIONS', false );

WordPress also autosaves your posts and pages every sixty seconds. The interval in which posts are saved can be modified by adding the following code to your wp-config.php file:
define( 'AUTOSAVE_INTERVAL', 160 ); // Seconds

Comments