5 Simple Code Snippets To Help You Do More With WordPress

If you have been using WordPress for quite a while, you might already be aware of the importance of code snippets. In fact, code snippets are not exclusive to WordPress alone – if you are using any other Content Management System, such as Drupal or Joomla!, code snippets are pretty useful most of the time.

Basically, you can do a lot by means of plugins. However, what if the action you intend to perform is pretty basic, you don’t really want to add an extra plugin for that, right? Say, you wish to prevent a given post category from appearing on the homepage? Sure, you can install a plugin for this purpose, but by relying on a code snippet, all you have to do is paste few lines of code and get the job done. This way won’t overload your site by using too many plugins.

There are numerous code snippets out there that can help you do a lot with WordPress. You can tweak the admin panel, modify appearance of posts, content layout, as well as tweak the sidebar widgets.

In this post, I have put together some handy code snippets that you can make use of.

5 Simple Code Snippets To Help You Do More With WordPress

1. Remove Width and Height Attributes from WordPress Image Uploader

By default, when you upload images using the WordPress image uploader and add them to your posts, WordPress adds width and height attributes to the <img> tag within the content. This is perfectly alright, but in some cases, especially when working with responsive themes, these attributes might pose a problem.

To fix this, you can make use of the following code snippet, which will ensure that height and width attributes are not added to the <img> tag in posts:

add_filter( 'post_thumbnail_html', 'remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_attribute', 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}

2. Apply Custom CSS to Admin Panel

The WordPress admin panel is pretty usable and straightforward. However, just in case you wish to tweak its appearance by means of custom CSS — say, modify the fonts or font size — you can use the following code snippet:

add_action('admin_head', 'my_custom_fonts');
function my_custom_fonts() {
echo '<style>
body, td, textarea, input, select {
font-family: "Lucida Grande";
font-size: 12px;
}
</style>';
}

You can edit the CSS styles in the above snippet as per your needs.

3. Promote RSS Feeds on All Posts

Getting new subscribers is tough, especially if your RSS feed itself is hidden in some obsolete corner of your site.

If you wish to display your RSS feed at the bottom of every post, you can use the following code snippet:

function wps_promote_feed($content,$class = "promote") {
echo $content;
if (is_single()) {
?>
<div class="<?php echo $class; ?>">
<h3>Enjoyed this article?</h3>
<p>Please consider subscribing to our <a class="feed" href="<?php bloginfo('rss2_url'); ?>" title="Subscribe via RSS">RSS feed!</a></p>
</div>
<?php
}
}
add_filter('the_content','wps_promote_feed');

4. Generate QR Code for Your Posts

Quick Response Code, or QR Code as it is commonly called, has had the world divided for quite a while now. Some people consider it to be dead, and of little use; whereas others are fond QR Codes and the ease of use that they bring along.

If you wish to add a QR Code to your WordPress posts, you can make use of the following code snippet:

<img src="http://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php the_permalink(); ?>" alt="QR:  <?php the_title(); ?>"/>

Note that you can edit the 100×100 size of the QR Code image as per your needs.

5. Automatically Add Links to Twitter Usernames

Adding links to Twitter usernames is an easy way to promote brands as well as gain followers. You can use the following code snippet to automatically add a link to Twitter usernames as and when they are mentioned in your content:

function content_twitter_mention($content) {
return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>", $content);
}
add_filter('the_content', 'content_twitter_mention');
add_filter('comment_text', 'content_twitter_mention');

The above code scans the contents of your posts for a single word after @ sans space, and if found, it links out to the concerned Twitter username (such as, @example will be linked to the Twitter username ‘example’).

In Conclusion

Code snippets can be used to do a lot more with WordPress and accomplish tasks and functions with ease.

One practical problem might arise, however, if you use too many code snippets. Since you will be editing your theme files to place the snippets, if you ever update your WordPress theme, you might end up losing all your code snippets — as such, you will be required to take proper backups of the concerned files, and then restore the snippets.

Code Snippets plugin

However, you can fix this with the help of the Code Snippets plugin. Basically, this plugin provides you an interface to add snippets to your theme’s functions.php file with ease, as if you were activating or deactivating plugins. The plugin works only with functions.php, and for any other snippet placements, you will have to resort to the traditional file editing method. However, most of the code snippets tend to work via the functions.php file, so this will not be too much of a problem.

Got any code snippets of your own? Share them in the comments below!

About the Author

Rachel Heslop

Hi I'm Rachel, content producer for Tap. Lover of travel, photography and a real foodie. I enjoy writing blogs so much that I also do it in my spare time!

Comments