KNOWNHOST BLOG

How to Produce Cleaner Code on Your Site – Part Two

Updated January 12, 2021

In Part 1, we covered 15 things you can do to help your site get more clean, lean and secure for improving user satisfaction and search engine rankings.

In Part 2, we’ve got another set of tasks that you can move ahead with, each of which will help get your site cleaned up and performing at its peak.

Remember to make a site backup and download it for safekeeping before making major changes to your hosting environment, platform, themes, plugins or files. This is not an overcautious suggestion – it’s a smart move!

Cleaner and Leaner Code Tasks (Part 2)

The tasks below won’t apply to every type of website or web hosting account, but are presented for the benefit of many SME site owners. If one doesn’t apply to you, then go on to the next.

PHP Version

PHP: Hypertext Preprocessor is a server scripting language that enables web developers to create dynamic sites using HTML. With regular version updates, over time PHP becomes more secure, more feature filled and faster.

PHP Task – Switch to a Newer Supported PHP Version (7.4)

Login to WordPress Admin, click Tools -> Site Health. Click the Info tab and click the Server section. Check the PHP Version.

If you’re not seeing 7.4.x (a 7.4 variant), then you’ll want to consider upgrading / switching versions. First, update your WordPress, theme(s) and plugins to their latest versions. Clear cache, make a site backup, download and save somewhere safe.

Login to cPanel, then in the Software section, look for the Select PHP Version icon. It’s here that you can select, via dropdown, alternative PHP versions.

If you don’t see 7.4 in the list, then your web hosting provider may not yet be ready to roll it out to customers. It’s at this point you’d want to check with technical support to find out when and possibly why it’s not yet available.

As soon as you switch PHP versions, check your site to confirm everything is working. Browse the site as a regular user. Login to the admin area, test it out and make sure everything is behaving properly. Check Site Health and see what it’s reporting.

Spam and Trash Comments

Cleaner code means keeping the database clean as well as the filesystem. With WordPress, if you’ve left the site open, allowing comments, it could be that you’ve been inundated with unwanted comments.

If you’ve kept on top of things and flagged comments as spam or trashed them, they are still cluttering up the database. Cleaning them out will help your WordPress function optimally. If you haven’t kept on top of the comments, you might just have a ton of pending comments that haven’t yet been flagged as spam, or trashed.

Cleanup of spam, trash and pending comments can be done with a plugin or manually using SQL statements. Make a backup before proceeding.

Comments Task – Clean up Spam, Trash (and possibly Pending)

Method A – SQL

Login to cPanel and launch phpMyAdmin.

Delete Pending Comments (only if there are many unwanted)
DELETE from wp_comments WHERE comment_approved = ‘0’;

Deleting Flagged Spam Comments
DELETE from wp_comments WHERE comment_approved = ‘spam’;

Deleting Trashed Comments
DELETE from wp_comments WHERE comment_approved = ‘trash’;

Method B – Plugin

Install and activate WP Bulk Delete plugin.

Select the comment status you want deleted, such as all Spam comments and delete.

Disable Pingbacks and Trackbacks

Frequently a source of SEO spam, pingbacks and trackbacks do have a purpose, but can quickly get out of hand, placing unnecessary loads on the database as their numbers rise.

Link Notifications Task – Disable Pingbacks and Trackbacks

Login to the WordPress Admin area. Click Settings -> Discussion.

Deselect the option to allow link notifications from other blocks (pingbacks and trackbacks).

Reduce Post Revision History

Being able to rollback a post to a prior version is handy if you’ve been making substantial edits and they didn’t go according to plan. But how many copies of the post does it make sense to save? 3? 5? 10? The default behavior for WordPress is an unlimited number of revisions!

By specifying the number of post revisions to retain, the WordPress database can be kept svelte and speedy. You may even want to remove the existing revisions, if you’re satisfied with the posts you’ve finished. The removal is beneficial, but optional, in case you’d like to keep some post history for further editing.

