15 WordPress Code Snippets & Plugins
WordPress Code Snippets Plugins and syntax highlighted code
Little things can make a huge difference with your WordPress website, which is one of the things that makes WordPress so great. The high level of customization that allows you to meet your needs is practically limitless. This has much to do with the vast plugin library where you can find many WordPress code Snippets plugins that will make your website perform the way you need it to perform. It’s the path of least resistance.
But even a path of little resistance comes with its challenges. The main challenge is finding the plugin in the first place because there are so many that are designed to do some of the same things. Just because it is designed to do something doesn’t mean it will and it also needs to be compatible with your theme. you can go through how to create WordPress theme .
But what if you could use a simple code snippet to do what you need to do without sifting through plugins and testing each to see how they function with your site?
Sounds nice, doesn’t it?
“But I know nothing about coding,” you anxiously say.
That’s okay because you don’t need to have advanced coding knowledge to use the most useful WordPress code snippets for your WordPress site. All you need to know is what to use and where to use it. Here at Templatetoaster WordPress theme builder, let’s see how to use code snippets.
How to Use Code Snippets ?
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 [highlight] echo $title [/highlight].
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. Here is the code snippet:
[box title=””]
Function new_excerpt_length($trength) {return 20;}
add_filter(‘excerpt_length’,’new_excerpt_length’);
[/box]
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. Here is what it should look like:
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 like this:
5. Add a custom background
When you want to add a custom background, just add this code snippet:
[box title=””]
// Add support for backgrounds
add_custom_background();
[/box]
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. It is possible for you to remove the nag code by using this piece of code:
[box title=””]
Add_action(‘admin_menu’,’wphidenag’);
function wphidenag(){
(remove_action(‘admin_notices’,’update_nag’,3 );}
[/box]
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:
[box title=””]
1 | add_filter( ‘widget_text’, ‘do_shortcode’ );
[/box]
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, insert this:
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 this code snippet:
[box title=””]
<?php add_action(‘wp_footer’,’ga’);function ga() { ?>// Place your Google Analytics code here <?php } ?>
[/box]
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. Enter this so you don’t have to worry about updating the copyright date ever again:
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 enter the below code:
[box title=””]
function wp_maintenance_mode(){
if(!current_user_can(‘edit_themes’) || !is_user_logged_in()){
wp_die(‘Maintenance, please come back soon.’, ‘Maintenance – please come back soon.’, array(‘response’ => ‘503’));
}
}
add_action(‘get_header’, ‘wp_maintenance_mode’);
[/box]
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 the following code:
[box title=””]
function filter_search($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘page’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘filter_search’);
[/box]
Which WordPress Code Snippets Plugins you use ?
Let us know in your comment. I will add WordPress code snippets my article if they worth visit. You can check out TemplateToaster to create your own website.
[call_to_action color=”gray” button_icon=”download” button_icon_position=”left” button_text=”Download Now” button_url=”https://templatetoaster.com/download” button_color=”violet”]
Design WordPress Themes/Templates with TemplateToaster offline website builder software
[/call_to_action]
Build a Stunning Website in Minutes with TemplateToaster Website Builder
Create Your Own Website Now
Wow ! You really have some great tips here. I am going to use some of these myself actually. Shorten blog excerpts, the New user re-direct is awesome, and #8 limiting the revisions in the database is really helpful. Thanks for a great post !
Great post and equally great snippets. As a supplement, developers might like to consider using a custom Clip as a destination for such code – “wpclips” – it essentially acts as a pre-prepared custom plugin of CSS, PHP and JS files. Available for multisite, too. And all plugins are free.
This is a very frustrating post, because the snippets seem like they would be very useful, but several things get in the way of us users actually being able to use them.
First, some background: I’ve been a computer programmer for over 30 years, and have been building websites for more than 25 years, so I am no novice at computers. If I am having trouble with your directions, it’s probably not because I am inept.
Next, please tell your web designer that whoever thinks grey font on a white background is “stylish” to learn to consider viewers who may not have perfect eyesight. Readability is always more important than being thought to be cool. Please reset your CSS to give more contrast,.
Next, in the top of your post, you say “All you need to know is what to use and where to use it,” then you do a great job of telling us all except the first two of those things. Slight attempt at humor there – the snippets were great, but frustrating because we cannot use them exactly as you give them.
To have made these snippets – and this entire post – fully usable, we would need a more complete explanation of the problem the snippet is solving.
For example, what on earth is a “nag code”? You should give us specific instances and show screen shots.
Another thing we would need would be specifics about how each snippet should be modified to be used in our own situations. For example, in the new user registration redirect section, on the second line, you have URL “/finished/” in the snippet, but there is no description in the text that this is the relative URL of the page to which to send the new user, and that we either need to have a page with that URL in our structure, or we need to modify the snippet to reflect the relative URL of the page to which to redirect the new user. As a computer programmer, I can figure this out, but as a professional educator, I can see many of your readers will be confused by this and not know what to do.
Those are some examples of how you could make the snippets explanations more useful.
The other thing I found confusing was you told us all we needed to know was “where to use” the snippets, and then gave us only some rather vague examples of where we COULD possibly put the snippets, but there were no specific examples of exactly how to do so. Yes, I recognize this might require an entirely separate post to show your readers how to copy or create a functions.php file in the child theme folder, or how to put the snippet into a newly-created plugin, but without that information ready to hand, all these snippets are essentially useless to the average WP website builder – the very kind of people who are Template Toaster customers.
I hope this feedback is helpful to your authors and your website designers; you have an excellent product and these blog posts are very thoughtful and could be wonderfully useful. I hope I have helped to make them more so.
Hi David,
If you know WordPress a bit, there is no problem to use them. Sure a WP novice wouldn’t understand how to use them, but that is maybe better so.
Some of the snippets are very common (you will find them on many sites), but others are great and remembered me that these options are available in WordPress.
For example:
#14 is really cool, because it’s much better to use that snippet instead of some code inside your .htaccess file. It’s a function which is used by WordPress during core updates.
@theAuthor, WTF is snippet #11 just use the PHP date() function 🙂
Anyway nice article
Hi, Olaf, and thanks so much for your comments.
However, my point was that WordPress DEVELOPERS are not the same people as WordPress GURUS. Developers are the people who build websites for clients, using WP, themes, plugins, and a bit of custom code. Gurus are the people who build plugins and themes and custom post types, etc. Each requires a different skillset. Bottom line: Developers are not (necessarily) gurus.
These tips (Snippets) are only intended for GURUS, the way they are presented.
However, they are presented on the TemplateToaster site, where presumably all the readers are TemplateToaster users, and these folks are mostly DEVELOPERS. Not Gurus.
My point was that the article should have been written with the principal readers in mind. Namely, TemplateToaster users, developers. Not Gurus.
If someone calls himself a developer and makes WordPress sites than a list of these code’s without explanation would be sufficient.
If a WordPress developer doesn’t know how to use these snippets that ‘developer’ has a few options:
1. use another job title (eg. WordPress installer, drag-n-drop-builder-user, Intermediate WordPress user),
2. Educate him/her self in a bit of programming and how to use WordPress,
3. Find another job
I don’t know if the post has been updated since your first comment but this how-to is very clear for somebody that made multiple sites who is doing customizations
> Code snippets can be used in different ways. Most of the time this involves going to the functions.php file
Hi David,
you’re welcome I understand your point, but you need to start somewhere if you write an article like this.
BTW your definition of a developer is called “Script kiddie” 😉