One of the things that WordPress really popular today is the opportunity to extend it a million ways. For example, functionality can be enhanced with plugins while themes are great for changing the appearance. As the result, the site gets tweaked to meet various needs of blogging, ecommerce, and others.
But what about unleashing a true power of WordPress with tweaks? Let’s take customizing one step further with these killer hacks.
Have you ever wondered why WordPress did not allow displaying featured images by default in RSS feeds? That’s clearly a better move if you are a blogger or you just want to provide a bit more encouragement to people to subscribe.
Here’s how to do it (add this to your theme’s functions.php file):
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; }
Bloggers see Facebook as a huge database of potential customers and visitors. That’s correct, with more than a billion users it can really increase the volume of traffic. So, how does one creates a “send to Facebook” button to add to a blog and boost the traffic?
It’s done by opening the single.php file in the current theme and pasting this code in the loop:
<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>
Done!
We all know that WordPress does not provide 100 percent quality images because it compresses them to 90 percent. While this may not sound like something that needs to be changed for many people, we developers know we can do better.
For example, we can force the platform to display 100 percent original images to ensure perfect quality. The following needs to be added to the current theme’s functions.php file:
add_filter( 'jpg_quality', 'high_jpg_quality' ); function high_jpg_quality() { return 100; }
By using WordPress, one can add a shortcut to a site as a reference. As the result, there is no need to type out the URL every time. Here’s how it’s done:
<?php bloginfo('url'); ?>
The function is then used like this:
<a href="<?php bloginfo('url'); >/about">About Our Company</a>
For hackers, knowing a WordPress version is enough to try to exploit some security holes (especially in older versions: the Panama papers breach is blamed on old installations!). To hide this information from anyone, you can use the following hack:
<?php // Remove the WP version for extra WordPress Security function remove_wp_version(){ return ''; } add_filter('the_generator', 'remove_wp_version'); ?>
If you noticed that many new WordPress users have been visiting the user page, it means you probably allowed anyone to register. When you check the setting, you’ll find that the users are subscribing through your RSS.
If you don’t want too many registered users, go to Settings and uncheck the box that permits everyone to register using membership option. That’s it!
Spam is a real problem for many people who use WordPress. For example, bloggers often discover links to suspicious sites in the comments section. The posts with spam are not a good way to get noticed, so the problem needs to be eliminated as soon as possible.
To disable HTML and prevent spammers to include links and other methods, this code needs to be added to functions.php file:
// This will occur when the comment is posted 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 );
This incredibly simple hack can be very helpful in terms of increasing security. For hackers, it will be much harder to predict an email than a username, so it is a more secure method.
This is a great hack for bloggers that allows to show the latest blog entries on the homepage. No need to make the blog a homepage! Only the following code should be used:
<?php query_posts($query_string . '&showposts=5' ); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="story"> <div class="story-content"> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <?php the_excerpt(); ?> <?php endwhile; endif; ?>
As it was said above, one of the greatest functions of WordPress is customization. This section falls into this category. The following hack allows to feature a client’s logo instead of the default admin logo. You don’t need a plugin or anything to do that, just insert the following code into functions.php:
function custom_admin_logo() { echo '<style type="text/css"> #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/admin_logo.png) !important; } </style>'; } add_action('admin_head', 'custom_admin_logo');
This is another security hack that removes the error message from the login page, thus preventing hackers from alert about incorrect login or password. Insert this code:
add_filter('login_errors',create_function('$a', "return null;"));
The next hack on our list lets WordPress users decide whether to use Visual Editor or HTML editor. If you prefer a particular one, use this code (insert them into functions.php):
# HTML Editor as default add_filter( 'ks29so_default_editor', create_function('', 'return "html";') ); # Visual Editor as default add_filter( 'ks29so_default_editor', create_function('', 'return "tinymce";') );
This hack could be useful for developers who want to make their clients pleasantly surprised by having some text on the dash footer. Just insert the following into functions.php file:
function remove_footer_admin () { echo "Your own text"; } add_filter('admin_footer_text', 'remove_footer_admin');
You can teach WordPress to use helpful shortcodes by adding the following code into functions.php:
add_filter('widget_text', 'do_shortcode');
Once more hack for developers to customize websites for their clients. To personalize the installation, you can add a logo to the dash by using the following:
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>'; }
This hack is for bloggers who want their upcoming posts to be scheduled in a list visible to the visitors. Copy and paste the following code anywhere in your theme:
<div id="zukunft"> <div id="zukunft_header"><p>Future events</p></div> <?php query_posts('showposts=10&post_status=future'); ?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div > <p class><b><?php the_title(); ?></b><?php edit_post_link('e',' (',')'); ?><br /> <span class="datetime"><?php the_time('j. F Y'); ?></span></p> </div> <?php endwhile; else: ?><p>No future events scheduled.</p><?php endif; ?> </div>
It’s important that the address of your site is correct. The esc_url() function allows to keep it error-free:
$my_url = 'http://myawesomesite.com/?awesome=true'; $url = esc_url( $my_url );
WordPress comes with no default limit of post revisions stored in the database. With time, their number can increase dramatically (which won’t do any good to the site). Add the following code to wp-config.php file to set the limit:
define( 'WP_POST_REVISIONS', 3 );
The example sets the limit to 3.
This one continues the topic of post revisions. If you want to disable them, use this code:
define( 'WP_POST_REVISIONS', -1 );
In some cases, standard excerpts may not be the best fit for the layout. Changing them is really easy. Open functions.php and insert (“20” is the value of length):
function custom_excerpt_length( $length ) { return 20; } add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
Some developers don’t know that WordPress can display the date using “Time Ago” format. This can be done by pasting the following code anywhere within the loop:
Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago';
Ready to unleash the power of WordPress with these simple but handy hacks? By trying them, you can discover new capabilities of your website and extend its functionality to make it a more functional environment.
Once you’ve seen the benefits of these hacks, you can make your content management effort more effective. Also, some of them can really enhance the security of your site, which is especially important.
All of this – without the need to install a single plugin! Given that WordPress sites rely on many plugins installed, these advancements could be very helpful to maintain a quick loading speed.
Hope this article is helpful to you to find some new WordPress hacks and enhance your site.