Revision History Task #1 – Limit the Number of Revisions Retained
Edit the wp-config.php file by adding the following line, changing the number 5 to be whatever number of revisions you’d actually like to keep for each post.

define( ‘WP_POST_REVISIONS’, 5 );

Revision History Task #2 – Delete Existing Post Revisions (optional)
Login to cPanel and launch phpMyAdmin.

DELETE FROM wp_posts WHERE post_type = ‘revision’;

Note: This assumes that your prefix is “wp_”.

Reduce Trash Auto Cleanup Interval

When you trash posts they go into a recycling bin of sorts for a default period of 30 days. That means every post you set as status “trash” will still stick around for 30 days before WordPress automatically performs cleanup.

There is a constant that can be set in the wp-config.php file which determines how many days will pass before a trashed post is cleaned.

Trash Auto Cleanup Task – Reduce Trash Cleanup Interval

Make a copy for safekeeping of your wp-config.php file, just in case. Then edit the file by inserting a new line (assuming a similar line isn’t already present), such as:

define(‘EMPTY_TRASH_DAYS’, 3);

In this example the trash cleanup interval will be reduced from 30 days to 3 days, giving you much less time to change your mind, but also giving you much less trash just hanging about waiting to be auto cleaned.

Cull the Herd of Low Quality Posts

Over time you’ll likely find that a small number of posts bring in the majority of traffic to the site while a sizable number of them are bringing in little to nothing. This could be because the content is thin (few words on the page), doesn’t contribute anything new or of value (similar to other pages in the index), competes with other pages on site (by being too similar), hasn’t been optimized for keywords that have decent search volumes or just doesn’t fit any common search intent very well.

In cases where you’ve got content that isn’t ranking well and bringing in traffic, you can reduce the database size and disk storage with a bit of housekeeping. In this case, you can completely remove the post(s), making note of their current URLs and then redirecting those URLs to other related content that could benefit from a bit of internal linking.

Low Quality Post Cleanup Task – Remove and Redirect

Check your Google Analytics to see URLs that are indexed but getting little to no monthly search traffic (make a list). Visit the posts and see if there are any easy wins where you could tweak, add a bit, optimize or differentiate and make them useful pages. If not, then proceed with the steps.

Find related posts for each one and make note of where you’ll want to redirect each.

Purge the low quality / non-fixable posts.

Add the old URL and new destination URL into your Redirection plugin and the redirect will be complete.

Note: This is similar to the task relating to removal and redirecting of discontinued ecommerce products.

Remove and Redirect Discontinued Products

Some ecommerce sites have products that come and go, being sold out and never to return once they’re out of stock. In these situations, rather than having landing pages where people can’t buy anything, the product pages should be redirected to active, similar products or offers.

Redirecting pages taps into the inherent value of the page, preserving any inbound or internal link juice the page may have acquired and passes it on to another page on site. Over time, this can mean the site mass gets bigger even though the raw number of active pages at any one time doesn’t get inflated. It’s good for SEO and the user experience, plus helps keep the database and filesystem clean and quick.

Keep in mind that redirecting the discontinued pages means more than just sold out / non-returning stock. It also means redirecting special offer landing pages – especially those which were promoted plus had links and social shares pointing to them. These pages have SEO value and shouldn’t just disappear off the map. They could be repurposed in some cases, but there are only so many uses for things like special holiday deals from a year or two ago!

Discontinued Product Page Cleanup Task – Removal / Redirection

If the product isn’t coming back into stock then there’s little point keeping it in the active pages lingering about. These are ideal for redirecting to active products or categories.

WooCommerce has posted advice on redirecting and has said the Redirection plugin for WordPress is a recommended solution. There are other redirect plugins available, but with over 1,000,000 installs and many glowing reviews, this is a proven path to redirecting success.

For SEO purposes, look to use permanent 301 redirects to notify search engines that the content is no longer at this location and instead can be found on the other product page on site. In order to get the maximum SEO benefit, you’ll want to redirect the old product page to something very similar content-wise. If the new destination is unrelated, then much of the passed authority will be diminished.

