widget_title
Allows you to change a widget title on the front end.
Usage
add_filter( 'widget_title', 'wp_kama_widget_title_filter', 10, 3 );
/**
* Function for `widget_title` filter-hook.
*
* @param string $title The widget title.
* @param array $instance Array of settings for the current widget.
* @param mixed $id_base The widget ID.
*
* @return string
*/
function wp_kama_widget_title_filter( $title, $instance, $id_base ){
// filter...
return $title;
}
- $title(string)
- Widget title.
- $instance(array)
Current widget settings. For example, depending on its settings, the Categories widget may contain the following data:
Array ( [title] => Categories [count] => 0 [hierarchical] => 1 [dropdown] => 1 )
- $id_base(string)
- Widget ID specified in the widget class constructor. For example, the Categories widget has
id = categories, while the Recent Posts widget hasid = recent-posts.
Examples
#1 Change only the titles of Categories widgets
Do the same as in the first example, but only for Categories widgets.
/**
* Changes the title of every Categories widget.
*
* @param string $title
* @param array $instance
* @param string $id_base
*
* @return string
*/
function filter_widget_categories_title( $title, $instance, $id_base ) {
if ( 'categories' == $id_base ) {
$title = "<span class='my-custom-class'>$title</span>";
}
return $title;
}
add_filter( 'widget_title', 'filter_widget_categories_title', 10, 3 );
#2 Change the title of every widget
Wrap every widget title in a span tag with a custom CSS class.
/**
* Changes the title of every widget.
*
* @param string $title
*
* @return string
*/
function filter_all_widget_title( $title ) {
return "<span class='my-custom-class'>$title</span>";
}
add_filter( 'widget_title', 'filter_all_widget_title' );
#3 Change the title of every Categories widget displayed as a dropdown
/**
* Changes the title of every Categories widget displayed as a dropdown.
*
* @param string $title
* @param array $instance
* @param string $id_base
*
* @return string
*/
function filter_widget_categories_title( $title, $instance, $id_base ) {
if ( 'categories' == $id_base && ! empty( $instance['hierarchical'] ) ) {
$title .= ' as a dropdown';
}
return $title;
}
add_filter( 'widget_title', 'filter_widget_categories_title', 10, 3 );Changelog
| Since 2.6.0 | Introduced. |
Where the hook is called
widget_title
widget_title
widget_title
wp-includes/widgets/class-wp-widget-pages.php 56
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-categories.php 52
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-search.php 47
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-rss.php 87
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-text.php 233
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-custom-html.php 136
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-recent-posts.php 53
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-archives.php 48
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-media.php 240
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
wp-includes/widgets/class-wp-nav-menu-widget.php 54
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-recent-comments.php 85
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-tag-cloud.php 89
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-meta.php 50
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
wp-includes/widgets/class-wp-widget-calendar.php 54
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
Where the hook is used in WordPress
wp-includes/default-filters.php 164
add_filter( $filter, 'wptexturize' );
wp-includes/default-filters.php 165
add_filter( $filter, 'convert_chars' );
wp-includes/default-filters.php 166
add_filter( $filter, 'esc_html' );