WordPress多站点:获取当前页面active admin_url()并实现后台切换按钮
Hey there! As someone who’s worked with WordPress multisite quite a bit, I can walk you through exactly how to build that site-switching button you need. Let’s break this into two key parts: grabbing the current active admin page URL, and adding the switcher itself.
In WordPress multisite, each subsite has its own admin area, and you’ll need to capture the relative path of the current admin page to replicate it across other sites. Here’s how to do that:
// Capture the current admin page's relative path $current_admin_path = str_replace( get_admin_url(), '', $_SERVER['REQUEST_URI'] ); // Clean up leading slashes to avoid issues $current_admin_path = ltrim( $current_admin_path, '/' );
get_admin_url(): Returns the base admin URL for the current subsite.$_SERVER['REQUEST_URI']: Gives the full URL path of the current page, so we strip out the base admin URL to get just the specific page path (likeedit.phpfor posts, oroptions-general.phpfor settings).
We’ll hook into WordPress’s admin bar to add a dropdown menu that lets you jump to the same admin page on any other multisite site. This keeps the switcher easily accessible across all admin pages.
Add this code to your theme’s functions.php file or a custom plugin:
add_action( 'admin_bar_menu', 'add_multisite_switcher_button', 999 ); function add_multisite_switcher_button( $wp_admin_bar ) { // Only show this switcher to super admins (adjust if you need other roles) if ( ! is_super_admin() ) { return; } // Get the current admin page's relative path (from Part 1) $current_admin_path = str_replace( get_admin_url(), '', $_SERVER['REQUEST_URI'] ); $current_admin_path = ltrim( $current_admin_path, '/' ); // Add the main dropdown button to the admin bar $wp_admin_bar->add_menu( array( 'id' => 'multisite-switcher', 'title' => 'Switch Sites', 'href' => '#', ) ); // Get all sites in your multisite network $sites = get_sites(); foreach ( $sites as $site ) { // Skip the current site so we don't list it if ( get_current_blog_id() === $site->blog_id ) { continue; } // Build the target URL: same admin page on the other site $target_url = get_admin_url( $site->blog_id, $current_admin_path ); // Optional: If the target page doesn't exist on the other site, fall back to the dashboard $dashboard_url = get_admin_url( $site->blog_id ); if ( $target_url === $dashboard_url && ! empty( $current_admin_path ) ) { $target_url = $dashboard_url; } // Add each site as a dropdown item $wp_admin_bar->add_menu( array( 'parent' => 'multisite-switcher', 'id' => 'site-switch-' . $site->blog_id, 'title' => $site->blogname, 'href' => $target_url, ) ); } }
How This Works:
- We use the
admin_bar_menuhook to inject our menu into the top admin bar. - We check for super admin access to make sure only authorized users see the switcher.
- We loop through all sites in the network, generate the URL for the same admin page on each site, and add them as dropdown options.
- The fallback to the dashboard ensures users don’t land on a 404 if the current page (like a plugin-specific page) doesn’t exist on another site.
- Style the Button: Add a bit of CSS to make the switcher stand out. Drop this into your theme’s
style.cssor use the WordPress customizer:#wpadminbar #wp-admin-bar-multisite-switcher > .ab-item { background-color: #007cba; color: #fff; } #wpadminbar #wp-admin-bar-multisite-switcher:hover > .ab-item { background-color: #006ba1; } - Limit Visibility: If you want specific roles to access the switcher, replace
is_super_admin()with a role check likecurrent_user_can( 'manage_network' ).
内容的提问来源于stack exchange,提问作者Roman L.

