How to make menu link open in new tab wordpress
There are a few ways to make a menu link open in a new tab in WordPress. Here are a few methods:
Method 1: Using the target
attribute
You can add the target
attribute to the link in your menu item. To do this:
- Log in to your WordPress dashboard.
- Go to Appearance > Menus.
- Click on the menu item you want to edit.
- In the "Navigation Label" field, add the following code:
target="_blank"
(without quotes) at the end of the link URL. - Save the changes.
For example, if your menu item URL is http://example.com/about
, you would add target="_blank"
to the end of the URL, like this: http://example.com/about target="_blank"
.
Method 2: Using a plugin
You can use a plugin like "WP Menu Open in New Tab" or "Menu Link Target" to achieve this. These plugins allow you to easily add the target
attribute to your menu links.
- Install and activate the plugin.
- Go to the plugin settings page (usually found under Settings > Menu Open in New Tab or similar).
- Configure the plugin to add the
target
attribute to your menu links. - Save the changes.
Method 3: Using a theme function
If you have a custom theme or a theme that allows you to add custom functions, you can use a theme function to add the target
attribute to your menu links.
- Add the following code to your theme's functions.php file:
function add_target_to_menu_links($items) { foreach ($items as $item) { if (!empty($item->url)) { $item->url.= ' target="_blank"'; } } return $items; } add_filter('wp_nav_menu_items', 'add_target_to_menu_links');
- Save the changes to your functions.php file.
- Go to Appearance > Menus and refresh the page. Your menu links should now open in a new tab.
Note: The above code is just an example and may need to be modified to fit your specific theme and menu setup.