How to Automatically Add Missing Link Titles

How to Automatically Add Missing Link Titles in WordPress for Better Accessibility and SEO

When developing or customizing WordPress websites, it is common to rely on themes and plugins that generate content dynamically. While this is convenient, it often comes at the cost of accessibility and search engine optimization, especially when these plugins or themes generate <a> tags without a title attribute.

In this article, we will explain why missing title attributes matter, and how you can automatically add them to links in your post content using a custom WordPress function.

Why Link Titles Matter

Accessibility

For users who rely on screen readers, link titles can provide additional context about where a link leads. Although screen readers usually read the visible anchor text, the title attribute can complement that by offering extra information. This can be particularly useful when the link text is vague, such as “click here” or “read more.”

It is important to use title attributes thoughtfully, and not to repeat the anchor text, which could be redundant for screen reader users.

SEO

Search engines analyze links for anchor text and related attributes. While the title attribute is not a direct ranking factor, it can contribute to clarity and semantic meaning. When linking to other posts, adding a descriptive title can reinforce the relevance of the target page and help crawlers better understand site structure.

User Experience

When users hover over a link, the browser can display a tooltip showing the title attribute. This can help reassure users before clicking a link, improving trust and usability.

The Problem with Missing Titles

Many plugins, page builders, and themes generate links without title attributes. This can lead to inconsistent markup, missed opportunities for SEO, and less accessible content. While developers can manually add title attributes, this is rarely feasible on large or dynamic sites.

For this reason, we created a function that automatically adds missing titles to links in post content.

The Code: Automatically Adding Missing Link Titles

Below is the full code snippet. You can add this to your theme’s functions.php file or, ideally, include it in a functional plugin.

defined( 'ABSPATH' ) || exit;
/**
 * Adds the post title as the "title" attribute to links in post content
 * that don't already have one. This improves accessibility and SEO.
 *
 * The function:
 * - Processes the content and finds links missing a title attribute.
 * - Determines the title from the current post or, if needed,
 *   from the target post ID.
 * - Replaces the links with a version that includes the title.
 *
 * @since 1.0.0
 *
 * @param string $content The post content.
 * @return string The modified content with title attributes added.
 */
function eos_add_missing_link_titles_to_links( $content ) {
    $content = do_shortcode( $content );
    $pattern = '/<a\s((?!.*\btitle=)[^>]+)>/i'; // Regex: Find anchor tags WITHOUT a title attribute.
    $title = false;
    if ( function_exists( 'is_main_query' ) && ! is_main_query() && isset( $post ) && isset( $post->post_title ) ) {
        // We are in a secondary loop.
        global $post;
        $title = $post->title;
        if( $title ) {
            $replacement = '<a title="' . esc_attr( $title ) . '">';
            $content = preg_replace( $pattern, $replacement, $content );
            return $content;
        }
    }
    else{
        // We aren't in a loop.
        preg_match_all( $pattern, $content, $matches );
        if( $matches && is_array( $matches[0] ) ) {
            foreach( $matches[0] as $link ) {
                if( false !== strpos( $link, ' href="' ) ) {
                    $arr = explode( ' href="', $link );
                    $url = rtrim( $arr[1], '">' );
                    $post_id = url_to_postid( $url );
                    if( $post_id ) {
                        $title = get_the_title( $post_id );
                        if( $title ) {
                            $new_link = str_replace( ' href="', ' title="' . esc_attr( $title ) . '" href="', $link );
                            $content = str_replace( $link, $new_link, $content );
                        }
                    }
                }
            }
        }
    }
    return $content;
}

if( ! is_admin() ) {
    // Run only on the frontend.
    add_filter( 'the_content', 'eos_add_missing_link_titles_to_links', 9999999999999 ); // Filter post content to automatically add title attributes to links for better accessibility and SEO.
}

If you don’t know how to create a custom functional plugin, you can use our Plugin Builder.

How the Code Works

Processing the Content

The function starts by applying do_shortcode() to the content. This ensures that shortcodes that generate links are expanded before we analyze the HTML.

Finding Links Without a Title

The regular expression searches for anchor tags that do not contain a title attribute. This is crucial to avoid overwriting links that already have meaningful title values.

$pattern = '/<a\s((?!.*\btitle=)[^>]+)>/i';

Handling Secondary Loops

In some situations, such as related posts or widgets, WordPress uses secondary loops. In that case, if a valid post object is available, the function uses its title to inject into the title attribute.

if ( function_exists( 'is_main_query' ) && ! is_main_query() && isset( $post ) && isset( $post->post_title ) )

Fallback: Using url_to_postid()

If we are not in a loop, the function attempts to extract the target URL from each link, convert that URL into a post ID using url_to_postid(), and retrieve the corresponding title. If successful, the title is added as a title attribute in the anchor tag.

This allows the function to work even on static pages or content loaded outside the Loop.

Hooking into WordPress

Finally, the function is hooked into the_content filter. It runs only on the frontend, and at a very high priority number to ensure it processes the content after most other filters.

 
if( ! is_admin() ) {
    // Run only on the frontend.
    add_filter( 'the_content', 'eos_add_missing_link_titles_to_links', 9999999999999 ); // Filter post content to automatically add title attributes to links for better accessibility and SEO.
}

Final Thoughts

Adding title attributes to links can improve accessibility, support search engines, and create a better user experience. While not every screen reader will use them, and while they should never duplicate the visible text, they can still offer valuable context when used properly.

Since many themes and plugins neglect to add them, automating this task helps keep your content clean and more accessible without manual editing.

You are welcome to adapt and extend this code to exclude certain links or handle external URLs, depending on the needs of your site.

Want the easiest way to implement the suggested code on your website? Just download the Add Missing Link Titles plugin.