WordPress Theme Detector Blog

Useful Code Snippets for WordPress

WordPress plugins are the most popular way of extending the functionality of WordPress. They allow you to change the way that WordPress works.

Plugins are simply functions written in HTML and PHP that are plugged into your website. Another way to change the functionality of WordPress is to use a code snippet.

Code snippets are small pieces of code that you add to your website directly.

  • Snippets that add functionality are commonly added to the theme functions.php file
  • Snippets that change functionality are sometimes added to a specific theme file e.g. single.php, page.php, search.php etc.
  • Snippets that change core WordPress functionality are usually added to the wp-config.php file that is located in the root of your WordPress installation i.e. www.yourwebsite.com/wp-config.php

Be aware that any additional code you add to theme files will be overwritten if you upgrade your WordPress theme. It is therefore prudent to take a note of any snippets you add to your website in a separate location. You should also check out the free WordPress plugin Code Snippets. It can be used to insert code snippets directly into your theme functions.php file.

In this article, I would like to share with you a collection of useful code snippets for WordPress. I hope you enjoy the list 🙂

1. Mark Comments with Very Long URLs as Spam

File to Edit: Functions.php

Spammers frequently link to web pages with very long URLs. Therefore, if someone publishes a comment and enters a long URL in the website field, there is a high chance that the comment was published by a spammer.

The snippet below allows you to mark a comment as spam that has a website URL over 50 characters. You can increase or decrease this limit to suit your own needs.


2. Remove the URL Field from the WordPress Comment Form

Marking comments that have very long URLs in the website field will help you tackle spammers. However, if you find that spam is getting out of control, you may want to consider removing the website URL field altogether.

If you implement the snippet below and configure your WordPress discussion settings so that any comment with a link is held for moderation; you can effectively stop all spam entirely.

File to Edit: Functions.php


3. Require Minimum Comment Length

File to Edit: Functions.php

A good way to tackle spam and encourage better comments is to apply a minimum length for comments. This helps stop small irrelevant comments such as “Great post” and “Thanks!”.


4. Remove Auto Linking of URLs in WordPress Comments

File to Edit: Functions.php

WordPress automatically turns URLs posted in comments into links. The downside to this feature is that it helps spammers publish links in their comments. You can remove this functionality with the following code snippet.


5. Automatically link Twitter usernames in WordPress

File to Edit: Functions.php

This useful function will automatically link Twitter usernames to the the person’s Twitter profile. Worth using if you frequently refer to Twitter users in your articles.


6. Exclude Pages from WordPress Search Results

File to Edit: Functions.php

By default, WordPress includes pages in search results. This means that pages such as about, contact, privacy, etc, will be included in search results. These types of pages are not necessary if they are linked in your main navigation menu. WordPress will also include private pages in search results too. For example, a newsletter confirmation page or an eBook download page.

To remove all pages from WordPress search, simple add the snippet below to your theme functions.php file.


7. Redirect To Post If Search Results Return One Post

File to Edit: Functions.php

When you search for something on a WordPress website, a search results page is displayed with all relevant posts, posts, and other post types. The problem with this setup is that WordPress will still display a results page if there is only one blog post result.

This code snippet addresses this issue. If only one blog post is found for a search, the visitor will be taken directly to the blog post in question.


8. Redirect a Successful Registration to Specific Page

File to Edit: Functions.php

You will find this snippet useful if you allow people to register on your website. It lets you redirect new users to a specific page after they have completed their registration.

This is useful if you permit visitors to sign up for an account to publish comments or join your discussion forum. For example, you can redirect new members to a thank you page that gives details of their membership.


9. Change Your WordPress Login Logo, URL, and Title

File to Edit: Functions.php

CSS Tricks published some useful snippets that can be used to customize your WordPress login page. The first snippet changes the logo that is displayed at www.yourwebsite.com/wp-login.php and www.yourwebsite.com/wp-admin/.

The second snippet allows you to customize the URL that is attached to your login logo. Rather than link to WordPress.org, the logo will now link to your website home page.

The third snippet can be used to change the title attribute of your login logo to the website title defined in your settings area.

All of the above functions should be added to your theme functions.php file.


10. Change the Author Slug URL Base

File to Edit: Functions.php

The default URL slug for authors is /author/name. For example, on WPThemeDetector my author URL is www.wpthemedetector.com/author/kevin-muldoon/.

You can change the author slug using the code snippet below. Simple change the author slug to your desired name. For example, instead of the word author, you could use profile, member, or account.


11. Allow PHP in Default Text Widgets

File to Edit: Functions.php

The default text widget in WordPress only supports HTML. If you insert the following snippet into your theme functions.php file, you can also use PHP code.

This greatly extends the functionality of the text widget. For example, you could use the text widget to display custom functions, PHP queries, calls to external files, and more.

