10 Useful Code Snippets for WordPress


Small things can make a big difference. This statement is absolutely correct when we talk about WordPress. Small things make this platform so great in a number of ways that you can customize it as you like to accomplish all your needs. Users find this platform powerful, flexible and easy to use.

There are several WordPress plug-ins available online that can fulfill every need imaginable. But when you are using WordPress.com, then it is difficult to install plug-in because WordPress.com has its limitations. In order to use the plug-in, you need to have a self-hosted WordPress.org. To avoid such hassles, you can get your job done with the help of a simple snippet. It is generally the path of least resistance. In this article, we will discuss different useful snippets from around the web, all of which can make your experience with WordPress a little bit easier and more functional.

What are code snippets?

A snippet is a small piece of PHP code that can be used to enhance the functionality of a WordPress powered website. It can be considered as a mini-plug-in with fewer loads on your website. Snippets are an easy, clean and simple way to add code to your website.

Moreover, Code Snippets also provides graphical interface just like plug-in menu, to deal with snippets. You can both activate and deactivate snippets just like a plug-in. You can also transfer them to another side, either in XML or PHP.

How To Use And Manage WordPress Code Snippets?

You will find many resources recommending you to add a new code snippet directly within active theme’s function.pjp file, but this is the not the correct way to incorporating snippets. It may result in a long and unorganized file that will make it difficult to navigate and edit when required. You may have to use WordPress Code Snippets plugin as it will make the whole process better.

Code Snippets plug-in makes it easy for you to add and organize code snippets on your WordPress website. It will give you a very familiar interface, a lot like WordPress page archive or post, where you can add a new snippet, delete snippets, edit existing snippets or search for new snippets.

A free code snippets plugin will allow you to produce your own customized repository of snippets that can be activated or deactivated as needed without messing around with your functions.php file.

Let’s check out some of the snippet codes that are commonly used in WordPress website.

1. Automatically Link Twitter Names To Their Accounts

Have you ever referenced someone on Twitter handles using their usernames and have them be linked automatically to the right profile? If yes, then here is a quick trick that will help your website up so that you can also type in someone’s Twitter name, and his/her Twitter profile will be linked automatically. Here is a small Snippet that will take care of your posts for text that indicates user name and it will automatically create a link for you.

 Add twitter names code snippet for WordPress

2. New User Registration And Redirecting Them To A Certain Page

When a new user register with your WordPress website, you need to redirect the new user to a certain page of the website upon successful completion of Registration. The code snippet will be like this:

New user registration WordPress code snippet


3. Shorten Blog Excerpts

If your excerpt of your blog post is too long, you can reduce it to 20 words. It will make your blog post page look clean and apt. Here is the code snippet:

 shorten blog excerpts code snippet for WordPress

4. Remove the Private/Protected From Post Titles

Sometimes you need password protected or private posts in WordPress. But you will notice that the title of the post is prefaced by a nasty reminder. To make it look good, add the below snippet.

 Remove private from post titles WordPress code snippet

5. Remove the WordPress Version Number

In case you want to remove your WordPress version number from visitors, simply add a short snippet.

 Remove WordPress version number code snippet

6. Display Comments In Admin To Authors Own Posts Only

By default, authors can see all the comments on their own posts when they log in through WordPress admin area. Here is a snippet that restricts authors, hide all the comments and let them look after their own comments.

Display comment WordPress code snippet


7. Remove the URL field in comments

When your personal or website blog is always hit by spammer as they share their URLs in comment section of the blog, you need to stop appearing these spammy links. Add this code snippet and remove the URL field.

 Remove URL Field in comments WordPress code snippet


8. Add Your Google Analytics Tracking Code

Many times you have to update your parent theme. When you do so, the last thing that you should not forget to re-add is Google Analytics tracking code. You can easily add it to your functions with the help of this code snippet:

 Add Analytics tracking code WordPress code snippet


9. Removing Featured Image When Deleting The Post

Sometimes you need to create posts and upload featured images from front end, but when people delete this post, you also need to remove these attachments when these posts are deleted. This is necessary otherwise there will be multiple orphan images taking up lots of precious space in your WordPress media library. Add this snippet to the functions.php and get your images removed when you delete a post.


10. Display YouTube Video with Embed Shortcode

With the help of a simple shortcode, you can easily embed YouTube video to your WordPress theme. You just need to add the YouTube URL to the video and specify a few parameters like width, height, name, etc. Once you are done, you are ready to go!

 Display youtube video with embed shortcuts

If there is any favorite WordPress snippet that you like the most, share it with us in the comments below.

Leave a Reply

Your email address will not be published.