the_title_attribute() WordPress Function

The the_title_attribute() function allows you to easily display the title attribute of an HTML element. This can be useful for displaying the title of a link or an image.

the_title_attribute( string|array $args = '' ) #

Sanitizes the current title when retrieving or displaying.


Description

Works like the_title(), except the parameters can be in a string or an array. See the function for what can be override in the $args parameter.

The title before it is displayed will have the tags stripped and esc_attr() before it is passed to the user or displayed. The default as with the_title(), is to display the title.


Top ↑

Parameters

$args

(string|array)(Optional)Title attribute arguments. Optional.

  • 'before'
    (string) Markup to prepend to the title.
  • 'after'
    (string) Markup to append to the title.
  • 'echo'
    (bool) Whether to echo or return the title. Default true for echo.
  • 'post'
    (WP_Post) Current post object to retrieve the title for.

Default value: ''


Top ↑

Return

(void|string) Void if 'echo' argument is true, the title attribute if 'echo' is false.


Top ↑

Source

File: wp-includes/post-template.php

function the_title_attribute( $args = '' ) {
	$defaults    = array(
		'before' => '',
		'after'  => '',
		'echo'   => true,
		'post'   => get_post(),
	);
	$parsed_args = wp_parse_args( $args, $defaults );

	$title = get_the_title( $parsed_args['post'] );

	if ( strlen( $title ) == 0 ) {
		return;
	}

	$title = $parsed_args['before'] . $title . $parsed_args['after'];
	$title = esc_attr( strip_tags( $title ) );

	if ( $parsed_args['echo'] ) {
		echo $title;
	} else {
		return $title;
	}
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.