It’s being more than 4 years for me now to use WordPress and within all these years I worked on number of WordPress-based projects. While working on all these projects, I have been able to build up a pretty big collection of little snippets and tricks that helps me from time to time.

I’ve been added some new and old WordPress snippets to the sites while all the snippets I’ve been adding have been tested with the WordPress 3.5+ version. Based on all the experimentations I did, you will find the collection of best 21 useful snippets that I used for mine WordPress websites.

Rich-Snippets

Remove URL from Comment Form

It often happens that you don’t want to include the URL field in the WordPress comment form and at that time you would be requiring a snippet which helps you out to turn it off. You have to add the following snippet in the function.php of your theme.

Display a Random Post

The snippet will show a random post on your website. All you have to do is to add the following snippet in your template where you want it to appear.

Customize the Default Avatar

If you want to use a customized default avatar instead of using the standard WordPress one, all you need to do is to place the following snippet in the functions.php in your theme.

Create a Custom Post Template

Many of you don’t know that you do have an option to create custom page templates, although you can’t by default create templates for your posts. In order to make this happen, you have to download the Single Post Template plugin, which in turn will enable the ability to add post templates.

Once done, you have to make a copy of the single.php file of your theme and give it a new name that you want to keep it (say for example, “example.php”). Keep the file in the same location where you saved single.php. Now to create a custom post template, just copy the following snippet to the top of the page and you are done. Make sure that you have save it and it will be with the other files in the root of your theme directory. Now all you have to do is to go into the WordPress admin, edit a post and you will see a dropdown from where you can specify a post template.

Limit Number of Post Revisions

WordPress having the authority itself to keep the record of all the revision you have made to a page or post. If you are a person who is in practice of having hundreds of revisions per page or post, then this particular snippet is something that you definitely require. The snippet will limit number of post revisions. All you have to do is to put up the following snippets in the wp-config.php file in the root directory of your website.

Temporary Maintenance

The purpose of using the snippet is that you can temporarily close down your site for visits while your website can still be accessed by admins, which let you do the changes that you are planning for. In order to make your website live again for visitors, all you have to do is simply delete the function or comment it out.

Add featured images to WordPress feeds

The functionality to add featured images to WordPress feeds still not offered by WordPress itself, but in order to have this with you, you need to add featured images to your feed by adding the following snippet to your functions.php template.

Set Minimal Comment Limit In WordPress

A comment can either make your blog or break it out in front of your visitors as getting a good response is really valuable but a poor quality comment like “Great Post”, “Thanks” or just a smiley will harm your reputation. In order to avoid this or to stop on making such things happen, simply change the value of the $minimalCommentLength string to the minimum number of characters you require for each comment to your functions.php template.

Allow PHP in WordPress Text Widgets

Although the default text widget in WordPress only allows text but in order to add PHP inside your widget, you need to add this function to your functions.php template.

Redirect your Author archive link to your “About” page

If there are many authors on your website, then it would be very much required for you to redirect your author archive link to your “About” page and in order to make this happen, all you have to do is to add this little snippet to your functions.php template.

Change Image Path Only

In order to speed up the loading time of your website pages, you do often host the images on an external content delivery services. To help you with this, all you need to perform two SQL queries on your database which can do the complete job for you in a go.

Add Thumbnails in Manage Posts/Pages List

To display the corresponding thumbnail at the time when you browse posts and pages through you admin area, you need to add the following snippet to the functions.php template.

Checks if the visitor is from mobile device

The purpose to add this function is that to check if the user is visiting using a mobile device and returns result in true or false. To avail this function, you need to add the snippet to your functions.php template.

Add titles to previous_post_link & next_post_link

If you are using previous and next links on your articles, then this little snippet will be a perfect thing for you. It will add titles to previous post links and next post links in your article, which encourages more visitors to check on your content.

Disable Plugin Updates

If there is any updates for your plugins, and if it’s irritating you a lot, then this snippet simply prevents WordPress from checking it. This snippet could be useful especially if you are working on client’s website.

List all categories with posts

To have this function work for you, you need to add this to the template where you want the list to appear.

Override WordPress and Site URL

The snippet will allow you to redefine two constraints which make it possible for you to work with production databases, locally. The function does not change the database records or something although also there is no need to search and replace a whole lot of data in SQL. In short it lets you use the same database for production and development.

Get the first link in post

The time when you are using the new post formats, you will find the link type. It should be a post which has the intention to link to something other, instead of providing a lot of content. The snippet will allow you to find that first link in the post and will return back to you by which you can link the title to that place again.

Check if sidebar has widgets

The snippets checks that if a sidebar of your website having any widget areas in it or not.

Author Bio Excerpt

You want to show the author bio in your blog but instead of showing the complete bio, you want it short. This snippet will make an excerpt of the bio, which includes a link to the author page, where you can display the full author bio. All you have to do is to add the snippet to the functions.php template.

Disable Self Trackbacks

The WordPress trackback system will let you have a link back to your blog when other put up your content. But, the problem comes at a place when you link to one of your own posts, on your own website and a trackback will be displayed with the link coming from you. To avoid this, just simply put the snippet in functions.php.