Diving Into WordPress Code Snippets: A Guide for Beginners

No Comments

Many WordPress users like the platform because of its power, flexibility, relative ease of use, and large array of both free and premium plugins and themes. In short, they like it because its empowering. Increasingly so for non-coders who are able to take advantage of the WordPress ecosphere’s increasing array of turn-key solutions for complex development needs.

You can do just about anything with WordPress these days given the right theme and plugins. But that doesn’t mean theme or plugin functionality is always the best option. That’s where code snippets come in. And I promise, they’re not as intimidating as they may sound to those who don’t like to mess with code (or who can’t code at all).

Learning how to use code snippets the way I’m going to show you in this post is basically the most powerful use of copy and past within WordPress you will ever encounter. Which also means its easy enough for anyone who can use a basic word processor. So let’s get into it!

What Are WordPress Code Snippets?

A code snippet is a small bit of PHP code that you can use to add new or extend existing functionality on your WordPress website.

Why Code Snippets Instead of Plugins or Theme Functions?

As a rule, WordPress functionality should be plugin based–not theme based. This is essential to successfully future proofing your WordPress website. It allows you to retain site functionality while making it possible for you to switch themes. Which most people want to do every few years or so, if not more often.

So that explains why code snippets are a better alternative to built in theme functionality, but why use code snippets instead of plugins?

Essentially, a code snippet is a sort of mini-plugin; except it is only the function you want and absolutely nothing else. It’s much leaner and requires less load than most plugins. This will not only help you optimize your site’s performance, but it will also keep your WP Admin clear of unnecessary sub-menus, option panels, etc.

There is not always going to be a good code snippet solution in place of a needed plugin. But when there is, it’s a great way to keep things simple, lean, and fast.

Using and Managing WordPress Code Snippets

Many WordPress code snippet resources will recommend that you add a new code snippet directly within your active theme’s functions.php file. However, doing this often will result in a long and possibly unorganized file that is hard to navigate and edit when needed.

Instead, I’ve taken to using the Code Snippets plugin. A bit ironic I know, using a plugin to implement code snippets meant to allow you to use less plugins, but it really does make the whole process better.

Code Snippets Plugin

WordPress-Code-Snippets-Plugin

Code Snippets simplifies the process of adding and organizing code snippets on your WordPress website. It does this by giving you an interface a lot like the WordPress post or page archive where you can add a new snippet, edit existing snippets, delete snippets, or search for snippets by name/filter.

Download | More Details

Using the Code Snippets Plugin to Implement & Organize WordPress Code Snippets

Once you find a code snippet that you would like to use on your WordPress website (which we’ll get into in the sections below) you’ll want to make sure you have somewhere to put it! So first things first, install the Code Snippets plugin.

When you do, you’ll notice that you have a new menu in WP Admin called Snippets. There are two options: Manage or Add New. When you click Add New you will see a screen like the one below.

new_snippet

As you can see, there is a space for you to name your code snippet, paste (or write) the actual snippet, and then describe what it is for below. When finished all you have to do is save/activate and you’ve successfully added a new function!

Additionally, when you now click Manage (under the Snippets menu) you will notice that you’ve started a code snippet archive that is very much like your post or page archives. It’s incredibly easy to sort and search for exactly what you’re looking for. All without having to use FTP or access your functions.php file directly!

Some Useful WordPress Code Snippets to Get You Started

Now that you know how to add code snippets to your site, lets begin by replacing some common WordPress plugins with simpler, leaner bits of code.

Automatically email contributors when their posts are published

There are some great editorial plugins out there, both free and premium, but if all you want to do is notify your contributors that their post has been published then this is the code snippet for you.

function wpr_authorNotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "
      Hi ".$author->display_name.",
      Your post, ".$post->post_title." has just been published. Well done!
   ";
   wp_mail($author->user_email, "Your article is online", $message);
}
add_action('publish_post', 'wpr_authorNotification');

