How do I use a custom title attribute in Autolink created links?

  1. Home
  2. Knowledge Base
  3. How do I use a custom title attribute in Autolink created links?

How do I use a custom title attribute in Autolink created links?

By default, the Autolinking will use the keyword as the link’s “title” attribute. So when a user hovers the mouse over the link, the “title” bubble that pops up will contain the keyword text.

But sometimes, you want to use a custom title for the created autolinks. In those cases, we have a filter to allow you to adjust the title before it’s inserted in the content. (This only works before the link is created, existing links won’t be updated with new titles.)

The filter is called “wpil_filter_autolink_title”, and it provides 3 arguments.

  1. The original title that Link Whisper was going to insert in the link. (The autolink keyword)
  2. The data object that link is going to be inserted in. It can be either a WordPress Post or a Term object.
  3. The Link Whisper keyword data object that contains all of the autolink’s creation data.

To see it in action, here is how to filter the autolink title so that instead of using the “keyword” as the title, the target post’s title will be used.

add_filter('wpil_filter_autolink_title', 'filter_link_whisper_autolink_titles', 10, 3);
function filter_link_whisper_autolink_titles($title, $wp_object, $keyword_data){
    // if no data type is given, return the default title
    if(empty($wp_object) || !class_exists('Wpil_Post')){
        return $title;
    }

    // if we have autolink data and there is a link provided
    if(!empty($keyword_data) && isset($keyword_data->link)){
        // try getting the target post or term
        $target = Wpil_Post::getPostByLink($keyword_data->link);
        // if there is a post or term and it has a title
        if(!empty($target) && !empty($target->getTitle())){
            // use its title for the destination title
            $title = str_replace(array('[', ']'), array('[', ']'), $target->getTitle()); // encode any brackets too so they don't break the link attribute
        }
    }

    return $title;
}

Since this is custom code, you will need to put it in a code snippet plugin or carefully enter it in the functions.php file of your site’s child theme. (Don’t put it in the parent theme’s functions.php file or it will be overwritten when you update the theme)

Build A Powerful Internal Link Strategy Today

Enter your email & we'll send you 8 tips to build an internal link strategy.

And 3 things you should avoid doing with internal links.

    We won't send you spam. Unsubscribe at any time.

    Was this article helpful?

    Need Support?

    Can't find the answer you're looking for?
    Contact Support

    Get Started with LinkWhisper

    Speed Up the Process of Internal Linking and Help You Rank Better in Google

    Get LinkWhisper Now