Code snippets can be used in different ways. Most of the time this involves going to the functions.php file, but you might want to think about that because this file is theme specific. If you decide to change your WordPress theme later and have non-theme specific functions in this file, you will have to start from scratch.
Another major warning is that if you want to make changes to the parent theme and then update it, all of the changes you make in the functions.php file will be overwritten and you will have to make them again. You want to make sure you’re using a child theme, but there are some ways to work around the problem:
1. You can create a custom plugin if you’re well-versed in such things.
2. Use Code Snippets, which is a snippet plugin that enables you to add code snippets to your site without you manually adding them to functions.php
3. Use a plugin that is specific to your theme, hence, take the path of the least resistance and hunt for compatible plugins.
No matter what you do, always back up your WordPress site before you make changes. You also want to have an original copy of your functions.php file saved in case something goes wrong and you need to restore the original.
With that said, it’s time to get to the good stuff.
Useful WordPress Code Snippets
The following is a collection of code snippets that you will find useful.
1. Highlight search terms
You can boost your search functionality by highlighting the keywords, or search terms, in the search engine results. All you have to do is open search.php, look for the_title() function and replace it with echo $title .
2. Shorten blog excerpts
If you think the excerpt to a blog post is too long, you can shorten it to 20 words. That could clean up the look of your blog post page a little.
3. Automatically link to Twitter usernames
This snippet will keep an eye on your posts for when you use a Twitter username, automatically creating the link for you.
4. New user registers and you want to send them to a certain page
If you require user registration on your WordPress site, you may need the new user to be redirected to a certain page after their registration is complete. That code snippet looks a little something as below.
5. Add a custom background
When you want to add a custom background, just add code snippet.
6. Keep clients from updating on their own
One of the things that some web designers market about their services is that clients can update their WordPress site on their own. However, there are times when this may not be the best way for the site to be managed.
7. Add a Shortcode to Widgets
Sometimes all you want to do is add a shortcode to a widget, but you just can’t. The good news is that the below WordPress code snippet will allow you to add a shortcode to a text widget.
8. Limit the number of times a post can be revised
This has more to do with how many revisions are stored in the database. If you do a lot of editing, then this can keep you from having dozens of revisions stored.The code snippet looks like this:
9. Make navigation easier with a search box in the nav menu
If you want to make navigation even easier by placing an obvious search box directly in the navigation menu.
10. Add your Google Analytics tracking code
When updating a parent theme, one of the last things anyone thinks of is re-adding their Google Analytics tracking code. You can add it to your functions with code snippet.
11.Make the copyright date in the footer change automatically
It can be easy to forget about the copyright in the footer. You don’t want it to be 2020 with the copyright year 2016 glaring in your footer. That makes it look to visitors like your website isn’t being regularly updated.
12. Remove the URL field in comments
If your blog is always getting hit by spammers sharing their URLs in your comment section, you can remove the URL field by adding this code snippet.
13. Change the number of results on the search results page
If you only want a certain number of search results to show up on the search results page,here is the code snippet that will let you do that:
14. Easily turn on maintenance mode
If you need to go into temporary maintenance mode and close the site to visitors in the meantime, you can use the code snippet.
15. Exclude certain pages from WordPress search results
When a user performs a search query, there may be pages that you don’t want showing in the search results, such as confirmation pages. You can exclude those pages by using code snippet.