7 WordPress Snippets to Enhance Your Themes

Whether it’s more functionality or a bigger thumbnail size, WordPress has you covered. It’s such a flexible CMS where the limits feel almost endless. I’ve searched through a large handful of past projects, as well as looking through sites like Stack Overflow to find some of the handiest WordPress snippets.

Sometimes small bits of code aren’t enough. In some cases I advise looking at some plugins as they tend to be more thoroughly tested.

To use the following WordPress snippets, just copy and paste them into your theme’s functions.php file.

What handy WordPress snippets are you using? Let me know in the comments.

You might also likeWordPress Custom Post Types and Taxonomies for Beginners.


1.) Add Custom Links to Profile Page

In the WordPress profile editor, you have options for email and URL, but what if you want links to your other social accounts? With this WordPress snippet you can add as many as you want, as a result pushing more people to your social profiles.

function add_to_author_profile($options) {
	$options['youtube_url'] = 'YouTube';
	$options['dribbble_url'] = 'Dribbble';
	$options['instagram_url'] = 'Instagram';

	return $options;
}

add_filter('user_contactmethods', 'add_to_author_profile', 10, 1);

To display your content you need to use the_author_meta(); template tag, like so:

<?php the_author_meta('youtube_url'); ?>

2.) Add Custom Post Types to Main Loop

When you create a custom post type it doesn’t show up in the main WordPress loop by default. By using this WordPress snippet you can it show in the main loop, therefore exposing it to more people on your site.

function add_to_main_loop($query) {
	if ($query->is_main_query()) {
		$query->set('post_type', array('post','POST_TYPE'));
	}
}

add_action('pre_get_posts', 'add_to_main_loop');

3.) Change Excerpt Length and ‘More’ Text

Nobody wants an excerpt that’s too short, too long or ends with […]. Therefore below are two quick WordPress snippets to change the length and remove the trailing […]:

//----- Change Length
function modify_excerpt_length($length) {
	return 30;
}

add_filter('excerpt_length', 'modify_excerpt_length', 999);

//----- Change '[...]' Text
function modify_excerpt_more($more) {
	return '...';
}

add_filter('excerpt_more', 'modify_excerpt_more');

4.) Limit Search to ONLY Posts

It’s frustrating when running a blog and you want the ability to search posts, not pages as well. Here’s a WordPress snippet that does just that:

function modify_search_filter($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
	}

	return $query;
}

add_filter('pre_get_posts','modify_search_filter');

5.) Enable HTML5 Output

If you’re a modern day web design or developer, and I hope you are, you’ll want some fancy HTML5 output from WordPress by default. Below is a WordPress snippet to do just that:

add_theme_support('html5',
	array(
		'comment-list',
		'comment-form',
		'search-form',
		'gallery',
		'caption'
	)
);

6.) Remove Toolbar Unless Admin

If you run an account based site with a plugin like Restrict Content Pro, all users get the WordPress toolbar at the top of your site. This can be a pain as it makes your site look less custom. By using the below snippet, you can remove the toolbar unless you’re an admin:

function remove_admin_bar() {
	if (!current_user_can('administrator') && !is_admin()) {
		show_admin_bar(false);
	}
}

add_action('after_setup_theme', 'remove_admin_bar');

7.) Redirect After Registration

When people register on your site you can specify where they’re taken. Maybe it’s a custom account page or a reward for signing up. Here’s how to do that:

function registration_redirect(){
	return home_url('/redirected page');
}

add_filter('registration_redirect', 'registration_redirect');

Comments

Leave a Comment

Comments (3)

Julia

December 18, 2017 at 12:56 am

That’s a wounderful tutorial!!! Thank you very much! I got a couple of techniques for myself! Thanks again

Reply

Bhumi

February 4, 2015 at 9:50 am

Each and every code need to add into theme’s functions.php file?

Reply

Seb Kay (Author)

February 4, 2015 at 9:53 am

Yes that’s right, just copy and paste into your themes functions.php file. I’ve updated the article to explain this a bit better 🙂

Reply

Inspirational Newsletter


Join the newsletter to get the best articles, tutorials and exclusive freebies every two weeks.

No spam. You can unsubscribe at any time.