Saturday, 22 March 2014

Eight Useful Code Snippets for WordPress

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);



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.
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


Move Your WP-Content Folders

The wp-content folder contains your themes, plugins and uploads. Certain plugins, such as caching plugins, also use the wp-content folder to store data.
Due to this, the wp-content folder is frequently a target for hackers, particularly those that insert malware into your theme files. You can make it difficult for people to find your wp-content directory by moving it to another area of your website.
If you want to simply move the wp-content folder to another location, you can add this code to your wp-config.php file:
* Notice the wp-content folder does not have a trailing slash
define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/newlocation/wp-content' );
If you prefer, you can define the new location using the URL:
define( 'WP_CONTENT_URL', 'http://www.yourwebsite.com/newlocation/wp-content' );
WordPress also allows you to rename your wp-content folder using:
define ('WP_CONTENT_FOLDERNAME', 'newfoldername');
Renakming your wp-content folder can make WordPress website even safer, however it is unfortunately not always practical to do so because many WordPress plugin developers continue to hard code “wp-content” into their plugin code. It may still be worth doing if security is a top priority, though be aware that it may require you to manually update the code of many plugins you use (and these would have to be manually updated every time you updated the plugin).






Redirect Author Archive Link to Your About Page

The author archive link that is listed in the meta information area of a blog post links to a page that displays all previous posts by the author. Sometimes a bio is displayed at the top this page too.
If you run a single author blog, there is no need for you to link to author archives as the same posts are linked in your category and monthly archives. A better solution is to link your author archive link directly to your about page.
You can do this by adding the following code to your theme’s functions.php file:
add_filter( 'author_link', 'my_author_link' );
  
function my_author_link() {
    return home_url( 'about' );
}

Post a Comment