最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Making menu link open in new tab?

programmeradmin1浏览0评论

I'm trying to add a top level menu to the left sidebar of the WordPress admin panel.

Here's the code I currently have:

add_action( 'admin_menu', 'linked_url' );
function linked_url() {
add_menu_page( 'linked_url', 'Menu Title', 'read', 'my_slug', '', 'dashicons-text', 1 );
}

add_action( 'admin_menu' , 'linkedurl_function' );
function linkedurl_function() {
global $menu;
$menu[1][2] = "";
}

This code DOES work and links the menu to an external page ().

I learned how to do this from here: /

However, I can't figure out how to make the external link open in a new tab. I'd prefer than a new tab/window is opened so people don't lose what they already have open in their admin area.

Is there something I need to change or add? Or is it just not possible?

I'm trying to add a top level menu to the left sidebar of the WordPress admin panel.

Here's the code I currently have:

add_action( 'admin_menu', 'linked_url' );
function linked_url() {
add_menu_page( 'linked_url', 'Menu Title', 'read', 'my_slug', '', 'dashicons-text', 1 );
}

add_action( 'admin_menu' , 'linkedurl_function' );
function linkedurl_function() {
global $menu;
$menu[1][2] = "https://www.example";
}

This code DOES work and links the menu to an external page (https://www.example).

I learned how to do this from here: http://www.techedg/2014/09/06/5575/a-simple-way-to-add-an-external-link-to-the-wordpress-admin-menu/

However, I can't figure out how to make the external link open in a new tab. I'd prefer than a new tab/window is opened so people don't lose what they already have open in their admin area.

Is there something I need to change or add? Or is it just not possible?

Share Improve this question asked Oct 6, 2015 at 1:45 JohnJohn 111 silver badge2 bronze badges 3
  • I would say that sending people to outside of the WP admin is wrong UX decision. User of wordpress do not expect new tabs/windows to be opened when clicking a link, especially not in the menu. – Mark Kaplun Commented Oct 6, 2015 at 6:19
  • @MarkKaplun Right but sometimes people add menu links for support topics/forum. Most of theme clubs do that. – Robert hue Commented Oct 6, 2015 at 7:22
  • It is still a bad UX even if many people do it. Why would you want a cluttered admin menu, which is probably already too long for your screen height, with links that you use once a year? and here the OP is talking about a top level menu and not one which is hidden under theme settings or similar. – Mark Kaplun Commented Oct 6, 2015 at 7:29
Add a comment  | 

3 Answers 3

Reset to default 3

You can do that with jQuery. We can open this link in new tab/window by adding target="_blank" attribute dynamically on link which has URL https://www.example. Here is the example function for that.

function wpse_my_custom_script() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready( function($) {
            $( "ul#adminmenu a[href$='https://www.example']" ).attr( 'target', '_blank' );
        });
    </script>
    <?php
}
add_action( 'admin_head', 'wpse_my_custom_script' );

Don't forget to change URL in above code or this will not work.

Tested & working!

In your meta array when adding a menu item just add a "target" attribute like so:

$admin_bar->add_menu( array(
    'id'    => 'download-plugin',
    'title' => 'Download Plugin',
    'href'  => '#',
    'meta'  => array(
        'title' => __('Download Plugin'),     
        'target' => '_blank',      
    ),

));

By this, your newly added admin menu item will open in new tab.

While there is no parameter available in add_menu_page to open external link in new tab, you can achieve that using a jQuery. Please refer below code:


jQuery( document ).ready(function() {
    jQuery('a').each(function() {
       var a = new RegExp('/' + window.location.host + '/');
       if(!a.test(this.href)) {
           jQuery(this).click(function(event) {
               event.preventDefault();
               event.stopPropagation();
               window.open(this.href, '_blank');
           });
       }
    });
});

Create a file named admin-ext-url.js and put above code in that file. Then, in your theme's functions.php file put following code:


add_action( 'admin_enqueue_scripts', 'admin_handle_ext_urls' );
function admin_handle_ext_urls(){
    wp_enqueue_script( 'ext_urls_handler', get_stylesheet_directory_uri() . '/admin-ext-url.js' );
}

Above code will enqueue the admin-ext-url.js file only on the admin side. This should solve your problem. Also, this will work for all external links on the admin side.

发布评论

评论列表(0)

  1. 暂无评论