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:

Twitter Linking

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:

User Registration

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:

Limit Post Revisions

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:

Search in navigation

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:

Copyright date change

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:

Remove the URL field in comments

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:

Change the number of results on the search results page

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]