Skip to content

Instantly share code, notes, and snippets.

@hitsujixgit
Last active August 29, 2015 14:05
Show Gist options
  • Save hitsujixgit/e5c84ebfefaed7ba220f to your computer and use it in GitHub Desktop.
Save hitsujixgit/e5c84ebfefaed7ba220f to your computer and use it in GitHub Desktop.
Replace WP widget title string to image.
<?php
// Widgetタイトルを画像に置き換える
if( !function_exists('change_widget_title_to_image') ) {
function change_widget_title_to_image($params) {
// categories-xxの形式で、画像を挿入する
if(preg_match('/^categories-\d+/', $params[0]['widget_id'])) {
$params[0]['before_title'] = $params[0]['before_title'].'<img src="'.get_bloginfo('template_url').'/img/categories_2x.png" width="91" height="20" alt="categories" /><span style="display:none;">';
$params[0]['after_title'] = '</span>'.$params[0]['after_title'];
} else if ( preg_match('/^recent-posts-\d+/', $params[0]['widget_id']) ) {
// recent-posts-xxの形式で、画像を挿入する
$params[0]['before_title'] = $params[0]['before_title'].'<img src="'.get_bloginfo('template_url').'/img/recent_posts_2x.png" width="114" height="20" alt="categories" /><span style="display:none;">';
$params[0]['after_title'] = '</span>'.$params[0]['after_title'];
} else if ( preg_match('/^search-\d+/', $params[0]['widget_id']) ) {
$params[0]['before_title'] = $params[0]['before_title'].'<img src="'.get_bloginfo('template_url').'/img/search_posts_2x.png" width="113" height="20" alt="search posts" /><span style="display:none;">';
$params[0]['after_title'] = '</span>'.$params[0]['after_title'];
} else if ( preg_match('/^text-\d+/', $params[0]['widget_id']) ) {
// Text-widgetのタイトルを全てAuthor画像にしてしまう。暫定対応
$params[0]['before_title'] = $params[0]['before_title'].'<img src="'.get_bloginfo('template_url').'/img/author_2x.png" width="58" height="20" alt="author" /><span style="display:none;">';
$params[0]['after_title'] = '</span>'.$params[0]['after_title'];
}
return $params;
}
}
add_filter('dynamic_sidebar_params', 'change_widget_title_to_image');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment