With over 10 years of existence, WordPress has received several applauds from web designers and developers residing in different parts of the world. Having used WordPress for around 2 years now, I too have become a huge fan of this brilliant CMS(Content Management System). Throughout my journey of exploring the features and utilities available with WordPress, I’ve come across some useful code snippets that can easily add convenience to every type of WordPress web development project. My main aim behind penning down this blog is to make you familiar with ten useful WordPress code snippets, which if added to your theme’s functions.php file can work as a great-add for adding/extending your site/blog’s functionality.
-
Add file size flexibility for thumbnails
Each time you hire wordpress expert, there are ample number of things jotted down in your requirement list. A common one is definitely the ease of setting different sizes for thumbnails. Well, there are situations when you need to display the image thumbnails in different sizes on different pages included within your WordPress site/blog. For this, you just need to add the below code snippet into your theme’s functions.php file:
// Thumbnail Size Flexibility
if ( function_exists( ‘add_theme_support’ ) ) {
add_theme_support( ‘post-thumbnails’ );
set_post_thumbnail_size( 250, 250 ); // default Post Thumbnail dimensions
}
if ( function_exists( ‘add_image_size’ ) ) {
add_image_size( ‘small-thumb’, 163, 121, true ); //Small Thumbnail
add_image_size( ‘header-image’, 1000, 440, true ); //Header Image
}
-
Add Post Formats and create custom templates
If you’re running a WordPress powered blog then it becomes necessary for you to include post formats in the same. These post formats allow them to switch between multiple formats for posts/articles in addition to displaying content differently, in accordance to the chosen format. For this, you need to register the post formats by simply pasting the below code snippet in your functions.php file:
// Add New Post Formats
add_theme_support( ‘post-formats’, array( ‘gallery’, ‘image’, ‘video’, ‘audio’ ) );
Now, you need to add a code snippet in your single.php file for creating custom templates based on the post format that has been chosen by the user. That means, the single.php file will now search for template file that begin with content and end with the post format. For example, the video format would be content-gallery.php.
The code snippet that you need to add to the single.php file is:
<?php get_template_part( ‘content’, get_post_format() ); ?>;
-
Delete unnecessary default widgets
WordPress comes loaded with numerous widgets which serve as handy tools for performing multiple tasks conveniently. Still, many a times, these widgets just seem to be an unnecessary add-on for your WordPress site/blog, making it slow and inefficient. Thus, if you want to rid of a single/multiple WordPress default widgets like: Calendar, RSS etc, simply include the below code snippet in your theme’s functions.php file:
// unregister all widgets
function unregister_default_widgets() {
unregister_widget(‘WP_Widget_Pages’);
unregister_widget(‘WP_Widget_Calendar’);
unregister_widget(‘WP_Widget_Archives’);
unregister_widget(‘WP_Widget_Links’);
unregister_widget(‘WP_Widget_Meta’);
unregister_widget(‘WP_Widget_Search’);
unregister_widget(‘WP_Widget_Text’);
unregister_widget(‘WP_Widget_Categories’);
unregister_widget(‘WP_Widget_Recent_Posts’);
unregister_widget(‘WP_Widget_Recent_Comments’);
unregister_widget(‘WP_Widget_RSS’);
unregister_widget(‘WP_Widget_Tag_Cloud’);
unregister_widget(‘WP_Nav_Menu_Widget’);
unregister_widget(‘Twenty_Eleven_Ephemera_Widget’);
}
add_action(‘widgets_init’, ‘unregister_default_widgets’, 11);
As is suggested from the above code, if you’re inclined on keeping any individual widget, just delete the line of code that’s related to it.
-
Place a custom WordPress query on web page
If you’re a WordPress theme developer and are supposed to design a template with multiple queries running on it, then using the below snippet of code will serve as a handy option:
<?php $query = new WP_Query(”); ?>
<?php if($query->have_posts()) : ?><?php while($query->have_posts()) : $query->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
The above code will allow you to query featured post for rendering an image slideshow, displaying a list of news-related posts on a page and a lot more.
-
Add a Custom Failed Login Message
As a WordPress developer, you might be familiar with the fact that if a WP site/blog recognizes a username but not the password, it will inform the user that his/her is password is incorrect. This makes the site/open vulnerable to attack of hackers who now know the username and can attempt to crack the password by entering millions of variations. It is here that the below code snippet comes to your help. Just add it in your functions.php file and you can customize the failed login message that is displayed in case a user enters a correct username but an incorrect password:
// Change the failed login message for extra WordPress Security
function failed_login() {
return ‘Incorrect login information.’;
}
add_filter(‘login_errors’, ‘failed_login’);
-
Hide your current/working WordPress version
Hackers find it convenient to gain access to a WordPress website which is running on an outdated version. Therefore, in order to refrain people from finding out the version you’re using for your site, simply hide it by include the below code snippet in your functions.php file:
<?php // Remove the WP version for extra WordPress Security
function remove_wp_version(){
return ”;
}
add_filter(‘the_generator’, ‘remove_wp_version’);
?>
-
Gain access to URL for the current theme directory
Custom-made for WordPress theme developers, below is the code snippet which returns an absolute URL to the current theme directory. Therefore, you can embed images, call scripts etc; without the need for typing a relative URL for the active theme directory.
<?php bloginfo(‘template_directory’); ?>
-
Open all links in new windows
If you want all links for your WordPress site to open in separate windows, then just use the below mentioned simple code snippet:
function autoblank($text) {
$return = str_replace(‘<a’,
-
Automate email firing for contributors whose posts get published on your WordPress blog
If you’re looking for a way to automate the process of sending emails to contributors when their posts are published on your WP Blog, then using the below code snippet will work for you:
function wpr_authorNotification($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);
$message = ”
Hi “.$author->display_name.”,
Your post, “.$post->post_title.” has just been published. Well done!
“;
wp_mail($author->user_email, “Your article is online”, $message);
}
add_action(‘publish_post’, ‘wpr_authorNotification’);
-
Include Anti-spam email links in your posts
Mailto links are undoubtedly a soft target for hackers. You can opt for including an anti-spam email link in your post to protect your mailto link from the attack of bots. Just add the below code snippet where you want the anti-spam link to appear in your post:
<? php
// Anti Spam Mailto Links
function email_encode_function($atts, $content){
return ‘<a href=”’.antispambot($content).’”>’.antispambot($content).'</a>’;
}
add_shortcode(‘email’, ‘email_encode_function’);
// Use shortcode [email]email@me.com[/email]
?>
Final Thoughts
Now that you know about some of the most useful WordPress code snippets, I’m sure you wouldn’t fall prey to flooding your WP site/blog with plugins and extensions. Do share your experiences of using these code snippets in the comments box below.
About the Author-
As a certified top-notch developer at OSSMedia Ltd, Edward Jones is working as a programmer with OSSmedia – WordPress Customization Service. If you need to hire wordpress developer then simply get in touch with him via his Twitter and Google+ handle. Having gathered a total of 5 years of experience in WordPress Development, he has delivered numerous projects within the allotted time-frame.
One Comment
Pingback:
January 30, 2015 at 6:22 am