WordPress Snippets Post Thumbnail

7 WordPress Snippets to Enhance Your Themes

Whether it’s in-depth functionality or a bigger thumbnail size, WordPress has you covered. It’s such a flexible CMS that the limits almost feel endless. I’ve searched through a large handful of my past and current projects, as well as looking through sites like Stack Overflow and more to find 7 of the best WordPress snippets for you to use on your WordPress themes.

Sometimes code snippets aren’t enough to add that extra bit of functionality that you’re after. In those cases I advise looking at some helpful plugins as they tend to be more thoroughly tested by the developer and the community.

How to use: 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.


1 Add Custom Links to Profile Page

On the profile editor screen of WordPress you have options for your email address and website, but what if you want to add links to your YouTube, Instagram, Dribbble or other accounts? With this WordPress snippet you can add as many as you want!

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. With this WordPress snippet you can have your custom post type show up in the main loop with everything else.

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 […]. 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 you’re running a blog and you want people to have the ability of searching only the posts on your site, not pages as well. Here’s a WordPress snippet that limits search to ONLY posts:

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, which this site was made for, you’ll want some shiny new HTML5 outputted from WordPress by default. Here’s 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 a community site where people can sign up for an account and you’re running a plugin like Restrict Content Pro, when a new user registers they automatically get the WordPress toolbar at the top of your site. This can be annoying because it makes your site appear less professional if users can see the (albeit limited) WordPress backend. With this WordPress snippet you can remove the default toolbar unless you have an account as 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

With this WordPress snippet, when people register on your site you can specify exactly where they’re taken, perhaps a custom designed account page or an exclusive freebie for signing up. Here’s how to do just that:

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

add_filter('registration_redirect', 'registration_redirect');

Follow IP on Twitter Like IP on Facebook

Advertisement