Last active
February 11, 2021 18:47
-
-
Save efc/33e3ed199b5c2300458809130ff0af57 to your computer and use it in GitHub Desktop.
Fixes wp-admin links for WPS Hide Login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( is_multisite() ) { | |
/** | |
* Hacks the $wp_admin_bar global in order to correct, | |
* simplify, or hide it. | |
* | |
* Called by action: wp_before_admin_bar_render | |
* | |
* @return void | |
*/ | |
function modify_mysites_menu() { | |
global $wp_admin_bar; | |
$all_toolbar_nodes = $wp_admin_bar->get_nodes(); | |
foreach ( $all_toolbar_nodes as $node ) { | |
if ( preg_match( '/^blog-(\d+)(.*)/', $node->id, $matches ) ) { | |
$blog_id = $matches[1]; | |
if ( $login_slug = get_wps_hide_login_slug( $blog_id ) ) { | |
// since we got a slug, we know that WPS Hide Login is active | |
// and we will modify and simplify the menu links as needed | |
if ( !$matches[2] || '-d' === $matches[2] ) { | |
// this is a link to the site admin for a subsite | |
// so we modify the link to use the WPS Hide Login slug | |
$args = $node; | |
$old_href = $args->href; | |
$args->href = preg_replace( '/wp-admin\/$/', "$login_slug/", $old_href ); | |
if ( $old_href !== $arg->href ) { | |
$wp_admin_bar->add_node( $args ); | |
} | |
} elseif ( strpos( $node->href, '/wp-admin/' ) !== false ) { | |
// we cannot be sure that deep links into the admin side | |
// will work when WPS Hide Login is present, so we | |
// remove these notes to new post and manage comments | |
// which are rarely used anyway | |
$wp_admin_bar->remove_node( $node->id ); | |
} | |
} | |
} | |
} | |
} | |
add_action( 'wp_before_admin_bar_render', 'modify_mysites_menu' ); | |
/** | |
* Returns a login slug if one has been defined | |
* by the WPS Hide Login plugin. | |
* | |
* @param int $blog_id | |
* @return string | |
*/ | |
function get_wps_hide_login_slug( $blog_id ) { | |
if ( defined( 'WPS_HIDE_LOGIN_BASENAME' ) ) { | |
if ( $blog_id ) { | |
if ( $slug = get_blog_option( $blog_id, 'whl_page' ) ) { | |
return $slug; | |
} | |
} else { | |
if ( $slug = get_option( 'whl_page' ) ) { | |
return $slug; | |
} | |
} | |
if ( ( is_multisite() && is_plugin_active_for_network( WPS_HIDE_LOGIN_BASENAME ) && ( $slug = get_site_option( 'whl_page', 'login' ) ) ) ) { | |
return $slug; | |
} | |
return 'login'; | |
} | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment