WordPress works with PHP to process and assemble content from the database. WordPress makes many tasks simple by offering parameters and functions that you can control in wp-config.php and functions.php . In addition, you can also add directives in .htaccess file to control the behavior. Here are some of the WordPress code snippets that you can use to modify the parameters and work as you desire.
How to Use These WordPress Code Snippets?
You have to insert the given code snippet in one of the following file:
- wp-config.php located in root directory of your site
- functions.php located in /wp-content/themes/yourtheme/
- .htaccess located in root directory of your site
Out of these three files, any changes you make with functions.php will be wiped off when you update your theme. You can either use child theme or update the code every time after updating theme.
1. Schedule Trash Clean Up
When you delete a post or page, WordPress by default send the content to trash section without any warning. However, the trash content will be there forever on your site unless you manually delete them. The purpose of this code snippet is to schedule a automatic job to empty the trash periodically to save your time.
You have to insert the code snippet in wp-config.php file.
define('EMPTY_TRASH_DAYS', 5 );
Replace 5 days to any number you want to schedule the cleanup job as per your need.
2. PHP Memory Limit
Locate wp-config.php file and insert the below snippet to increase PHP memory limit in WordPress site.
define('WP_MEMORY_LIMIT', '96M');
96M indicates, 96MB of memory for running PHP scripts. You can change the value as per your need like 256MB or 512MB. However, make sure to confirm with your hosting company for the maximum allowable limit. Note that many popular hosting companies like SiteGround offers PHP Manager app in your hosting account. You can use this app and modify the memory limit without editing config.php file.
Learn more about changing PHP memory limit in WordPress.
3. Maintenance Mode
Edit your theme’s functions.php file and add the below snippet to show quick maintenance mode:
function maintenace_mode(){
if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
die('Maintenance.');
}
}
add_action('get_header', 'maintenace_mode');
To disable maintenance mode – simply go to “Appearance > Theme Editor” and delete the above code from your theme’s functions.php file. Alternatively, use FTP or File Manager to edit functions.php file.
4. Limit Post Revisions
You can limit the maximum number of revisions in WordPress by adding the below code snippets to your wp-config.php file.
Use the below snippet for limiting maximum of revisions to 5. You can change this number to any number as you prefer.
# Maximum 5 revisions #
define('WP_POST_REVISIONS', 5);
Use the below code if you want to completely disable revisions.
# Disable revisions #
define('WP_POST_REVISIONS', false);
Learn more about controlling revisions in WordPress.
5. Autosave Interval
Autosave is a useful feature in WordPress to save your content when you preparing to publish online. It saves the entire content on the editor every few minutes and save it as a separate backup on your database. However, this can be annoying and sometimes struck without allowing you to upload or publish content. If you are not doing something mission critical, setting the interval to longer time is preferred.
Copy the below code snippet and paste in your wp-config.php file.
# Autosave Interval 10 Minutes #
define('AUTOSAVE_INTERVAL', 600);
You have to indicate the time in seconds. In the above code example, we have used 10 minutes interval by mentioning 600 seconds. You can change to any preferred value that works for your need.
Learn more about autosave in WordPress.
6. Leverage Browser Caching
Web browsers like Google Chrome, Safari, Firefox and Microsoft Edge cache the static files on your webpage. This will help browsers to speed up loading your content when the user visits the pages on your site next time. This is technically called browser caching. In order for the browsers to cache your static files, you have to tell them how many days they should keep the cache on the visitors computer. The static files include images, stylesheets, JavaScript files, icons and any other file types you serve on your site.
Insert the below code snippet in your .htaccess file.
## EXPIRES CACHING ##
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
## EXPIRES CACHING ##
We have used 1 year and 1 month for keeping the cache on the browser. You can change the time period, in case if you update the content on your site frequently. Otherwise, long caching period may cause the old content loaded on the browser if the user did not cleanup the cache on the browser.
Learn more on how to leverage browser caching in WordPress.
7. Limit Post Title Length
Did you ever notice WordPress did not limit the length of your post title? However, you can add the below code in your functions.php to limit the length.
function maxWord($title){
global $post;
$title = $post->post_title;
if (str_word_count($title) >= 10 )
wp_die( __('Error: Title exceeded the length...') );
}
add_action('publish_post', 'maxWord');
Here 10 indicated the number of words allowed in post titles. You can change the number as per your need.
8. Set Minimum Words for Post
Similar to post title, you can use the following code snippet to set minimum number of words in your content post. In this way, you can avoid publishing thin content with less number of words.
function minWord($content){
global $post;
$num = 500;
$content = $post->post_content;
if (str_word_count($content) < $num) wp_die( __('Error: Title below specified length...') ); }
add_action('publish_post', 'minWord');
You can change 500 to any number to set the number of words in your post content.
9. Limit Excerpt Length
Add the below code snippet in functions.php to limi the length of your excerpt. Here, the limit is set as 25, you can change it as per your need.
function new_excerpt_length($length) {
return 25; }
add_filter('excerpt_length', 'new_excerpt_length');
10. Remove WordPress Version
WordPress by default insert a generator tag in the source code to show the installed version. You can disable this by using plugins. However, insert the below code in functions.php to achieve the same without any plugin.
function no_generator() { return ''; }
add_filter( 'the_generator', 'no_generator' );
Note: We update this list continuously, so bookmark and come back to check new snippets added.
Leave a Reply
Your email is safe with us.