May 21, 2012

Modifying Widget Titles in WordPress

On a recent project, my client wanted graphical headers for the widgets on her WordPress blog. Unfortunately, widgets are not that easily themed. Nor can you do this in CSS.

The internet was no help. Lots of people were suggesting using a “dyamic_widget_params” filter. Nope, that isn’t what I want. I don’t want to modify before and after the title, I want to actually change the title.

But, with some poking around, I figured it out. It’s actually pretty easy.

The trick is to use the ‘widget_title’ filter, and the ‘link_category’ filter. Both of these pass the title itself to your function.

Here’s my code, fix the path for your theme and put in the theme’s functions.php file:


/**
 * Show a graphic title if one matches
 */
function fancy_title($title) {
 $clean = strtolower(str_replace(' ', '_', $title));
 $fpath= '/wp-content/themes/YOUR-THEME/images/'.$clean.'.jpg';
 if (file_exists(getcwd() . $fpath)) {
   return "<img src='$fpath' alt='$title' />";
 }
 return $title;
}

add_filter('widget_title', fancy_title);
add_filter('link_category', fancy_title);

Share and Enjoy:
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress
  • services sprite Modifying Widget Titles in WordPress

Technorati Tags: ,

Related posts:

  1. WordPress title suffix plugin I read some research lately which seems to show that...
  2. Running WordPress on Lighttpd How to install Lighttpd/Wordpress on your Debian server....
  3. WordPress 2.0.1 still has annoying attribute stripping When Wordpress 2.0 was released, its aggressive tag attribute stripping...
  4. Multi-blogging for WordPress update One of the first post on this site was Multi-blogging...
  5. Importing WordPress to Banjo Thanks to Andy C I’ve finished the WordPress import script...

About Bruce Kroeze

Comments

  1. I came here looking for something similar, but different regarding Widget titles. I’m trying to hide the titles on the frontend, but keep them in the backend.

    More here:
    http://wordpress.org/support/topic/381986?replies=1

    Any thoughts?

  2. Led TV says:

    wow… i got an error, not actually an error, but my page doesnt opened at all, please help me!!!

Speak Your Mind

*