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.
- The original title that Link Whisper was going to insert in the link. (The autolink keyword)
- The data object that link is going to be inserted in. It can be either a WordPress Post or a Term object.
- 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)