12. Change the Number of Default Post Revisions

File to Edit: WP-Config.php

The WordPress revision system saves a copy of your posts and pages every time you update them. This is handy as it allows you to refer back to previous versions of your article, compare drafts, and correct mistakes.

The problem is that WordPress does not limit how many revisions it saves. Therefore, if you have saved an article 100 times, your website will store 100 revisions of the article. This can greatly increase the size of your database as one article with 100 revisions will take up just as much room as 100 articles with no revisions.

It is therefore practical to limit the number of revisions that are stored. On my own websites, I limit revisions to two. This allows me to refer back to the last few copies of an article, without taking up too much room in the database.

You can limit revisions by adding the code snippet below to wp-config.php.

Revisions can be disabled altogether by using the following code.

Be careful about disabling post revisions. If you lose your internet connection or accidentally close your browser tab, you could lose everything you had written since you last saved your article.


13. Empty Your WordPress Trash

File to Edit: WP-Config.php

When you delete something in WordPress, such as a comment, post, image, or link; it is not permanently deleted. Instead, it is sent to the trash folder. This fail safe system is used to allow you to retrieve items that were deleted in error. The downside to this is that deleted items will remain in your database until trash is emptied.

Items that are sent to the trash folder are automatically deleted after 30 days. You can reduce the number of days before trash items are deleted by adding the following code to wp-config.php.

The trash system can be completely disabled if you wish. You can do this by using this code.

Be careful of disabling the trash system completely as it means that there is no way of recovering an item once you have deleted it. At the very least, I recommend setting trash to be emptied after one day. This will let you recover any items you deleted by accident.


14. Modify WordPress Automatic Updates

File to Edit: WP-Config.php

WordPress introduced automatic background updates in WordPress 3.7. All automatic updates can be completely disabled by adding this code to your wp-config.php.

When it comes to updating WordPress, there are two types of updates: minor and major. Major updates add additional functionality to the WordPress platform. They can be identified quickly due to their WordPress version number e.g. WordPress 3.7, 3.8, 3.9, 4.0 etc.

Minor updates are updates of major versions of WordPress that have been released to address security holes and correct bugs. Their version number increases by 0.01 e.g. 4.01, 4.02, 4.03 etc.

By default, WordPress is configured to apply all minor updates automatically when they are released.

If you want to disable core updates, you can do so using the following code.

Minor and major updates of WordPress can be installed automatically by adding the following code to wp-config.php.

Once the above code has been added to wp-config.php, you can repair your database by going to www.yourwebsite.com/wp-admin/maint/repair.php.


15. Automatic Database Optimizing

File to Edit: WP-Config.php

In time, all WordPress websites accumulate overhead due to queries being processed. A little overhead in a database is normal, however if there is too much overhead, the performance of your website may be affected.

To remove overhead from a MySQL database, you need to repair it. WordPress allows you to repair your database automatically. All you need to do is add the following code to your wp-config.php file.


What is your favorite WordPress code snippet? Please share it with other WPThemeDetector readers in the comment area below 🙂