If you’d like to speed up Google seeing this change so that your new product page gets traffic more quickly, login to Google Search Console and select the URL Inspection Tool. Paste in your new product URL and select Request Indexing.

Note: This is similar to the task relating to removal and redirecting of low quality or outdated content that is no longer relevant, correct or being updated.

Remove Unused Images / Clean Up the Media Library

Ever upload a half dozen images then decide to use just one in a post? It happens, a lot! Combine this with the WordPress default behavior of making 3 different sized images for every upload (plus any created by other themes and plugins) and, over time, the filesystem and database suffer from the bloat.

Knowing which images to remove is much easier said than done. There are images in the uploads folder(s) as well as references within the media library. A proper cleanup means checking both against what’s actually needed and used within the site.

Image Cleanup Task – Remove Unused Images

There are a number of good plugins that can be used to do part of the cleanup, but to get the filesystem and media library both, it’s likely that a premium version of the particular plugin will be required.

Look at specialist tools like Media Cleaner or multifunction tools like WP-Optimize.

Offload Video (optionally Offload Images)

If you’ve ever run your site through a page speed checker like GTmetrix, you’ve probably noticed the number of images your pages have and the file size of any videos. Rather than keeping all these files on your web hosting account, it’s possible to offload them to another site and embed them in your pages.

The obvious advantage is the space savings for your hosting. When it comes to video, by offloading to YouTube you get an expanded audience, effectively giving you free marketing for your content. By having the content on YouTube, you’ll get found when people search on there, plus you get the social aspects that go with it such as comments, shares, and so on.

Video Cleanup Task – Consider Offloading Video to YouTube
Video from many different sites can be directly embedded in blog posts and widgets without need for a special plugin. However, there are some very cool features that video embed plugins bring to the table. It’s all down to how you intend to display the video content.

Image Cleanup Task – Consider Offloading Images to Google Photos
Video from many different sites can be directly embedded in blog posts and widgets

Logging and Log Files

Server statistics are information you can use to manage your account. But do you need to preserve historical information forever? Do you need 3 different logging services running?

Error logs give you insights into what’s going wrong with the server and the software running. Having a regular look at the error log and dealing with errors promptly is a part of having a server. Why keep those error logs around, especially older errors and ones you’ve already handled?

Logging Cleanup Task #1 – Error Logs

Login to cPanel, click on Metrics and then Errors. Deal with the errors and clean out the log file. If you’re using File Manager or FTP, have a look in the /tmp/ folder for log files.

Logging Cleanup Task #2 – Archived Access Logs

Within cPanel, click on Metrics and then Raw Access. To automatically remove archived logs from the month prior, tick the Remove Previous Month and click Save.

Logging Cleanup Task #3 – Choose Less Statistics Logging

Within cPanel, click on Metrics and then Metrics Editor. Untick whichever of the logging software you can do without (you can have any or all, it’s up to you) and then click Save.

By having less monitoring and logging, you’ll lighten the load in more ways than one.

Cleaning and Optimization Plugins and Tools

There are so many different WordPress specialty tools, it’s hard to single out just one. But for database optimization, there are plugins like WP Optimize and WP Sweep.

Multifaceted cleanup tools such as WP Asset Clean Up and Autoptimize are popular because they do so much.

Premium multifunction tools have an incredible range of features and work to improve performance through cleanup and optimization. These include Perfmatters and FlyingPress. If you want clean, lean, ultrafast site performance, have a look at both (though you wouldn’t need both – it’s more of an either/or).

Conclusion

Keeping your hosting account and web pages clean and lean is a sure fire way of getting even better performance out of your server, delivering pages faster and keeping site visitors happily sticking around.

If you’d like even more technical tips, such as how to remove unused CSS and JS, plus how to minify and compress your code, drop a comment or email to our team. We’ll be happy to take a deep dive with you!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.