Adding target attribute to links in WordPress with links_add_target

The links_add_target function is a feature provided by WordPress that modifies the behavior of hyperlinks in your content. It is designed to automatically append a target attribute to all outgoing links in your posts, pages, or text widgets. This function is part of WordPress’s link functionality.

When this function is utilized, it modifies the HTML of your links to open in a new browser window or tab, instead of the current one. This is achieved by adding the target="_blank" attribute to the anchor tags in your content. This function works on the output of your content, meaning it doesn’t alter the actual content stored in your database.

By making use of the links_add_target function, you can improve the user experience on your site by allowing users to keep your website open while they visit the linked pages. This can be particularly useful for websites that feature a lot of external links, such as blogs, news sites, or e-commerce stores.

Parameters Accepted by the links_add_target Function

The links_add_target function in WordPress is designed to accept three parameters. These parameters are:

  • $content (string): This is a required parameter. It represents the string in which the function will search for links.
  • $target (string): This parameter is optional. If not specified, the function will default to ‘_blank’. This parameter represents the target to be added to the links.
  • $tags (string[]): This is also an optional parameter. Its default value is an array containing ‘a’. This parameter represents the array of tags to which the function will apply.

If the function is not provided with any parameters, it will not execute any operation.

Return Value of the links_add_target Function

The links_add_target function processes the content based on the parameters provided and returns the resultant string. This output string is the content that has been processed by the function.

Examples

How to add target attribute to all anchor tags in post content

The following code snippet uses the links_add_target function to add a target attribute to all anchor tags in the post content. This is useful when you want all links in your post to open in a new tab.

function add_target_to_links($content) {
 $content = links_add_target($content);
 return $content;
}
add_filter('the_content', 'add_target_to_links');

How to add target attribute to specific tags in post content

The following code snippet uses the links_add_target function to add a target attribute to specific tags in the post content. This is useful when you want to open only specific links in new tabs.

function add_target_to_links($content) {
 $tags = array('a', 'area');
 $content = links_add_target($content, '_blank', $tags);
 return $content;
}
add_filter('the_content', 'add_target_to_links');

How to add a custom target attribute to all anchor tags in post content

The following code snippet uses the links_add_target function to add a custom target attribute to all anchor tags in the post content. This is useful when you want all links in your post to open in a specific frame.

function add_target_to_links($content) {
 $content = links_add_target($content, '_top');
 return $content;
}
add_filter('the_content', 'add_target_to_links');

Conclusion

The links_add_target function in WordPress has a specific role in modifying the behavior of hyperlinks. By default, all links in WordPress open in the same tab or window. However, if a developer wants to change this behavior and make certain links open in a new tab or window, they can use the links_add_target function. This function automatically adds a target attribute to all links in a certain context, enabling developers to control how their links behave and improving the user experience on their website.

Related WordPress Functions