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] = "https://www.example.com";
}

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

I learned how to do this from here: http://www.techedg.com/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?

  • 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 Oct 6 '15 at 6:19
  • @MarkKaplun Right but sometimes people add menu links for support topics/forum. Most of theme clubs do that. – Robert hue Oct 6 '15 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 Oct 6 '15 at 7:29
1

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.com. 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.com']" ).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!

0

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.

  • this solution will open all external links in new tabs. – Mark Kaplun Oct 6 '15 at 6:21
  • @MarkKaplun I tested this before posting, so I am sure this works only for external URLs. Kindly confirm. – Prasad Nevase Oct 6 '15 at 6:24
  • yes it works, but my point is that it will work for all links not just the menu one. It might be something that the OP will want, but the question is only about the menu. Or did I miss something? – Mark Kaplun Oct 6 '15 at 6:25
  • Yes, it will work for all external links and not for menu one. I did mention at the end of my solution that all external link would open in new tab. I think you missed that part :-) – Prasad Nevase Oct 6 '15 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.