Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Forked from johnmorris/gist:5245822
Created July 7, 2014 22:20
Show Gist options
  • Save ivanhoe011/74ad138142884d2c9f16 to your computer and use it in GitHub Desktop.
Save ivanhoe011/74ad138142884d2c9f16 to your computer and use it in GitHub Desktop.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment