get_the_title( int|WP_Post $post ): string

Retrieves the post title.

Description

If the post is protected and the visitor is not an admin, then "Protected" will be inserted before the post title. If the post is private, then "Private" will be inserted before the post title.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.

Return

string

Source

function get_the_title( $post = 0 ) {
	$post = get_post( $post );

	$post_title = isset( $post->post_title ) ? $post->post_title : '';
	$post_id    = isset( $post->ID ) ? $post->ID : 0;

	if ( ! is_admin() ) {
		if ( ! empty( $post->post_password ) ) {

			/* translators: %s: Protected post title. */
			$prepend = __( 'Protected: %s' );

			/**
			 * Filters the text prepended to the post title for protected posts.
			 *
			 * The filter is only applied on the front end.
			 *
			 * @since 2.8.0
			 *
			 * @param string  $prepend Text displayed before the post title.
			 *                         Default 'Protected: %s'.
			 * @param WP_Post $post    Current post object.
			 */
			$protected_title_format = apply_filters( 'protected_title_format', $prepend, $post );

			$post_title = sprintf( $protected_title_format, $post_title );
		} elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) {

			/* translators: %s: Private post title. */
			$prepend = __( 'Private: %s' );

			/**
			 * Filters the text prepended to the post title of private posts.
			 *
			 * The filter is only applied on the front end.
			 *
			 * @since 2.8.0
			 *
			 * @param string  $prepend Text displayed before the post title.
			 *                         Default 'Private: %s'.
			 * @param WP_Post $post    Current post object.
			 */
			$private_title_format = apply_filters( 'private_title_format', $prepend, $post );

			$post_title = sprintf( $private_title_format, $post_title );
		}
	}

	/**
	 * Filters the post title.
	 *
	 * @since 0.71
	 *
	 * @param string $post_title The post title.
	 * @param int    $post_id    The post ID.
	 */
	return apply_filters( 'the_title', $post_title, $post_id );
}

Hooks

apply_filters( ‘private_title_format’, string $prepend, WP_Post $post )

Filters the text prepended to the post title of private posts.

apply_filters( ‘protected_title_format’, string $prepend, WP_Post $post )

Filters the text prepended to the post title for protected posts.

apply_filters( ‘the_title’, string $post_title, int $post_id )

Filters the post title.

Changelog

VersionDescription
0.71Introduced.

User Contributed Notes

  1. Skip to note 5 content

    get_the_title intentionally allows for HTML

    So get_the_title should not be escaped.

    Use the_title_attribute() instead of get_the_title() if you’re outputting the post title for html attributes.

    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
  2. Skip to note 6 content

    get_the_title should be escaped.

    Super admins and administrators have the ability to enter arbitrary HTML in the title field, but that doesn’t prevent problems from appearing, for example:

    • A rogue administrator adds a script tag with malicious javscript
    • A hacker manages to change the title via an exploit
    • A compromised plugin uses a filter to change the title
    • A broken plugin allows it to be changed
    • A hacker has broken into Redis/APC/Memcached and modified the cache
    • File based caches have been compromised

    All of this is a non-issue with escaping, which makes sure what’s outputted is what you expected. That doesn’t mean you can’t let users put HTML in there, as long as you specify which tags are allowed

    To display the title safely, do this:

    echo esc_html( get_the_title() );

    And if you want the title to include HTML tags:

    echo wp_kses_post( get_the_title() );
  3. Skip to note 7 content

    Print the current post’s title

    echo get_the_title();

    Simple breadcrumb trail for pages, two levels deep.

     
    echo '<div class="breadcrumb">';
    	// If there is a parent, display the link.
    	$parent_title = get_the_title( $post->post_parent );
    
    	if ( $parent_title != the_title( ' ', ' ', false ) ) {
    		echo '<a href="' . esc_url( get_permalink( $post->post_parent ) ) . '" alt="' . esc_attr( $parent_title ) . '">' . $parent_title . '</a> » ';
    	}
    
    	// Then go on to the current page link.
    	echo '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark" alt="' . esc_attr( get_the_title() ) . '">' . get_the_title() . '</a>';
    echo '</div>';
  4. Skip to note 8 content

    get_the_title is being filtered before value return. If you are checking for raw value of a post title, for empty titles you might get an ‘untitled’ string value depending on the locale of the blog. In order to get raw value of a post title, use get_post and access its post_title property

You must log in before being able to contribute a note or feedback.