Open custom Wordpress admin menu item in external URL - wordpress

I have this code snippet which creates custom Wprdpress admin menu items and they show up nicely. Since these are links to the external URL I would like to open them in the external tab. Can’t figure out how to to that though. Any suggestion would be appreciated.
add_action('admin_menu', 'admin_menu_add_external_links_as_submenu');
function admin_menu_add_external_links_as_submenu() {
global $submenu;
$menu_slug = "externallink"; // used as "key" in menus
$menu_pos = 1; // whatever position you want your menu to appear
// create the top level menu
add_menu_page( 'external_link', 'Tutorials', 'read', $menu_slug, '', '', $menu_pos);
// add the external links to the slug you used when adding the top level menu
$submenu[$menu_slug][] = array('Yahoo', 'manage_options', 'https://www.yahoo.com/');
$submenu[$menu_slug][] = array('Google', 'manage_options', 'https://www.google.com/');
}

To make it work as you expect you have to create the extra jQuery function admin_menu_add_external_links_as_submenu_jquery which will open your submenu items in a new tab. This function is checking for the div's ID which is now set for each of your submenu items newtab and newtab2 and the final result is target: _blank (new window) output. Code goes into your active theme or child theme functions.php file. Tested and works.
add_action('admin_menu', 'admin_menu_add_external_links_as_submenu');
add_action( 'admin_head', 'admin_menu_add_external_links_as_submenu_jquery' );
function admin_menu_add_external_links_as_submenu() {
global $submenu;
$menu_slug = "externallink"; // used as "key" in menus
$menu_pos = 1; // whatever position you want your menu to appear
// create the top level menu
add_menu_page( 'external_link', 'Tutorials', 'read', $menu_slug, '', '', $menu_pos);
// add the external links to the slug you used when adding the top level menu
$submenu[$menu_slug][] = array('<div id="newtab">Yahoo</div>', 'manage_options', 'https://www.yahoo.com/');
$submenu[$menu_slug][] = array('<div id="newtab2">Google</div>', 'manage_options', 'https://www.google.com/');
}
function admin_menu_add_external_links_as_submenu_jquery()
{
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
$('#newtab').parent().attr('target','_blank');
$('#newtab2').parent().attr('target','_blank');
});
</script>
<?php
}

Related

Wordpress: add_submenu_page() creating error

When I try to create a main menu, with this line
add_menu_page("page title","menu name",10,"test-slug");
Wordpress loads just fine and displays the new menu, but when I try to add a submenu,
add_menu_page("page title","menu name",10,"test-slug");
add_submenu_page("test-slug","sub title","sub menu",10,"test-sub-slug");
only an empty page is displayed, right after half a second. Seems like a php-error, but I can't see the error. Does anyone see what I am doing wrong?
Try the code below in your functions.php file. It should work correctly.
add_menu_page(
'Menu Page Title', // page title
'Menu Menu Text', // menu link text
'manage_options', // capability to access the page
'menu_slug', // page URL slug
'menu_callback_function', // callback function to display the content on options page
'dashicons-format-status', // menu icon
2 // priority
);
add_submenu_page(
'menu_slug', // page URL slug
'Sub Menu Title', // page title
'Sub Menu Text', // menu link text
'manage_options', // capability to access the page
'submenu_slug', // page URL slug
'submenu_callback_function', // callback function to display the content on options page
1 // priority
);
function menu_callback_function(){
echo "This is menu page...";
}
function submenu_callback_function(){
echo "This is submenu page...";
}
Problem solved. add_submenu_page() obviously must be called using a hook. Hence, this way it works and will not cause Wordpress to crash:
function dp_menu_item()
{
add_menu_page("page title","menu name","manage_options","mypage.php");
add_submenu_page("edit.php?post_type=cpt_people","sub title","sub menu","manage_options","mysubpage.php");
}
add_action("admin_menu", "dp_menu_item");

WordPress admin menu: custom logout link shows as submenu item instead of menu item

I added a custom logout link to my WP admin menu, but instead of appearing as a top-level menu item, it is displayed as a submenu item (smaller font size, left padding). The link itself works perfectly though. Any ideas how the code can be changed? Thanks!
current admin menu
The code I use is from this thread.
add_action('admin_init', 'text_domain_logout_link');
function text_domain_logout_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
}
Can you try this?:
add_action('admin_menu', 'text_domain_logout_link');
function text_domain_logout_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
}
Tested and works on my wordpress
Updated:
If you want to show it to top-level then use this code:
add_action('admin_menu', 'text_domain_logout_link');
function text_domain_logout_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
// add class
$menu[9999][4] = "menu-top toplevel_page_menu";
// Add Icon
$menu[9999][6] = "dashicons-update";
}
So it will look like this:

WooCommerce Product Gallery: Swap Featured Image with Selected Thumbnail

Woocommerce displays the product gallery on a lightbox or new tab by default. I would like to display the product gallery on the box where the product image is displayed. I tried WooCommerce Dynamic Gallery but the LITE version doesn't allow me to display all the product gallery images on each product. Also, it displays the image on the product description.
Can anyone suggest me a wordpress plugin or coding that can display the product gallery dynamically ? Thanks a lot.
It's possible with some straightforward jquery (see below). You'll also need to override the default lightbox that WooCommerce includes for the gallery.
I believe that it's possible to disable it from the admin under WC -> Settings -> Products -> Display. But you can also remove the styles and scripts directly.
I haven't tested this code on a fresh install of WooCommerce--this is some modified code from a site with a heavily customized product gallery that includes video support.
You'll need to include this in your theme's functons.php file, etc. etc.
add_action( 'wp_enqueue_scripts', 'wc_remove_lightboxes', 99 );
/**
* Remove WooCommerce default prettyphoto lightbox
*/
function wc_remove_lightboxes() {
// Styles
wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
// Scripts
wp_dequeue_script( 'prettyPhoto' );
wp_dequeue_script( 'prettyPhoto-init' );
wp_dequeue_script( 'fancybox' );
wp_dequeue_script( 'enable-lightbox' );
}
/* Customize Product Gallery */
/**
* Click on thumbnail to view image for single product page gallery. Includes
* responsive image support using 'srcset' attribute introduced in WP 4.4
* #link https://make.wordpress.org/core/2015/11/10/responsive-images-in-wordpress-4-4/
*/
add_action( 'wp_footer', 'wc_gallery_override' );
function wc_gallery_override()
{
// Only include if we're on a single product page.
if (is_product()) {
?>
<script type="text/javascript">
( function( $ ) {
// Override default behavior
$('.woocommerce-main-image').on('click', function( event ) {
event.preventDefault();
});
// Find the individual thumbnail images
var thumblink = $( '.thumbnails .zoom' );
// Add our active class to the first thumb which will already be displayed
//on page load.
thumblink.first().addClass('active');
thumblink.on( 'click', function( event ) {
// Override default behavior on click.
event.preventDefault();
// We'll generate all our attributes for the new main
// image from the thumbnail.
var thumb = $(this).find('img');
// The new main image url is formed from the thumbnail src by removing
// the dimensions appended to the file name.
var photo_fullsize = thumb.attr('src').replace('-300x300','');
// srcset attributes are associated with thumbnail img. We'll need to also change them.
var photo_srcset = thumb.attr('srcset');
// Retrieve alt attribute for use in main image.
var alt = thumb.attr('alt');
// If the selected thumb already has the .active class do nothing.
if ($(this).hasClass('active')) {
return false;
} else {
// Remove .active class from previously selected thumb.
thumblink.removeClass('active');
// Add .active class to new thumb.
$(this).addClass('active');
// Fadeout main image and replace various attributes with those defined above. Once the image is loaded we'll make it visible.
$('.woocommerce-main-image img').css( 'opacity', '0' ).attr('src', photo_fullsize).attr('srcset', photo_srcset).attr('alt', alt).load(function() {
$(this).animate({ opacity: 1 });
});
return false;
}
});
} )( jQuery );
</script>
<?php
}
}

Adding a new BuddyPress menu item

