Wordpress how to make link open in new tab
There are a few ways to make a link open in a new tab in WordPress:
Method 1: Using the target
attribute
You can add the target
attribute to the link and set its value to _blank
. This will open the link in a new tab.
Example:
<a rel="nofollow" target="_blank" href="https://www.example.com" target="_blank">Link text</a>
Method 2: Using a plugin
There are several plugins available that can help you achieve this, such as:
- Open in New Tab: This plugin adds a button to the WordPress editor that allows you to open links in a new tab.
- Link to New Tab: This plugin adds a checkbox to the link settings that allows you to open links in a new tab.
To install a plugin, go to the WordPress dashboard, click on Plugins > Add New, and search for the plugin you want to install.
Method 3: Using a code snippet
You can add a code snippet to your theme's functions.php file to make all links open in a new tab. Here's an example:
function open_links_in_new_tab() {
return ' target="_blank"';
}
add_filter('the_content', 'open_links_in_new_tab');
This code snippet uses the add_filter
function to add a filter to the the_content
hook, which is used to display the content of a page or post. The open_links_in_new_tab
function returns the target="_blank"
attribute, which will make all links open in a new tab.
Method 4: Using a theme function
If you're using a theme that has a functions.php
file, you can add the following code to make all links open in a new tab:
function theme_links_open_in_new_tab($content) {
return str_replace('<a ', '<a target="_blank" ', $content);
}
add_filter('the_content', 'theme_links_open_in_new_tab');
This code snippet uses the str_replace
function to replace all <a
tags with <a target="_blank"
tags, which will make all links open in a new tab.