Home / Widgets / Admin Menu Editor plugin – Open Custom URL in New Tab
Duplicate Snippet

Embed Snippet on Your Site

Admin Menu Editor plugin – Open Custom URL in New Tab

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 to allow the Custom URL to open in a new tab

Use Case: 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
/**
 * 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 ONLY 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**
 * - Using Code Inspection find the unique ID value of the top-level parent menu item's <li> element.
 * - Replace the empty string in `var submenuParentId = '';` with that ID value.
 * - Find the exact TEXT content of the submenu item you want to open in a new tab (i.e. the "Name" assigned to the submenu button - in my case it was 'PageSpeed Insights'.
 * - 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. If yes, feel free to delete this top message block
 **/
function open_specific_admin_menu_new_tab_combined() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
// *** CONFIGURATION STARTS HERE ***
 // --- Method 1: Target a TOP-LEVEL menu item (by ID) ---
            var topLevelTargetId = ''; // Replace with the unique ID of your top-level menu item, if applicable.
 // --- Method 2: 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 ***
        if (topLevelTargetId) {
            $('li#' + topLevelTargetId + '>a').attr('target', '_blank');
        }
        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;
                    }
                });
            }
        }
    });
    </script>
    <?php
}
add_action( 'admin_footer', 'open_specific_admin_menu_new_tab_combined' );

Comments

Add a Comment