10+ Hacks to add functionality to your WordPress Theme
22/08/2012 | 3 Comments
WordPress comes with standard functions but there are many plugins and code snippets/hacks to give your blog a different look and/or function(s). I have collected a couple of very useful code snippets, .. all you’ll have to do is to ‘copy & paste’ the code when a specific functionality is needed.
![]()
Despite of some brilliant WordPress plugins you really should use, there are situations when plugins could become an unnecessary problem, causing longer loadtime, including non-validated codes, not working scripts, etc.. The point is any plugin can crash your WordPress website functionality in some cases, or even worse, an (not updated) plugin could be a possible backdoor for hackers to start an attack, and infect your database with some kind of script or virus.
With this in mind I have prepared for you some interesting WordPress snippets/hacks, most of these hacks can be pasted into your functions.php, this file is one of the most useful and important files in your theme. But before I begin, as always, a word of warning ..
Note: These hacks worked at the time they were published, but as new versions of WordPress are released, some may no longer work. Please backup your theme before attempting any hacks so you can restore things if something goes wrong.
There are a lot more WordPress hacks and/or code snippets here in the gonzoblog, take a look in our tag-list WordPress Hacks. In this article I’ld like to show you 10+ new and useful code snippets/hacks that will help you extend the capabilities of your WordPress site, and of course save you time.
So let’s quit the chit-chat and let’s get rollin’ ..
Create a dropdown menu of tags
In order to display your categories in a dropdown menu, WordPress provide the wp_dropdown_categories() function. But if you want to display your tags in a dropdown as well, there’s no built in function. Let’s use wp_dropdown_categories() and modify it in order to be able to display tags in a dropdown menu.
<h2><?php _e('Posts by Tags'); ?></h2>
<form action="<?php bloginfo('url'); ?>/" method="get">
<div>
<?php
$select = wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
echo $select;
?>
<noscript><div><input type="submit" value="View" /></div></noscript>
</div></form>
Replace the text for “Featured image”
Use this code to provide yourself and your authors a constant reminder of your ideal Featured Image size:
add_filter( 'gettext', 'change_featuredimage_txt' );
add_filter( 'ngettext', 'change_featuredimage_txt' );
function change_featuredimage_txt( $translated ) {
$translated = str_ireplace( 'Featured image', 'Featured image (440w x 300h)', $translated );
return $translated;
}
Related Posts for Tags with Thumbnails
Displaying related posts is really a great way to help visitors stay longer on your blog. To show related posts with thumbnails, you can use a plugin but very diffecult in customizing your related posts style and that will make your blog has many plugins. So why don’t we try a functionality for displaying related posts with thumbnails?
First step, check your WordPress theme to ensure if your theme supports post thumbnail? Find this next line of code in your functions.php theme file:
add_theme_support('post-thumbnails');
If you can’t find this code add these following 2 lines of code in your functions.php (theme folder), or if you find the above line of code select the above code and change it into:
add_theme_support('post-thumbnails');
add_image_size( 'relatedpost-thumb', 199, 180, true );
You can tag each of your posts with multiple keywords. And related posts will show posts with the same post tags and if your post don’t have a featured image, we will use a default image to show it as the thumbnail. We will display 3 related posts to that, please add the following code in your single.php file:
<div class="related-posts">
<ul>
<?php if( has_tag() ) { ?>
<?php
//for use in the loop, list 3 post titles related to first tag on current post
$backup = $post; // backup the current object
$tags = wp_get_post_tags($post->ID);
$tagIDs = array();
if ($tags) {
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args=array('tag__in' => $tagIDs, 'post__not_in' => array($post->ID), 'showposts'=>3, 'ignore_sticky_posts'=>1);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><article>
<a href="<?php the_permalink();?>" title="<?php the_title();?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('relatedpost-thumb', array('class' => 'omc-image-resize'));
} else {
echo('<img src="'.get_template_directory_uri().'/images/no-image-half-landscape.png" alt="no image" />');
} ?>
</a>
<h5><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h5>
</article></li>
<?php endwhile; ?>
<?php } else { ?>
<h4><?php _e('No related posts found!', 'uxde'); ?></h4>
<?php }
}
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
?>
<?php } else { ?>
<?php
global $post;
$tmp_post = $post;
$args = array( 'numberposts' => 3 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><article>
<a href="<?php the_permalink();?>" title="<?php the_title();?>">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('relatedpost-thumb', array('class' => 'omc-image-resize'));
} else {
echo('<img src="'.get_template_directory_uri().'/images/no-image-half-landscape.png" alt="no image" />');
} ?>
</a>
<h5><a href="<?php the_permalink();?>" title="<?php the_title();?>"><?php the_title();?></a></h5>
</article></li>
<?php endforeach; ?>
<?php $post = $tmp_post; ?>
<?php } ?>
</ul>
</div>
You can easily style this related post hack with CSS, making this into a customized module for your blog. In the original article you’ll also find a prefab CSS makeup style for this snippet.

Remove the url field from WordPress comment form
In some cases, you may not need or want that people who comment your posts can leave their website url. Here is a simple code snippet to remove the url field from WordPress comment form. Paste the code below into your functions.php file. Once saved, the url field will be removed from your comment form.
function remove_comment_fields($fields) {
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
Force the RSS Widget to refresh more often
WordPress have a built-in widget that allows you to display any RSS feed on your blog. Although this widget is great, sometimes you may wish it refresh more often. Here is a simple solution to modify the widget refresh rate. To apply this trick, simply put the following code into your functions.php file
add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 600;') );
Add a Delete Button to the WordPress Admin Bar
Add a Delete button to your WordPress Admin Bar when you’re on a single post page. This lets you easily delete a post from your front end – very handy for some sites that end up doing a lot of deleting for one reason or another. To use this, enter the following code into your functions.php file.
function fb_add_admin_bar_trash_menu() {
global $wp_admin_bar;
if ( !is_super_admin() || !is_admin_bar_showing() )
return;
$current_object = get_queried_object();
if ( empty($current_object) )
return;
if ( !empty( $current_object->post_type ) &&
( $post_type_object = get_post_type_object( $current_object->post_type ) ) &&
current_user_can( $post_type_object->cap->edit_post, $current_object->ID )
) {
$wp_admin_bar->add_menu(
array( 'id' => 'delete',
'title' => __('Move to Trash'),
'href' => get_delete_post_link($current_object->term_id)
)
);
}
}
add_action( 'admin_bar_menu', 'fb_add_admin_bar_trash_menu', 35 );
Disabling Plugin Deactivation
This snippet is particularly useful if you have given a client plugin activation/deactivation privileges (allowing them to add new plugins themselves), but the site you have built requires some core plugins to function and should never be deactivated. The code below will remove the ‘Deactivate’ links from whichever plugins you deem fundamental as well as removing the ‘Edit’ links from all plugins. Paste this into your functions.php:
add_filter( 'plugin_action_links', 'slt_lock_plugins', 10, 4 );
function slt_lock_plugins( $actions, $plugin_file, $plugin_data, $context ) {
// Remove edit link for all
if ( array_key_exists( 'edit', $actions ) )
unset( $actions['edit'] );
// Remove deactivate link for crucial plugins
if ( array_key_exists( 'deactivate', $actions ) && in_array( $plugin_file, array(
'slt-custom-fields/slt-custom-fields.php',
'slt-file-select/slt-file-select.php',
'slt-simple-events/slt-simple-events.php',
'slt-widgets/slt-widgets.php'
)))
unset( $actions['deactivate'] );
return $actions;
}
Allow PHP in WordPress text widgets
Paste the code in your functions.php file.
function php_text($text) {
if (strpos($text, '<' . '?') !== false) {
ob_start();
eval('?' . '>' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}
add_filter('widget_text', 'php_text', 99);
How to execute shortcodes inside custom fields
By default, WordPress do not allows shortcodes to be executed inside custom fields. If for some reason you need to be able to execute a shortcode inside a specific custom field, here is an easy way to do it. Just put this code into whatever page you are displaying the results of the shortcode, and change the your_custom_field_here to the name of your custom field.
<?php echo apply_filters('the_content', get_post_meta($post->ID, 'your_custom_field_here', true)); ?>
Create custom dashboard help messages
If you’re building a site for a client and they have some problems with some parts of the dashboard, a good idea is to provide contextual help to the client. The following hack will allow you to add a custom help messages for the blog admin. The code have to be pasted into your functions.php file in order to work. Please edit line 4 with the desired help message.
function my_admin_help($text, $screen) {
// Check we're only on my Settings page
if (strcmp($screen, MY_PAGEHOOK) == 0 ) {
$text = 'Here is some very useful information to help you use this plugin...';
return $text;
}
// Let the default WP Dashboard help stuff through on other Admin pages
return $text;
}
add_action( 'contextual_help', 'my_admin_help' );
Automatically generate a QR code for your posts
QR Codes are quite popular online theses days. Do you want to generate QR codes for all of your blog posts? Here is a very simple way to do it. Copy the following code and paste it into your single.php file, where you want the QR code to be displayed. If you also want to provide a QR code on pages, paste the code into page.php as well.
<img src="http://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php the_permalink(); ?>" alt="QR: <?php the_title(); ?>"/>
Add a widget to your dashboard
For those who want to create your own widget, it’s easier than it seems. just add the html to the snippet. Insert this code snippet in your functions.php
function custom_dashboard_widgets(){
wp_add_dashboard_widget('my_custom_widget_id', 'My Widget Title', 'my_own_widget');
}
function my_own_widget() {
echo '<p>Your content here</p>';
}


Straight to my Delicious favorites collection… ty!
Hi Kobe,
thanks man, glad you liked the hacks! Enjoy your weekend, cheers & ciao!