5 Wordpress Hacks To Make You Smile

POSTED ON March 1st, 2010 BY Kristin


Whether you’re building a new site for a client, starting a new blog on your server or just trying your hand at the latest online fad, there are tips all over the internet that will help you make your site faster, better looking and easier to read. Although Wordpress was originally created as a blog platform, many people today (BCD included) are now using WP for CMS of full sites, hence the need for further customization via hacks. We’ve put together just a short list of some of our favorites.

1. Add your logo to the WP Dashboard

Tired of seeing that pesky “W” logo every time you log in to make a couple of changes or add a new post? Add your own logo to the dashboard by placing the following code in your functions.php file; it’s sure to look better and might make you smile every time you log on.

add_action('admin_head', 'my_custom_logo');

function my_custom_logo() {
   echo '<style type="text/css">
         #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }</style>';
}

You can also change the login screen logo by pasting the following into your functions.php file:

function my_custom_login_logo() {
    echo '<style type="text/css">
        h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; }
    </style>';
}

add_action('login_head', 'my_custom_login_logo');

2. Get and display the first image from a post

There’s been a lot going around lately about using custom fields to create/display post thumbnails, but sometimes you just want to use the first image from your post somewhere else on your site. Insert the following code into your functions.php file:

function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i’, $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = “/images/default.jpg”;
}
return $first_img;
}

Then put this code wherever you want your first image to appear (within the loop of course):

<?php echo get_first_image() ?>

3. Use a different single template based on category

If you want to use a different format for single posts based on the category a post is in, all you need to do is create your template files, put them in a convenient directory, and insert the following code (with your category numbers and file names) in your single.php file.

<?php
  $post = $wp_query- >post;

  if ( in_category('1') ) {
  include(TEMPLATEPATH . '/single1.php');

  } elseif ( in_category('2') ) {
  include(TEMPLATEPATH . '/single2.php');

  } else {
  include(TEMPLATEPATH . '/single_other.php');

  }
? >

4. Create a category specific template

Using a different .php template for each category can be even easier than for single posts. Just save your Category template in your theme folder as category-”categoryID”.php. For example, category-2.php would apply to your Category 2, category-3.php would apply to your Category 3, and so on.

5. Use WordPress to detect user browser

Paste the following code into your functions.php file, then create class assignments in your CSS for each browser type. The final result will assign a browser based class to the body of your page depending on which browser is being used to view your site.

<?php
add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari,$is_chrome, $is_iphone;

if($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
else $classes[] = 'unknown';

if($is_iphone) $classes[] = 'iphone';
return $classes;
}
?>

Here are a few more hacks/tutorials that we like and you should check out:
WP Date Image Hack
Archive That Works
Detect Mobile Users For Mobile Theme
Add Gravatar For Post Author

Share and Enjoy:
  • RSS
  • del.icio.us
  • Digg
  • StumbleUpon
  • Twitter
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Sphinn
  • Mixx
  • Blogplay
  • email
  • Print