the_title_attribute

The timeline below displays how wordpress function the_title_attribute has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Sanitizes the current title when retrieving or displaying.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
 */
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;
    }
}

WordPress Version: 6.1

/**
 * Sanitizes the current title when retrieving or displaying.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
 */
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;
    }
}

WordPress Version: 5.4

/**
 * Sanitize the current title when retrieving or displaying.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return void|string Void if 'echo' argument is true, the title attribute if 'echo' is false.
 */
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;
    }
}

WordPress Version: 5.3

/**
 * Sanitize the current title when retrieving or displaying.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return string|void String when echo is false.
 */
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;
    }
}

WordPress Version: 4.6

/**
 * Sanitize the current title when retrieving or displaying.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return string|void String when echo is false.
 */
function the_title_attribute($args = '')
{
    $defaults = array('before' => '', 'after' => '', 'echo' => true, 'post' => get_post());
    $r = wp_parse_args($args, $defaults);
    $title = get_the_title($r['post']);
    if (strlen($title) == 0) {
        return;
    }
    $title = $r['before'] . $title . $r['after'];
    $title = esc_attr(strip_tags($title));
    if ($r['echo']) {
        echo $title;
    } else {
        return $title;
    }
}

WordPress Version: 4.3

/**
 * Sanitize the current title when retrieving or displaying.
 *
 * Works like {@link 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 {@link
 * esc_attr()} before it is passed to the user or displayed. The default
 * as with {@link the_title()}, is to display the title.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return string|void String when echo is false.
 */
function the_title_attribute($args = '')
{
    $defaults = array('before' => '', 'after' => '', 'echo' => true, 'post' => get_post());
    $r = wp_parse_args($args, $defaults);
    $title = get_the_title($r['post']);
    if (strlen($title) == 0) {
        return;
    }
    $title = $r['before'] . $title . $r['after'];
    $title = esc_attr(strip_tags($title));
    if ($r['echo']) {
        echo $title;
    } else {
        return $title;
    }
}

WordPress Version: 4.0

/**
 * Sanitize the current title when retrieving or displaying.
 *
 * Works like {@link 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 {@link
 * esc_attr()} before it is passed to the user or displayed. The default
 * as with {@link the_title()}, is to display the title.
 *
 * @since 2.3.0
 *
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return string|null Null on failure or display. String when echo is false.
 */
function the_title_attribute($args = '')
{
    $defaults = array('before' => '', 'after' => '', 'echo' => true, 'post' => get_post());
    $r = wp_parse_args($args, $defaults);
    $title = get_the_title($r['post']);
    if (strlen($title) == 0) {
        return;
    }
    $title = $r['before'] . $title . $r['after'];
    $title = esc_attr(strip_tags($title));
    if ($r['echo']) {
        echo $title;
    } else {
        return $title;
    }
}

WordPress Version: 3.7

/**
 * Sanitize the current title when retrieving or displaying.
 *
 * Works like {@link 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 {@link
 * esc_attr()} before it is passed to the user or displayed. The default
 * as with {@link the_title()}, is to display the title.
 *
 * @since 2.3.0
 *
 * @param string|array $args Optional. Override the defaults.
 * @return string|null Null on failure or display. String when echo is false.
 */
function the_title_attribute($args = '')
{
    $defaults = array('before' => '', 'after' => '', 'echo' => true, 'post' => get_post());
    $r = wp_parse_args($args, $defaults);
    extract($r, EXTR_SKIP);
    $title = get_the_title($post);
    if (strlen($title) == 0) {
        return;
    }
    $title = $before . $title . $after;
    $title = esc_attr(strip_tags($title));
    if ($echo) {
        echo $title;
    } else {
        return $title;
    }
}