the_titlefilter-hookWP 0.71

Applies to the post title, before retrieving or displaying it.

Post title normally displays with the_title() or retrieves with get_the_title() functions. Those functions trigger this filter.

In some cases, such as when the_title() is used, the title can be removed by returning an empty (NULL, FALSE or '') value in this filter.

Usage

add_filter( 'the_title', 'wp_kama_the_title_filter', 10, 2 );

/**
 * Function for `the_title` filter-hook.
 * 
 * @param string $post_title The post title.
 * @param int    $post_id    The post ID.
 *
 * @return string
 */
function wp_kama_the_title_filter( $post_title, $post_id ){

	// filter...
	return $post_title;
}
$title(string)
Post title.
$post_id(int)
Post ID.

Examples

0

#1 Let's add the word "Page" to the title of the static pages:

A demonstration of how the filter works:

add_filter( 'the_title', 'add_text_to_page_title' );

function add_text_to_page_title( $title ) {

	if( is_page() ){
		$title = 'Page: '. $title;
	}

	return $title;
}

As a result, the title of all static pages will look like this: Page: Page Title.

0

#2 Deleting titles in a category

This example shows how to remove headers from entries that are in the "advertise" category:

add_filter( 'the_title', 'suppress_if_blurb', 10, 2 );

function suppress_if_blurb( $title, $id = null ) {

	if ( in_category( 'advertise', $id ) ) {
		return '';
	}

	return $title;
}

The optional parameter $id = null, the default value for the second parameter, is specified because some function calls may not pass this parameter. This bug appeared in version 3.1 and was fixed in version 3.3. If backward compatibility with versions 3.1-3.3 is needed then you must specify the default value for $id, otherwise you will get a PHP warning that no required parameter is specified.

Changelog

Since 0.71 Introduced.

Where the hook is called

get_the_title()
the_title
Walker_Page::start_el()
the_title
wp_setup_nav_menu_item()
the_title
previous_post()
the_title
next_post()
the_title
get_boundary_post_rel_link()
the_title
get_parent_post_rel_link()
the_title
WP_Customize_Nav_Menu_Item_Setting::get_original_title()
the_title
WP_REST_Templates_Controller::prepare_item_for_response()
the_title
WP_REST_Menu_Items_Controller::prepare_item_for_response()
the_title
do_trackbacks()
the_title
get_adjacent_post_link()
the_title
Walker_Nav_Menu::start_el()
the_title
wp_get_archives()
the_title
WP_Posts_List_Table::column_title()
the_title
Walker_Nav_Menu_Checklist::start_el()
the_title
wp-includes/post-template.php 173
return apply_filters( 'the_title', $post_title, $post_id );
wp-includes/class-walker-page.php 205
apply_filters( 'the_title', $page->post_title, $page->ID ),
wp-includes/nav-menu.php 852
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
wp-includes/deprecated.php 151
$string .= apply_filters('the_title', $post->post_title, $post->ID);
wp-includes/deprecated.php 186
$string .= apply_filters('the_title', $post->post_title, $post->ID);
wp-includes/deprecated.php 2697
$title = apply_filters('the_title', $title, $post->ID);
wp-includes/deprecated.php 2772
$title = apply_filters('the_title', $title, $post->ID);
wp-includes/customize/class-wp-customize-nav-menu-item-setting.php 274
$original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php 681
$data['title']['rendered'] = apply_filters( 'the_title', $template->title, $template->wp_id );
wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php 515
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
wp-includes/comment.php 2991
$post_title = apply_filters( 'the_title', $post->post_title, $post->ID );
wp-includes/link-template.php 2292
$title = apply_filters( 'the_title', $title, $post->ID );
wp-includes/class-walker-nav-menu.php 218
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
wp-includes/general-template.php 2176
$text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) );
wp-admin/includes/class-wp-posts-list-table.php 1076
$parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID );
wp-admin/includes/class-walker-nav-menu-checklist.php 102
$title = apply_filters( 'the_title', $menu_item->post_title, $menu_item->ID );

Where the hook is used in WordPress

wp-admin/includes/class-wp-media-list-table.php 685
add_filter( 'the_title', 'esc_html' );
wp-admin/includes/class-wp-posts-list-table.php 778
add_filter( 'the_title', 'esc_html' );
wp-includes/default-filters.php 154
add_filter( $filter, 'capital_P_dangit', 11 );
wp-includes/default-filters.php 180
add_filter( 'the_title', 'wptexturize' );
wp-includes/default-filters.php 181
add_filter( 'the_title', 'convert_chars' );
wp-includes/default-filters.php 182
add_filter( 'the_title', 'trim' );