Skip to content

Instantly share code, notes, and snippets.

@johnmorris
Last active December 15, 2015 10:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save johnmorris/5245822 to your computer and use it in GitHub Desktop.
Save johnmorris/5245822 to your computer and use it in GitHub Desktop.
Hijacking WordPress menus using wp_nav_menu_objects
if ( !class_exists( 'HijackMe' ) ) {
class HijackMe {
public function hijack_menu($objects) {
/**
* If user isn't logged in, we return the link as normal
*/
if ( !is_user_logged_in() ) {
return $objects;
}
/**
* If they are logged in, we search through the objects for items with the
* class wl-login-pop and we change the text and url into a logout link
*/
foreach ( $objects as $k=>$object ) {
if ( in_array( 'wl-login-pop', $object->classes ) ) {
$objects[$k]->title = 'Logout';
$objects[$k]->url = wp_logout_url();
$remove_key = array_search( 'wl-login-pop', $object->classes );
unset($objects[$k]->classes[$remove_key]);
}
}
return $objects;
}
}
}
$hijackme = new HijackMe;
add_filter('wp_nav_menu_objects', array($hijackme, 'hijack_menu'), 10, 2);
@khromov
Copy link

khromov commented Mar 25, 2014

Excellent, thanks for this snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment