Now, as “everyday” as WordPress may have become – for the fact that every second or third website you come across is powered by WordPress – there are always the frequent “how the heck did they do this!” moments when you are scrolling around the WordPress realm.
Despite WordPress being so remarkably pursued by the web development community, it has never ceased to wear some incredible tricks and surprises up its sleeve. While there is a whole ensemble of plugins that never fail to take a WordPress website’s functionality beyond defaults and succeed in letting webmasters shape their website in exactly the same way they want to, it is the WordPress Hacks that well and truly give the webmasters a customization capability of the likes of which can’t be accomplished by any plugins whatsoever.
The WordPress hacks are establishing a strong foothold and gaining a lot of ground as the non-programming webmasters are seeking the help of some web developers to inject few features in their website that would really help them enhance its functionality and take it beyond its peers. We, however, are making your job easier by tossing at you few code snippets you would need (and want) to get your website rolling in just the direction you want it to.
The following list is about such hacks that you can implement on your website for it to boast of exceptional custom capabilities and flaunt features that seem pretty improbable otherwise.
Widgets never cease to give our website that edge we are always vying for. They form an indispensable part of just about every WordPress website, and thus, a way to enhance their appearance or their features is always welcome, and that’s exactly what the shortcodes are meant to do. Use a filter for the same:
add_filter( 'widget_text', 'do_shortcode' );
The menu on the top navigation bar of your website is probably ignorable, simply for its position. And this is why you need to move it and this is where moving it will help:
#top-nav {float: right;}
There is every possibility that you may have decided to realign your logo to place it at the center of the header, so, you would need to move the navigation menu to the center so that it doesn’t look out of the place. And this is how you do it:
#navigation { position: relative; } #main-nav { clear: left; float: left; list-style: none; margin: 0; padding: 0; position: relative; left: 50%; text-align: center; } .nav li { display: block; float: left; list-style: none; margin: 0; padding: 0; position: relative; right: 50%; } .nav li.hover, .nav li.hover { position: relative; } .nav li ul li { left: 0;
It’s not rare to make mistakes while writing a post, but when the posts are already sent to RSS, the minor mistakes can prove to be costly enough. And this is when delaying the process comes to the rescue.
Now, you can make sure that the posts don’t go to the RSS feed as soon as you publish them on your blog. All you need to do is to add this piece of code to the functions.php file in your website’s backend:
function publish_later_on_feed($where) { global $wpdb; if ( is_feed() ) { $time_now = gmdate('Y-m-d H:i:s'); $time_delay = '15'; // integer $time_span = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR $where = " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$time_now') > $time_delay "; } return $where; } add_filter('posts_where', 'publish_later_on_feed');
The theme you have chosen for your WordPress website may be automatically modifying the avatar images to present them in a circular form. But many webmaster would rather prefer their avatars to be displayed in a square form. For the same, this code by GitHub works exceptionally well:
#post-author .profile-image img, #comments .avatar img { border-radius: 0; -moz-border-radius: 0; -webkit-border-radius: 0; }
Using pictures in your posts always gives it an extra push among your audience and coaxes them to read and further share it among their circles. And when you are able to use the featured image of the post in your RSS Feeds, you further enhance the reach of the post by making it visually catchier:
add_filter('the_content_feed', 'rss_post_thumbnail'); function rss_post_thumbnail($content) { global $post; if( has_post_thumbnail($post->ID) ) $content = '<p>' . get_the_post_thumbnail($post->ID, 'thumbnail') . '</p>' . $content; return $content; }
While there are tools that facilitate removing titles from all the pages of the site, at times, you only want to remove them from particular pages. And here is the code you need for that – just replace “abcd” with the page id number for any post:
.page-id-xxxx .title { display: none; }
HTML in the comment section can lead to a truckload of spam, which eventually might have serious repercussions on your website. And this is why disabling the HTML in comment section is so critically important to keep your website free of spam. To do so, add this piece of code in the functions.php file:
//As soon as someone comments function plc_comment_post( $incoming_comment ) { // convert everything in a comment to display literally $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']); // the one exception is single quotes, which cannot be #039; because WordPress marks it as spam $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] ); return( $incoming_comment ); } // This will occur before a comment is displayed function plc_comment_display( $comment_to_display ) { // Put the single quotes back in $comment_to_display = str_replace( ''', "'", $comment_to_display ); return $comment_to_display; } add_filter( 'preprocess_comment', 'plc_comment_post', '', 1 ); add_filter( 'comment_text', 'plc_comment_display', '', 1 ); add_filter( 'comment_text_rss', 'plc_comment_display', '', 1 ); add_filter( 'comment_excerpt', 'plc_comment_display', '', 1 ); // This stops WordPress from trying to automatically make hyperlinks on text: remove_filter( 'comment_text', 'make_clickable', 9 );
You may not have deemed it all too necessary, but if the users who search for particular posts are taken directly to the full post instead of a thumbnail view of it with excerpt, they’d appreciate that. It doesn’t force them to continue navigating and thus improves the user-experience considerably well.
add_action('template_redirect', 'redirect_single_post'); function redirect_single_post() { if (is_search()) { global $ks29so_query; if ($ks29so_query->post_count == 1 && $ks29so_query->max_num_pages == 1) { ks29so_redirect( get_permalink( $ks29so_query->posts['0']->ID ) ); exit; } } }
Copyright is an indispensable part of any website that contains a lot of useful information that the webmaster doesn’t want to be copied by the other randomizers on the web. This is why displaying copyright information accurately and in sync with the current year is important. The following piece of code makes it possible for you to constantly update the copyright information automatically:
function comicpress_copyright() { global $wpdb; $copyright_dates = $wpdb->get_results(" SELECT YEAR(min(post_date_gmt)) AS firstdate, YEAR(max(post_date_gmt)) AS lastdate FROM $wpdb->posts WHERE post_status = 'publish' "); $output = ''; if($copyright_dates) { $copyright = "© " . $copyright_dates[0]->firstdate; if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) { $copyright .= '-' . $copyright_dates[0]->lastdate; } $output = $copyright; } return $output; }
Once any user is logged into your site, he or she can be easily redirected to another URL of your choice. All you need is this code snippet in the functions.php file:
<?php function redirect_user_on_role() { //retrieve current user info global $current_user; get_currentuserinfo(); //If login user role is Subscriber if ($current_user->user_level == 0) { ks29so_redirect( home_url() ); exit; } //If login user role is Contributor else if ($current_user->user_level > 1) { ks29so_redirect( home_url() ); exit; } //If login user role is Editor else if ($current_user->user_level >8) { ks29so_redirect( home_url() ); exit; } // For other roles else { $redirect_to = 'http://google.com/'; return $redirect_to; } } add_action('admin_init','redirect_user_on_role'); ?>
The header of your website is a great advertising area where you can display really noticeable ads. However, one common thing that is observed is that the site admins don’t enjoy a lot of control over how they wish to display elements on the header and this leads to the lack of flexibility.
What admin want is the customization capability to let them add whatever they want to the right of the logo, and the following lines of code lets them do exactly that:
if (function_exists('register_sidebar')) { register_sidebar(array( 'name' => 'Header Widget', 'id' => 'header-widget', 'description' => 'This is a widgetized area in the right side of the header.', 'before_widget' => '<div id="%1$s" div class="widget">', 'after_widget' => '</div>', 'before_title' => '<h3>', 'after_title' => '</h3>' )); add_action( 'woo_header_inside', 'custom_canvas_header' ); function custom_canvas_header () { ?> <div id="header-widget"> <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('header-widget')) : else : ?> <?php endif; ?> </div> <?php } }
Many webmasters are vying to have a more personalized logo for their dashboard in the WordPress backend. The following code serves the purpose, even when it is the client that has come to you with this kind of a request:
add_action('admin_head', 'custom_logo'); function custom_logo() { echo ' <style type="text/css"><!-- #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; } --></style>'; }
You may not be all too keen on using the Visual Editor, and instead, it is the HTML Editor that has caught your fancy. Well, either way, you have the freedom to choose what editor you wish to set as the default one:
# Visual Editor as default add_filter( 'ks29so_default_editor', create_function('', 'return "tinymce";') ); # HTML Editor as default add_filter( 'ks29so_default_editor', create_function('', 'return "html";') ); 15. Cast Away the Primary or Top Navigation Canvas, in particular, has two navigation bars. One navigation bar is located above the header and it is thereby called the Top Navigation Bar. Then, you have the Primary Navigation that is located below the header. Now, whether you wish to use both or just one is entirely your prerogative, on our end, we can show you how to remove them: Primary Navigation can be gotten rid of by this code: add_action( 'init', 'remove_canvas_main_navigation', 10 ); function remove_canvas_main_navigation () { // Remove main nav from the woo_header_after hook remove_action( 'woo_header_after','woo_nav', 10 ); } And to remove the Top Navigation, you will need this: add_action( 'init', 'remove_canvas_top_navigation', 10 ); function remove_canvas_top_navigation () { // Remove top nav from the woo_top hook remove_action( 'woo_top', 'woo_top_navigation', 10 ); }
So, these are some of the most incredibly effective WordPress hacks that command all your attention. When having free rein over your website’s functionality is on the agenda, the above WordPress hacks serve the purpose in a manner most seamless and surgical.
Nice explanation indeed.
Jack,
Great article and valuable information but could you please clarify which WP file to insert the code for the copyright code and where exactly to insert it within that file?
I tried inserting into the footer.php file without any success! Next I tried the functions.php file but I am not really sure where exactly to insert that code in that file… could you please clarify or direct me to another helpful resource that might explain?
Thanks!
Rick
Rick, you don’t need to go to all that work with the code above. All you have to do is put this code where you want it in the footer and it will display the current year.
©
Apparently, the comments don’t display the code. You can go here and see a tutorial.
http://css-tricks.com/snippets/php/automatic-copyright-year/