Today I would like to share with you 25 useful code snippets for WordPress. These snippets will enhance or modify your WordPress website in some way. They are very easy to use. All you have to do is add or modify the relevant template with the code detailed in this article.

The snippets will work on any regular WordPress installation. If you are using a WordPress framework, you may need to adjust the code in some way and add the function in a different way. Every framework works in a different way so be sure to refer to the documentation for the framework you are using.

* Remember to backup your template files and/or database before performing any edits. Accidents happen! 🙂

1. Remove the url field from WordPress comment form

Template File to Edit: functions.php

This handy little function will remove the URL field from your comment box. Worth using if you are only getting comments from spammers.

2. Display comments in admin to authors own posts only

Template File to Edit: functions.php

By default, authors can see all comments when they log in to the WordPress admin area. This function restricts authors so that they can only see comments on their own articles.

3. Temporary Maintenance

Template File to Edit: functions.php

This function can be added to your functions.php template to temporarily close down your site to visitors. Your website can still be accessed by administrators, allowing you to make the changes you need without worrying about messing up your live website.

To display your website to visitors again, simply delete the function or comment it out.

4. Break Out of Frames for WordPress

Template File to Edit: functions.php

I find it unbelievable that in 2013, many people are still trying to wrap the content of an external website inside a frame. I have seen many Twitter users do this recently, which results in an ugly sharing bar at the top of your website.

To stop anyone from placing your website inside a frame, simply add this function to your functions.php template.

5. Set attachment as featured image

Template File to Edit: functions.php

This function will automatically set the first image that is uploaded to a post as a featured image.

6. Add featured images to WordPress feeds

Template File to Edit: functions.php

WordPress still does not offer the functionality to display a post’s featured image in the RSS feed. Hopefully this is something they will add in the future. Until then, you can add featured images to your feed by adding this function to your functions.php template.

7. Add titles to previous_post_link & next_post_link

Template File to Edit: single.php

This is a useful little snippet to use if you use previous and next links on your articles. It will enhance the links by showing the actual post name, which should encourage more people to click the links.

8. Exclude pages from WordPress search results

Template File to Edit: functions.php

WordPress will show results from pages in regular search results. This is not always what you want, particularly if you only have a few pages. It is also common for bloggers offer a free gift to email subscribers such as an eBook; yet few actually hide these pages. A quick way to resolve this issue is to remove all pages from search results.

The function can be expanded by adding more post types to the query.

9. How to redirect to post if search results only returns one post

Template File to Edit: functions.php

WordPress will send visitors to your search.php template after a search to display a list of posts. It does this even if there is only one search result. This function addresses the issue. If there is only one search result, the user directly to the post in question.

10. Set Minimal Comment Limit In WordPress

Template File to Edit: functions.php

If you are sick of poor quality comments such as “Great post”, “Thanks” or simply “:)”; you should add this function to your functions.php template. It allows you to set a minimal length for comments. Simply change the value of the $minimalCommentLength string to the minimum number of characters you require for each comment.

11. Increase WordPress Memory Limit

Template File to Edit: wp-config.php

The default amount of memory in WordPress (32MB) is controlled by the php.ini file. Some plugins need more which can result in an “out of memory” error message. The amount of memory WordPress uses can be modified directly through the WordPress configuration file that is located at www.yoursite.com/wp-config.php. Simply change the value of WP_MEMORY_LIMIT to 64M, 128M etc.

12. Custom Class To Last Post In A Loop

Template File to Edit: functions.php

This function will add a new class to the last post in your loop. By using this function, you can style the post listed at the bottom of your home page and archives differently from the rest.

13. Hide WordPress Core Update Messages

Template File to Edit: functions.php

If you are building a WordPress website for a client or for a friend, you may want to hide the WordPress update notification in the admin area. The function below will do just that, therefore reducing the chance of someone inexperienced trying to update WordPress.

14. Admin Only Notes

Template File to Edit: functions.php

This function will allow you to post notes to all other admin. It’s a useful snippet for reminding other admin of important information.

Admin Notice
‘ . $content . ‘

15. Add Content to the End of Each RSS Post in WordPress

Template File to Edit: functions.php

Add content to the bottom of every post of your RSS feed. You could use this to discourage scrapers and add a return link back to your blog.

16. Allow PHP in WordPress text widgets

Template File to Edit: functions.php

The default text widget in WordPress only allows text. There are some widget plugins in the official plugin directory that allow you to add PHP inside a widget. Alternatively, you can enhance the default text widget to allow PHP by adding this function to your functions.php template.

17. Move WordPress Admin Bar to the Bottom

Template File to Edit: functions.php

The WordPress admin bar is displayed in your header when you are logged in. You can move the bar to the button using a plugin. Alternatively, you can move the admin bar to the bottom of every page using this function.

18. Redirect your author archive link to your “About” page

Template File to Edit: functions.php

If there is only one author on your website, the authors link is kind of pointless as people can view all your posts through your archives template. This little snippet will redirect the author link to your about page instead.

19. Delete unused post revisions

Template File to Edit: SQL query

Revisions can take up a lot of room in your database. By running this query in your database manager (e.g. phpMyAdmin), you can delete all of your revisions.

20. Assign all articles by Author B to Author A

Template File to Edit: SQL query

This short little SQL query allows you to transfer all posts from one author to another. A great time saver.

21. Delete all Spam Comments

Template File to Edit: SQL query

If you have hundreds or thousands of spam comments sitting in your comment administration area, deleting all of them can be tedious. This simple SQL query deletes all spam comments at once.

22. Change Image Path Only

Template File to Edit: SQL query

Many WordPress users host their images on an external content delivery service in order to speed up the loading time of their pages and reduce the load on their current hosting solution. If you decide to do this, you need to change the image location of all of your previously uploaded images.

To do this, you need to perform two SQL queries on your database:

And:

23. Remove Settings Menus From the WordPress Dashboard

Template File to Edit: functions.php

Another code snippet for those of you are designing a website for a client or friend. This function allows you to completely remove the settings options from the WordPress admin area. This is something I recommend doing for novice users as there is a possibility they will stray onto the options page and mess something up (leaving it to you to fix it!).

24. Style First Paragraphs

Template File to Edit: functions.php

A useful snippet from Jake Rocheleau that allows you to style the first paragraph of your posts and pages differently from the rest of your content.

25. Add Thumbnails in Manage Posts/Pages List

Template File to Edit: functions.php

Display the corresponding thumbnail when you browse posts and pages through your admin area.

I hope you found this list useful. If you know of any other great WordPress snippets, please feel free to share them in the comment area 🙂

Image Credit

FREE EBOOK: The Ultimate Guide To Speed Up Your Website and Increase Conversions!

FREE

Site Speed Secretsis a is a step-by-step blueprint about how to speed up your website and increase conversions.

Scroll to Top