Source

Go to Maintenance Mode

There are a ton of maintenance mode plugins out there. They can really add a lot of useful design/functionality to a website you’re hoping to launch. But if that’s not what you’re up to and you just want to make sure that non-admins can’t see the website for a bit, this code snippet is what you need.

function maintenace_mode() {
 if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
 die('Maintenance.');
 }
 }
 add_action('get_header', 'maintenace_mode');

Source

Delete Default Widgets

Ever go to your widgets page in WP Admin and feel like you’re overwhelmed with widget options you never plan to use? Why not get rid of them all with this handy code snippet?

// unregister all widgets
 function unregister_default_widgets() {
     unregister_widget('WP_Widget_Pages');
     unregister_widget('WP_Widget_Calendar');
     unregister_widget('WP_Widget_Archives');
     unregister_widget('WP_Widget_Links');
     unregister_widget('WP_Widget_Meta');
     unregister_widget('WP_Widget_Search');
     unregister_widget('WP_Widget_Text');
     unregister_widget('WP_Widget_Categories');
     unregister_widget('WP_Widget_Recent_Posts');
     unregister_widget('WP_Widget_Recent_Comments');
     unregister_widget('WP_Widget_RSS');
     unregister_widget('WP_Widget_Tag_Cloud');
     unregister_widget('WP_Nav_Menu_Widget');
     unregister_widget('Twenty_Eleven_Ephemera_Widget');
 }
 add_action('widgets_init', 'unregister_default_widgets', 11);

Source

Open all links in a new window

Personally, I hate it when I’m reading something and go to open a link I wanted to read later only to be taken out of the article I’m currently reading. If that bothers you too, make sure it won’t happen on your blog by using this code snippet.

function autoblank($text) {
	$return = str_replace('<a', '<a target="_blank"', $text);
	return $return;
}
add_filter('the_content', 'autoblank');

Source

Add a search box to your nav menu

Sometimes the best place for a search box is right in the nav menu. This is the code snippet that will make that happen for you.

add_filter('wp_nav_menu_items','add_search_box', 10, 2);
 function add_search_box($items, $args) {
 ob_start();
 get_search_form();
 $searchform = ob_get_contents();
 ob_end_clean();
 $items .= '<li>' . $searchform . '</li>';
 return $items;
 }

Source

Other WordPress Code Snippet Collections & Resources

Addicted yet? I know I am. If you’ve caught the bug too and need access to more snippets then the resources below should keep you busy for a while.

In Conclusion

As you can see, there is so much that you can accomplish with WordPress code snippets. And you don’t have to be a professional developer or even a coding hobbyist to take advantage of them!

What are you going to do with WordPress code snippets now? Or do you still have questions? Either way, we’d love to hear from you in the comments below.

Newsletter

We never spam. Subscribe to receive inspiration, news, and ideas in your inbox.

 

Nathan is a professional writer and digital publisher. He's been using and writing about WordPress since 2010 and enjoys supplying useful WordPress news, reviews, tips, tricks, and tutorials. You can connect with him at his personal website or on facebook and twitter.

 

Our Themes

Zap

Zap is a premium business theme with an amazing set of unique features like before/after slider, team profile, testimonials, and…
This is a premium WordPress blogging theme that is well suited for all kinds of blogs. Whether it is a travel…

Guide to Getting Started with WordPress

Twelve free easy-to-follow blog posts covering all the essentials every WordPress user needs to know.

Cohhe WordPress tracks

Get news by email

We never spam. Subscribe to receive inspiration, news, and ideas in your inbox.

More from our blog

Top 20+ WordPress Music Themes For Musicians

WordPress is the most popular online platform for building websites. It is…
Continue reading

20+ Best WordPress Directory Themes

Are you looking to create a directory for a car dealership, real…
Continue reading

WordPress Popup plugin

Popups is a great tool for marketers but you have to be…
Continue reading

Leave a Comment

No Comments