Home / Widgets / Admin Menu Editor plugin – Open Custom URL in New Tab (i.e. target=”_blank”)
Duplicate Snippet

Embed Snippet on Your Site

Admin Menu Editor plugin – Open Custom URL in New Tab (i.e. target=”_blank”)

This code Snippet was initially created to be used with the Admin Menu Editor plugin (https://wordpress.org/plugins/admin-menu-editor/), by enhancing the Custom URL feature in that plugin.
I'm also using Jetpack Boost (https://jetpack.com/boost/), which gets its rating scores from PageSpeed Insights - but Boost doesn't tell you the details. By providing a link in the sidebar menu directly to PageSpeed Insights you can then take action to improve your score, and this Code Snippet allows for that page to open in a new tab. While I had a specific use in mind, this code can be extended to ANY new Admin menu button you create if you want that button to launch a page in a new tab.
Enjoy!

Code Preview
php
<?php
<?php
/**
 * Opens specific WordPress admin menu items in a new tab.
 * Handles both top-level items (by ID) and submenu items (by parent ID and text).
 /**
 * HOW TO USE:
 *
 * 1. Import the code Snippet.
 * 2. **IMPORTANT: CONFIGURE THE TARGETS (Choose ONE method):**
 *
 * **Method 1: Target a TOP-LEVEL Menu Item**
 * - Find the unique ID of the top-level menu item's <li> element.
 * - Replace the empty string in `var topLevelTargetId = '';` with that ID.
 * - Ensure `var submenuParentId = '';` and `var submenuTargetText = '';` remain empty.
 *
 * **Method 2: Target a SUBMENU Item**
 * - Find the unique ID of the top-level parent menu item's <li> element.
 * - Replace the empty string in `var submenuParentId = '';` with that ID.
 * - Find the exact text content of the submenu item you want to open in a new tab.
 * - Replace the empty string in `var submenuTargetText = '';` with that text.
 * - Ensure `var topLevelTargetId = '';` remains empty.
 *
 * 3. Set the snippet to run on "Admin Area Only".
 * 4. Save and activate the snippet.
 * 5. Clear any WordPress caching and refresh your admin page.
 * 6. Test to ensure it's working.
 **/
function open_specific_admin_menu_new_tab_combined() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            // *** CONFIGURATION STARTS HERE ***
            // --- Target a TOP-LEVEL menu item (by ID) ---
            var topLevelTargetId = ''; // Replace with the unique ID of your top-level menu item, if applicable.
            // --- Target a SUBMENU item (by parent ID and text content) ---
            var submenuParentId = '';    // Replace with the unique ID of the top-level parent menu of the submenu item.
            var submenuTargetText = '';  // Replace with the exact text content of the submenu item you want to target.
            // *** CONFIGURATION ENDs HERE ***
            // --- JavaScript Logic ---
            // Target a top-level menu item by its unique ID
            if (topLevelTargetId) {
                $('li#' + topLevelTargetId + ' > a').attr('target', '_blank');
            }
            // Target a submenu item by its parent ID and text content
            if (submenuParentId && submenuTargetText) {
                var submenu = $('li#' + submenuParentId + ' ul.wp-submenu');
                if (submenu.length > 0) {
                    submenu.find('a').each(function() {
                        if ($(this).text().trim() === submenuTargetText) {
                            $(this).attr('target', '_blank');
                            return false; // Stop searching
                        }
                    });
                }
            }
        });
    </script>
    <?php
}
add_action( 'admin_footer', 'open_specific_admin_menu_new_tab_combined' );

Comments

Add a Comment