the_title_attribute

Sanitize the current title when retrieving or displaying.

Description

(string|void) the_title_attribute( (string) $args = '' ); 

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.

Returns (string|void)

String when echo is false.

Parameters (1)

0. $args — Optional. (string) => ''
Title attribute arguments. Optional.

Options

  • before (string) => ''

    Markup to prepend to the title.

  • after (string) => ''

    Markup to append to the title.

  • echo (bool) => true for echo

    Whether to echo or return the title.

array(

    /**
     * Markup to prepend to the title.
     *
     * @type string
     * @default ''
     */
    'before' => '',

    /**
     * Markup to append to the title.
     *
     * @type string
     * @default ''
     */
    'after' => '',

    /**
     * Whether to echo or return the title.
     *
     * @type bool
     * @default true for echo
     */
    'echo' => true for echo
);        


Usage

  1. if ( !function_exists( 'the_title_attribute' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-template.php'; 
  3.  
  4. // Title attribute arguments. Optional. 
  5. $args = array( 
  6. 'before' => '', 
  7. 'after' => '', 
  8. 'echo' => true for echo 
  9. ); 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = the_title_attribute($args); 
  13.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post-template.php  
  1. function the_title_attribute( $args = '' ) { 
  2. $defaults = array( 'before' => '', 'after' => '', 'echo' => true, 'post' => get_post() ); 
  3. $r = wp_parse_args( $args, $defaults ); 
  4.  
  5. $title = get_the_title( $r['post'] ); 
  6.  
  7. if ( strlen( $title ) == 0 ) { 
  8. return; 
  9.  
  10. $title = $r['before'] . $title . $r['after']; 
  11. $title = esc_attr( strip_tags( $title ) ); 
  12.  
  13. if ( $r['echo'] ) { 
  14. echo $title; 
  15. } else { 
  16. return $title;