.. Your Daily Design Dosis

15 WordPress Code Snippets/Hacks to Use


WordPress is an open source Content Management System (CMS), often used as a blog publishing application, powered by PHP and MySQL. WordPress comes with standart functions but you can use many plugins or code snippets/hacks to give your blog a different look or function(s).

For a beginner the WordPress Codex is the best place to learn basic things as WordPress improves day by day their plug-in architecture and their template system by using more and more functional codes. I love these code snippets/hacks for WordPress, it’s the way how to make a great customized WordPress Theme. Here is a small collection of different code snippets/hacks which I have found on different WordPress related blogs.

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.

Styling Your Code Tags

By default, using <pre> and <code> will put the text in a monospaced font and use the font-size from the body tag. What if you want the font-size to be different, and the look of these tags to also have a different color or style to them?

In your style.css style sheet in your WordPress Theme folder, add styles to these two tags. Here is an example:

pre {border: solid 1px blue;
font-size: 1.3 em;
color: blue;
margin: 10px;
background: #FFFFB3;
overflow: auto;}

code {font-size:1.2em;
color: #008099}

The extra CSS-line overflow: auto; I’ve added to my CSS, to let my code stay in the ‘box’ I’ve created with the <pre></pre> command so where needed I’ll get an extra scrollbar.

Source: WordPress Codex

Unlock More Useful Buttons in the WordPress Post Editor

I’ve added buttons which are not shown by default on the WordPress post editor, including horizontal rules, subscripts, superscripts, font select, font size select, clean up messy code, styles (alignment, captions, gallery, etc.).

Adding more buttons to the editor is really easy. Simply paste this snippet into your theme’s functions.php file.

function enable_more_buttons($buttons) {
  $buttons[] = 'hr';
  $buttons[] = 'sub';
  $buttons[] = 'sup';
  $buttons[] = 'fontselect';
  $buttons[] = 'fontsizeselect';
  $buttons[] = 'cleanup';
  $buttons[] = 'styleselect';
  return $buttons;
add_filter("mce_buttons_3", "enable_more_buttons");

Source: WPMU

Show WordPress Post Attachments

To achieve this hack, just paste the following code anywhere in your post.php file and attachments will be displayed.

$args = array(
	'post_type' => 'attachment',
	'numberposts' => null,
	'post_status' => null,
	'post_parent' => $post->ID
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo apply_filters('the_title', $attachment->post_title);
		the_attachment_link($attachment->ID, false);

Source: Snipplr

Disable Comments in Old Posts via PHP

Just a quick WordPress snippet for future reference.  Here’s a good way to do it viaPHP:

<?php  function close_comments( $posts ) { 	if ( !is_single() ) { return $posts; } 	if ( time() - strtotime( $posts[0]--->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
		$posts[0]->comment_status = 'closed';
		$posts[0]->ping_status    = 'closed';
	return $posts;
add_filter( 'the_posts', 'close_comments' );

You can run this script as a plugin, through your theme’s functions.php, or through a custom user-functions.php file. Simply set the desired number of days by changing the number “30” to whatever you would like. As is, this script will close comments, pingbacks and trackbacks on all articles posted more than 30 days ago.

Source: Perishable Press

How to stop WordPress asking for FTP Detail

Here’s a quick hack to make WordPress remember your FTP detail like FTP hosting, username, password and SSL connection…

1. Find the “wp-config.php” file, locate at the root of your WordPress folder.
2. Add following code inside :

define('FTP_HOST', 'Your_FTP_Hosting');
define('FTP_USER', 'Your_FTP_Username');
define('FTP_PASS', 'Your_FTP_Password);
//If you use SSL connection, set this to true
define('FTP_SSL', false);

P.S You can paste above code everywhere inside your wp-config.php
3. Done, try do a plugin upgrade again, WordPress will no longer ask for any FTP detail.

Source: ahmook

Display Most Popular Post from a Specific Category

Sometimes you might want to display only your popular posts from your “Featured” category, or your “books” category. Use this snippet to achieve it.

'cat' => 3,
'orderby' => 'comment_count',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'caller_get_posts'=> 1
$my_query = null;
$my_query = new WP_Query($args);

if( $my_query->have_posts() ) { ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php  endwhile; ?>
<?php }

wp_reset_query(); ?>

Remember to change this line to the category you want:

 'cat' => 3,

And set this line to how many posts you want to display:

 'posts_per_page' => 6,

Source: Dynamic WP

List Upcoming Posts

Want to tease your readers with what you’ve got to come? What to display an event that’s happening in the future? This snippet will let you list which posts you have in draft.

<div id="zukunft">
<div id="zukunft_header"><p>Future events</p></div>
<?php query_posts('showposts=10&amp;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><?php the_time('j. F Y'); ?></span></p>
<?php endwhile; else: ?><p>No future events scheduled.</p><?php endif; ?>

Source: WP Recipes

Exclude Specific Category

It can sometimes come in handy to exclude specific categories from being displayed.

<?php query_posts('cat=-2'); ?>
<?php while (have_posts()) : the_post(); ?>
//the loop here
<?php endwhile;?>

Source: Web Designer Wall

Separate Trackbacks from Comments

Trackbacks are great for acknowledging people who have linked to you and they help you to keep track of where your post has appeared. However, they can be annoying when they break up your comments. Use this snippet to separate them.

Find this in your comments.php

foreach ($comments as $comment) : ?>
// Comments are displayed here

And replace with this:

<ul class="commentlist">
<?php //Displays comments only

<span style="white-space: pre;"> </span&gt;foreach ($comments as $comment) : ?>
<span style="white-space: pre;"> </span><?php $comment_type = get_comment_type(); ?>
<span style="white-space: pre;"> </span><?php if($comment_type == 'comment') { ?>
<span style="white-space: pre;"> </span> <li>//Comment code goes here</li>
<span style="white-space: pre;"> </span><?php }

<?php //Displays trackbacks only

<span style="white-space: pre;"> </span&gt;foreach ($comments as $comment) : ?>
<span style="white-space: pre;"> </span><?php $comment_type = get_comment_type(); ?>
<span style="white-space: pre;"> </span><?php if($comment_type != 'comment') { ?>
<span style="white-space: pre;"> </span> <li><?php comment_author_link() ?></li>
<span style="white-space: pre;"> </span><?php }

Source: WP Recipes

Change the default admin name

It’s a good idea to not have a username called “admin” as it means that any hackers already have access to your username. If you already have a default called “admin” you can change it with this snippet in your SQL database. Make sure to put in your new username.

UPDATE wp_users SET user_login = 'Your New Username' WHERE user_login = 'Admin';

Source: Smashing Magazine

Remove the WordPress version

This is especially helpful if you’re using an older version of WordPress. Best not tell anyone else that you are.

function no_generator() { return ''; }
add_filter( 'the_generator', 'no_generator' );

Source: WP Zine


Protect your wp-config.php

Use this snippet to protect the precious. Add this to your .htaccess file

<Files wp-config.php>
order allow,deny
deny from all

Source: WP Zine

Share Via Email

Let everyone share your work. They can click on the button which opens the visitor’s default email client. Then they can email people and tell them how much they love you.

<?php echo "<a href="mailto:type%20email%20address%20here?subject=I%20wanted%20to
<?php bloginfo('name'); ?>&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;body=<?php the_title(); ?> - <?php the_permalink(); ?>"
title="Email to a friend/colleague"target="_blank">Share via Email</a>"; ?>

Source: Lava 360

Add Facebook and Twitter to the bottom of each post

Easily add Facebook and Twitter to the bottom of your posts by adding this to yourfunctions.php

function share_this($content){

if(!is_feed() &amp;&amp; !is_home()) {
$content .= '<div class="share-this">
<a href="http://twitter.com/share"

<script type="text/javascript"

<div class="facebook-share-button">

scrolling="no" frameborder="0" style="border:none;
overflow:hidden; width:200px; height:21px;"

return $content;
add_action('the_content', 'share_this');

Source: WP Recipes

Display your number of Twitter Followers

You can add your Twitter followers too! Make sure you insert your own username.

$twit = file_get_contents('http://twitter.com/users/show/USERNAME.xml');

$begin = '<followers_count>'; $end = '</followers_count>';
$page = $twit;
$parts = explode($begin,$page);
$page = $parts[1];
$parts = explode($end,$page);
$tcount = $parts[0];

if($tcount == '') { $tcount = '0'; }
echo '<b> '.$tcount.' </b> Followers';

Source: webm.ag

– § –

I hope you’ve enjoyed this resourceful article. If you’ve liked this article, please retweet, and share it with your friends on Facebook. If you think I’ve missed a very special or great hack or snippet, please let me know by filling in a comment … cheers!


Author: Jan Rajtoral

Jan Rajtoral AKA Gonzo the Great is the Founder of and Designer at gonzodesign, providing design services across the full spectrum of Brand Identity, Graphic Design, Print and Advertising Design & Website Design.


on this article: “15 WordPress Code Snippets/Hacks to Use”
  1. great post bookmarked keep up the good work

  2. These are really helpful snippets! Thank you.

  3. This blog has got a lot of really helpful stuff on it! Cheers for sharing it with me!

  4. Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is excellent, let alone the content!

    • Hi Barend,

      Thanks for the compliment ;-P

      I’m blogging for 2 years now, and no, it’s not easy ;-P But I’m glad you – as a reader – can’t see how much time I invest into writing articles and updating my theme. But Barend, you must made my day, Cheers & Ciao ..

  5. Hey,
    For me showing the WP Version is a security issue. So it’s not done by removing it in the html markup. The Feeds also contain it! Here the solution (insert in functions.php)
    remove_action(‘wp_head’, ‘wp_generator’);
    remove_action(‘rss2_head’, ‘the_generator’);
    remove_action(‘rss_head’, ‘the_generator’);
    remove_action(‘rdf_header’, ‘the_generator’);
    remove_action(‘atom_head’, ‘the_generator’);
    remove_action(‘commentsrss2_head’, ‘the_generator’);
    remove_action(‘comments_atom_head’, ‘the_generator’);
    remove_action(‘opml_head’, ‘the_generator’);
    remove_action(‘app_head’, ‘the_generator’);

    • Hi Sebastian,

      The hack mentioned in the article is a hack for when making a WP Theme for a client. This hack should remove the version of your WP shown in your Dashboard (at least that’s told in the original article – see the source link in the article). This is done because you don’t want to be ‘stalked’ by your clients everytime to update their WP version!

      But nevertheless I want to thank you for your (even better) hack, although I question if the version of WP is removed from the dashboard with this hack? Thanks for your constructive comment, I love it ;-P

      Cheers & Ciao ..

  6. When it comes to wordpress tricks and tactics, trust me, gonzodesign is the place to go. Great article, Jan. You’re the WordPress Man!!

  7. Awesome tips, nice to have these in one place. I added all of these to my WP hack sheet.
    Thanks Jan,

    • Hi Gabi,

      Thanks for your comment, much appreciated! There are more articles with hacks and code snippets for WP here on the gonzoblog (check them out – see: related posts [at the bottom of the post]).

      But please, always look out with hacks/snippets, I don’t know if some don’t work (anymore) in WP3+ – from an earlier post here, I know the logo-on-the-login-page-hack doesn’t work in WP3+!

      So please always update your database before adding these hacks to your WP-theme! BTW: – What a good idea to collect all hacks on a WP-Sheet. Cheers & Ciao ..