WordPress the_title_attribute() Function

Definition
In WordPress, the_title_attribute() prints the post title in a safe form for attributes. Use it in title="", alt="", or aria-label="".
It removes HTML tags and escapes quotes, so the page HTML cannot break.
Syntax
A few ways to call the_title_attribute().
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
| Call | What it does |
|---|
the_title_attribute() | Print the escaped title. |
the_title_attribute( array( 'before' => 'Read ' ) ) | Add text before the title. |
the_title_attribute( array( 'echo' => false ) ) | Return the title instead of printing. |
the_title_attribute( array( 'post' => 12 ) ) | Use the title of the post with ID 12. |
It takes one array of options. The options are before, after, echo, and post.
Full declaration
the_title_attribute( string|array $args = '' ): string|void
Parameters
| Parameter | Description |
|---|
before | string. Text before the title. Default is empty. |
after | string. Text after the title. Default is empty. |
echo | bool. true prints the title. false returns it. Default is true. |
post | int|WP_Post. The post to use. Default is the current post. |
Return Value
| Return Value | Description |
|---|
| void | When echo is true, the title is printed. |
| string | When echo is false, the title is returned. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Title attribute on a link
Use it for the title of a post link.
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
Read more
</a>
<a href="https://example.com/hello-world/" title="Hello World">
Read more
</a>
How It Works
the_title_attribute() prints the title with quotes escaped.- It sits inside the
title attribute. - The link text is ours.
Example 2 — Add text before
Use before for a clear label.
<a href="<?php the_permalink(); ?>"
aria-label="<?php the_title_attribute( array( 'before' => 'Read more about ' ) ); ?>">
Read more
</a>
<a href="https://example.com/hello-world/"
aria-label="Read more about Hello World">
Read more
</a>
How It Works
- The options go in one array.
before adds text in front of the title.- The
aria-label helps screen reader users.
Example 3 — Return the title
Set echo to false to store the text.
<?php
$label = the_title_attribute( array( 'echo' => false ) );
echo '<img src="cover.jpg" alt="' . $label . '">';
<img src="cover.jpg" alt="Hello World">
How It Works
echo set to false returns the title.- It is already escaped for attributes.
- We place it in the
alt attribute.
Frequently Asked Questions
It prints the post title in an escaped form for use inside an HTML attribute.
the_title() is for page text. the_title_attribute() is for attributes, and it removes tags and escapes quotes.
before, after, echo, and post, all inside one array.
Pass array( 'echo' => false ).
Not if you pass a post in the options.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful