Useful WordPress Snippets 5
I love WordPress snippets, and you’ll love them too when you need to add a killer function to your site. Below, I’ve compiled a list of super-useful WordPress snippets to use in your next WordPress site. Add these to your WordPress site in seconds to save you time customize your site just how you want it.
Disable WordPress Plugin Updates
When you have a client who likes to dabble with their own website, this snippet will keep them from updating plugins and messing things up. This is especially true if you’ve modified or customized a plugin to add custom functionality to their site. Use the snippet below to halt plugin updates.
<?php
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );
?>
Hide Custom Post Type From Search
Just because you create a custom post Type doesn’t mean that you want to include it when someone searches your site. Use the snippet below to exclude custom post types from search results.
<?php
add_action('init', 'codex_custom_init');
function codex_custom_init()
{
$args = array(
'exclude_from_search' => false, // the important line here!
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
register_post_type('book',$args);
}
?>
Show Random Posts
This is super handy when you want to breathe some new life (traffic) into old posts. Having a section on your site that brings up random older content is a great way to get people to see older posts.
<ul> <li><h2>A random selection of my writing</h2> <ul> <?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </li> </ul>
Multiple Custom Menus & Place Them Wherever You Want in your Theme
This snippet lets you create as many menus as you want. You can place them wherever you want within your theme. The snippet below goes in your functions.php file.
<?php
add_action( 'init', 'my_custom_menus' );
function my_custom_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' )
)
);
}
?>
Place the following snippets anywhere within your theme.
<?php wp_nav_menu( array('theme_location' => 'primary-menu' ) ); ?>
<?php wp_nav_menu( array( 'theme_location' => 'secondary-menu' ) ); ?>
Filter Your Posts
You can use the following snippet to only show certain posts. Let’s say you had category called featured. If you wanted to exclude all other posts, you can filter them out and focus on one category with the snippet below.
<?php query_posts('showposts=5&category_name=featured'); if ( have_posts() ) : while ( have_posts() ) : the_post(); <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_content(); ?> <?php endwhile; else: // No posts. endif; wp_reset_query(); ?>
Conclusion
Which of these snippets is your favorite? I’d say mine would be a tie between filtering your posts and creating multiple menus. Do you have a snippet you’d like to share? Feel free to leave your thoughts, questions or snippets in the comments section below.