Handy WordPress Snippets

Handy WordPress Snippets

It’s time again for another round of WordPress snippets. These handy WordPress snippets add or take away functionality, and makes your life so much easier. Speed up your site, get rid of admin bars, add useful things like author boxes, and more with these super-handy WordPress snippets.

Gzip Compression via .htaccess

Gzip compression is highly sought after, because of the fact that it reduces http response times. This code snippet will make sure your site is using Gzip image compression.

# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

Display an Author Box at the End of Posts

When you run a blog with more than one author, it’s likely that you’ll want to display an author box for them that includes a bio, avatar, and and links to their social media account. If your WordPress theme doesn’t come default with an author box, use the following snippet in your single.php template to create an author box. As long as they’ve filled out there user profile in WordPress, their bio and links will show up. There’s no styling with this, but if you style the classes laid out in the example, you can highly customize the bio section and make it stand out.

<div>
<div>
<div><?php echo get_avatar( get_the_author_email(), '80' ); ?></div>
</div>
<div>
<div><?php the_author_meta( "display_name" ); ?></div>
<div><?php the_author_meta( "user_description" ); ?></div>
<div><?php if (get_the_author_url()) { ?><a href="<?php the_author_url(); ?>">Visit <?php the_author(); ?>'s website</a><?php } else { } ?></div>
</div>
<div></div>
</div>

Specify a Custom Excerpt Length

Sometimes the preset excerpt length just isn’t enough. I feel that the normal excerpt length doesn’t draw readers into reading more, because it cuts it too short. With the snippet below, you can set your own excerpt length, which will allow you to display more words and draw your readers in.

// custom excerpt length
function custom_excerpt_length($length) {
	return 150;
}
add_filter('excerpt_length', 'custom_excerpt_length');

Disable Comment Links

This one I picked up from WP Recipes and CatsWhoCode and I find it extremely useful. It disables spammers’ abilities to add live links in the comments section. I love this little snippet, because it keeps your comments section clean.

remove_filter('comment_text', 'make_clickable', 9);

Open files in Google Docs With PDF Viewer Shortcode

This is another gem from CatsWhoCode, and when I found it I was ecstatic. Instead of readers being forced to download a pdf, this shortcode will open a PDF, doc or xls in Google Docs. Many users will find this incredibly handy.

function pdflink($attr, $content) {
	return '<a href="http://docs.google.com/viewer?url=' . $attr['href'] . '">'.$content.'</a>';
}
add_shortcode('pdf', 'pdflink');

Then, use the shortcode in your site. This will open all three document types in Google Docs.

[pdf href="http://yoursite.com/linktoyour/file.pdf"]View PDF[/pdf]

Disable The WordPress Admin Bar

I don’t know about you, but I’ve hated that stupid admin bar ever since they brought it in. I think it’s obtrusive, ugly, and it just gets in the way. The following WordPress snippet will remove the admin bar.

remove_action('init', 'wp_admin_bar_init');

That’s it for this week, but I think you’ll get some good use out of these snippets. Refining the user experience and being able to highly customize your site is one of the reasons why people love WordPress so much. Where else can you add or take away a feature of your site with just a simple line of code? Which of these WordPress snippets are your favorite? Leave your thoughts or questions in the comments section below.