admin管理员组文章数量:1325332
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
3 Answers
Reset to default 3You 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.
本文标签: pluginsMaking menu link open in new tab
版权声明:本文标题:plugins - Making menu link open in new tab? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742196523a2431180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论