Kevin Muldoon
Follow Me
Latest posts by Kevin Muldoon (see all)


  1. Thank you Kevin for writing a great new tutorial for our blog.

    When I saw #12, Change the Number of Default Post Revisions, I had to ask myself: Why didn´t I do anything about this before?

    I mean, I hated all those innumerable revisions appearing every time I was editing a post. But as I was always busy with the publishing and editing, I never even gave it a thought to see how I could avoid such an annoying thing.

    Now I copied you and limited it to just 2 revisions 🙂

  2. Thanks Luis. I think 2 revisions is a good amount. It won’t take too much room in your database but still allows you to refer to the last two versions of your article.

  3. useful…..

  4. Wow full Code

  5. Thanks louis that’s very helpful…

    • Can you provide code for scroll to top button? that will be very helpful

  6. oh really thanks to provide that code..

  7. Great write up that even a fairly new user to WordPress can use. I like #3 – Require Minimum Comment Length. Lately I have been getting 2-3 word comments on 2 of my sites that are not really of much use to other readers. Hopefully I can implement this and start getting better comments.

    Thanks for a nice write up that’s easy to understand.

  8. Just limited post revisions to 2! Thanks a ton! 🙂

  9. I visit every day a few sites and information sites to read posts, but this website presents feature based articles.

  10. Hi, I need help with #9. … a bit more snippets for dummies clarifying please. 🙂 I managed to get as far as pasting in a url to my logo for step 1. It did not work. It did result in the WP logo being removed though. I proceeded to part 2. I got the link to my site working, which is good. The title still said wordpress.org though, so I went on to step 3. It does not show my site’s title. However, it no longer shows WP. So, I’m partway there, but I’m sure I left out something I was supposed to replace and don’t know what. My site’s in maintnenace mode now, so I can’t link to show you. If you could please tell us exactly what needs replaced in each step I’d be ever so grateful. Thanks.

  11. Thanks for this useful wordpress snippets

  12. Saved revision is kinda big discovery snippets for me because I used to wonder how to delete all the revision below the post editor but not anymore with your amazing snippets you’ve shared. Thanks a millions Muldoon. 🙂


  13. Great article, I had been trying to change the login URL for one of my WordPress sites and couldn’t find the exact information I needed. Then I scroll down to #9 on your list and there was exactly what I had been searching for.

    Great job and thanks for the helpful write-up.

    BTW, your Theme Detector is Awesome!

  14. its very good snippets

  15. In item 15 you say adding the code to your config will automatically optimise the database.

    define( ‘WP_ALLOW_REPAIR’, true );

    That’s not true, adding this simply makes the repair/maintenance mode available. Once you’ve added that code to wp-config, you need to visit the maintenance page yourself and select one of the two options. The repair &/or optimise scripts will then be run on the database.

    Once complete, you should remove the code from wp-config or set the value to false.

  16. Nice collection of functions.php hacks. I would like to to know that how to add nofollow attribute in comment links.

  17. Awesome, been looking to limit revisions, thanks for the code.

  18. very nice collection…

  19. Useful post as this has given me a few options to prevent my site from comment spam.

  20. A nice collection of snippets, helped a lot and made my work easy.

  21. i like database optimizing code… woww

  22. That’s really helpful, let me try to use of the codes on my blog.

  23. Great post with some really helpful tips here. I especially like the required minimum length tip. This will help cut down on those 2 word or so replies that are usually nothing but spam.

    Several other good ones, but I really like that one and think that tip will help a lot of people with their spam problems.

    Great post, thank for sharing these good tips.

  24. very useful snippet codes

  25. Great snippets! Thanks!

  26. This codes are really useful… I shall use them on my new worpress site..
    Thnx for writing

  27. Great list of useful snippets. I`ve added one to the code snippet database at www.wpcustoms.net

  28. what kind of snippets there are for Defer parsing of JavaScript?
    and where shoild i put theme?
    im usuing wordpress 4.4.2

  29. Thank for this post, Does anyone encountered the issue that it is not possible add snippets of more than about 100 line of code?
    best regards

  30. Thanks for this code to protection spam comment

  31. Hey Update the Exclude Pages from WordPress Search Results code
    bcz it exclude the posts not pages
    replace line 3 in that code add this query->set(‘post_type’, ‘post’);

    correct code

    function filter_search($query) {
    if ($query->is_search) {
    $query->set(‘post_type’, ‘post’);
    return $query;
    add_filter(‘pre_get_posts’, ‘filter_search’);

    • Thanks, Santhosh. I´ve updated it.

  32. Nice post.. great snippets.. thanks.. 🙂

  33. Thanks for this post!
    Great collection of functions to improve a wordpress blog!

  34. Very useful article. Thanks for sharing.

  35. This is just what i needed. Good stuff here.

  36. Thank for this post, Does anyone encountered the issue that it is not possible add snippets of more than about 100 line of code?
    best regards

  37. Thanks a lot . A very important guide for bloggers like myself who work with wordpress themes

  38. Awesome article! Absolutely love #7, will be a huge help for me!

  39. these are some of the very helpful codes for wordpress, i specially liked changing logo for the login wordpress. it makes site look premium.

  40. I removed autolinks From Comments Section Thanks You

  41. These are very important codes for wordpress developers who work with wordpress themes and development.

    Thankd a lot..!!!

  42. Nice collection of code mate. Actually never tried coding, as it seems harder to me. I think I should also start learning about code.

  43. Nice Collection of wp function code. Thanks for share.

  44. Thank you, i will implement the code to my site.

  45. Thanks for this useful wordpress custom codes.
    I think every one need those codes.


  1. Useful Code Snippets for WordPress  – Ventisqueros Digitales - […] Origen: Useful Code Snippets for WordPress – WordPress Theme Detector […]
  2. Understanding and Using the WordPress Hook System | Tech Pipeline - […] Useful code snippets for WordPress from WP Theme Detector – quite a few of these are actually making use of…
  3. Understanding and Using the WordPress Hook System | SeoRocketBar Network - […] Useful code snippets for WordPress from WP Theme Detector – quite a few of these are actually making use of…
  4. ENTENDENDO E USANDO O SISTEMA DE GANCHO DO WORDPRESS - TEMAS WORDPRESS E TUTORIAIS - […] Snippets de código úteis para o WordPress  do WP Theme Detector – bastantes destes estão realmente fazendo uso de…
  5. Understanding and Using the WordPress Hook System - WPMU DEV - […] Useful code snippets for WordPress from WP Theme Detector – quite a few of these are actually making use of…

Submit a Comment

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