add_action( 'bp_setup_nav', 'test_board', 100 );
function test_board() {
global $bp;
bp_core_new_nav_item( array(
'name' => 'Test Board',
'slug' => 'test-board',
'screen_function' => 'bpis_profile',
'position' => 10
)
);
}
function bpis_profile () {
echo do_shortcode('[bpis_tags]');
echo do_shortcode('[bpis_images]');
}
The issue is that when I click on this link in the BP nav bar, it outputs the shortcodes (as per the bpis_profile function) outside of any divs (meaning it just appears at the top of the website, outside of the theme). In addition, the nav bar disappears and I receive the “About” page of the user I am currently viewing (which shows subscribed forum topics, favorite forum topics, etc).
Is there any workaround for this? Ideally, I’d like my shortcodes to output in the body region, beneath the BP nav bar (which has currently disappeared).
Thank you!
Try doing this:
function bpis_profile() {
bp_core_load_template( 'buddypress/members/single/posts' );
}
Then you can create a new file under buddypress/members/single/ called posts.php. Inside that file you can use get_header and get_footer to output your whole page. Of course, be sure to include your shortcodes too.

How do you add a WordPress admin page without adding it to the menu?

I'm building a WordPress plugin and I'd like to have an edit-item page that can't be reached via the submenu (because then the item wouldn't be specified).
This resource (http://codex.wordpress.org/Adding_Administration_Menus) shows how to associate an admin page with a function, but not how to do so without adding it as a menu item.
Can this be done?
Thanks!
Best solution here http://wordpress.org/support/topic/add-backend-page-without-menu-item
use add_submenu_page with parent slug = null
I have finally discovered a way to do this that isn't an ugly hack, doesn't require JS to highlight the desired menu item (and submenu item), and works for regular menus registered by plugins (#Josh's answer only works for custom post types).
Essentially, you just need to register your submenu normally, but then hook into the 'submenu_file' filter to deregister it and optionally also set another submenu item to highlight instead.
function so3902760_wp_admin_menu() {
// Register the parent menu.
add_menu_page(
__( 'Parent title', 'textdomain' )
, __( 'Parent', 'textdomain' )
, 'manage_options'
, 'my_parent_slug'
, 'display_my_menu'
);
// Register the hidden submenu.
add_submenu_page(
'my_parent_slug' // Use the parent slug as usual.
, __( 'Page title', 'textdomain' )
, ''
, 'manage_options'
, 'my_hidden_submenu'
, 'display_my_submenu'
);
}
add_action( 'admin_menu', 'so3902760_wp_admin_menu' );
function so3902760_wp_admin_submenu_filter( $submenu_file ) {
global $plugin_page;
$hidden_submenus = array(
'my_hidden_submenu' => true,
);
// Select another submenu item to highlight (optional).
if ( $plugin_page && isset( $hidden_submenus[ $plugin_page ] ) ) {
$submenu_file = 'submenu_to_highlight';
}
// Hide the submenu.
foreach ( $hidden_submenus as $submenu => $unused ) {
remove_submenu_page( 'my_parent_slug', $submenu );
}
return $submenu_file;
}
add_filter( 'submenu_file', 'so3902760_wp_admin_submenu_filter' );
Yes, this can be done (well, technically, it would be more like registering the whole thing and then removing the menu item later), but It would just be easiest (I think) to check for parameters in the $_GET super-global to indicate that the user wishes to edit a specific item.
For example, you could have a page that lists items to edit, and clicking 'edit' only adds the item's ID to the current URL(query-string).
In the function that displays this page, if ID is defined, give them the page to edit that item.
Otherwise, give them the list view. That's how posts, pages, and other custom post types do it.
add_submenu_page with parent slug = null
OR
add_submenu_page with menu title = null
use this code for creating new page without adding in menu
add_action( 'admin_menu', 'register_newpage' );
function register_newpage(){
add_menu_page($appname, $appname, 'administrator','custompage', 'custom');
remove_menu_page('custom');
}
function custom()
{
echo "hai";
}
Note: This solution doesn't automatically set the current menu and submenu item. If you want to highlight a particular menu as current when the hidden page is viewed, see my other answer.
From the answers that come before me, you can see that there are many ways to do this. However, there is another way that I think may be the best.
Loading the page differently based on the value of a $_GET query var is one option, but it may not be what some people are looking for.
The suggestions regarding add_submenu_page() are on the right track, but each of the previous suggestions have problems. Setting $menu_title to null doesn't keep the menu item from being displayed, it just makes it so the link doesn't have any text. The link still takes up some room in the menu though, so it looks funny. Setting the $parent_slug to null doesn't have this problem, but I noticed that the page's HTML title doesn't display the $page_title text.
My solution was to set $parent_slug to a fake menu slug, like 'i_dont_exist'. The menu item won't be displayed, and when viewing the admin screen the page title will be filled out properly.
add_submenu_page(
'_doesnt_exist'
,__( 'Page title', 'textdomain' )
,''
,'manage_options'
,'menu_slug'
,'display_my_menu'
);
Yes. It is very possible to make a page cannot be reach via submenu, or even the main menu in the WP admin panel. See the code snippet below.
function myplugin_render_edit_page() {
// Code contains the UI for edit page.
}
/**
* Manage menu items and pages.
*/
function myplugin_register_admin_page() {
global $_registered_pages;
$menu_slug = plugin_basename('myplugin.php');
$hookname = get_plugin_page_hookname($menu_slug,'');
if (!empty($hookname)) {
add_action($hookname, 'myplugin_render_edit_page');
}
$_registered_pages[$hookname] = true;
}
add_action('admin_menu', 'myplugin_register_admin_page');
Hopefully, this will help.
Create sub menu page and parent slug leave it empty like this:
// Create page were you can add new users.
public function add_create_user_menu() {
add_submenu_page(
'',
'Create User',
'Create User',
'manage_options',
'create-user',
array( $this, 'add_create_user_page' )
);
}
You can access it like this:
Add New
I've tried all of the suggestions here but with various issues associated with each.
The WordPress codex for add_submenu_page now gives the correct answer, which is to use options.php as your parent slug. I tried the trick of using a made up name but that gives permissions errors, equally use of null at various locations either causes the menu text to simply be missing (but still clickable) or for the browser title to go missing.
Using options.php worked and I've not seen any issues as a result of its use.
Using add_submenu_page with a parent of NULL definitely works, however if you want to keep the non-linked page associated with a particular menu (say a custom post type menu), you have to use a variation of #Boopathi's answer:
function my_hidden_submenu_page(){
//add the submenu page the usual way
add_submenu_page('edit.php?post_type=custom-type', 'My Page Title', 'My Page Title', 'manage_options', 'my-page-slug', 'my_page_callback');
//then remove it
remove_submenu_page('edit.php?post_type=custom-type','my-page-slug');
}
add_action('admin_menu', 'my_hidden_submenu_page');
It looks as though the two actions would cancel each other out, however remove_submenu_page does not unregister the callback function; it merely removes the link.
This way when someone is viewing your non-linked page, the correct navigation menu (our custom post type menu in this example) will still show as active.
One of the problems I found with merely adding null as the parent slug for a sub menu item is that if you're currently viewing that specific page the submenu itself won't display (at least it didn't for me (along with the page title not showing).
What I did was add an empty span element inside the menu title and use jquery to traverse the parent elements and hide it.
It seems this need is still valid for nowadays version.
I am using WordPress 5.3.2 and I used the following methods to remove the parent named menu from the submenu.
add_action( 'admin_menu', 'submenus' );
function submenus()
{
global $submenu;
$parent_slug = 'parent_slug_name';
// remove parent named menu from submenu because it is always the first one in the submenu array, so the offset is 0 and remove just 1
array_splice( $submenu[$parent_slug], 0, 1 ); // with reindex
// or unset( $submenu[$parent_slug][0] ); // without reindex
}
Hope it helps others who want to achieve the same goal.
About php array_splice()
this method could be found in the source of WP function remove_submenu_page() which is available since WP 3.1.0
Edit / Added
Apart from submenu, parent menu could also be updated in similar way.
For parent menu, the global variable is $menu.
example for reference:
add_action( 'admin_menu', array( $this, 'modify_menu_title' ) );
function modify_menu_title() {
global $menu;
$page = 'some-page-slug';
$new_menu_title = 'New Title Name';
foreach( $menu as $key => $value ) {
if( $menu[$key][2] === $page ) {
$menu[$key][0] = $new_menu_title;
}
}
}
I find you can do it by reusing the insert id, like so:
add_menu_page( 'User AS Packages', 'User AS', 'manage_options', 'myplugin/editaspackages.php', 'UserASPackages', '', 8);
add_menu_page( 'User ARP Packages', 'User ARP', 'manage_options', 'myplugin/editarppackages.php', 'UserARPPackages', '', 8);
add_menu_page( 'AS Packages', 'AS Packages', 'manage_options', 'myplugin/ars-s2.php', 'ARPPackages', '', 8);
The last 3 using position 8 and the last one overrides the two before so the two before do not appear.

Resources