Do you need to add an item like Log in, Log out, Register, Site Admin to your Menubar menu? Now, with the new PHP item type in Menubar 4.9, you can!
A) Let’s say you wish to add a menu item that lets you logout when you are logged in, and login when you are logged out.
That’s simple. Add to your Menubar menu a new item with type PHP, and enter in the PHP code field the following PHP snippet:
if (is_user_logged_in ())
return array ('Log out', wp_logout_url ());
else
return array ('Log in', wp_login_url ());
That means that, if you are logged in, you’ll get a menu item labeled Log out and pointing to your site logout URL, otherwise you’ll get a menu item labeled Log in and pointing to your site login URL.
B) What if you need a menu item to allow visitors to register to your site? Of course that item should be present only if the visitor is not already logged in.
That’s the content to write into the PHP code field:
if (!is_user_logged_in () && get_option ('users_can_register'))
return array ('Register', site_url ('wp-login.php?action=register', 'login'));
else
return false;
That means that, if the visitor is not logged in and user registration is enabled, you’ll get a menu item labeled Register and pointing to your site signup URL, otherwise you’ll get no menu item at all.
C) Now let’s make a menu item which brings you to the admin dashboard, if you are already logged in.
The code you need to put in the PHP code field is:
if (is_user_logged_in ())
return array ('Site Admin', admin_url ());
else
return false;
That means that, if you are logged in, you’ll get a menu item labeled Site Admin and pointing to your dashboard URL, otherwise you’ll get no menu item at all.
I hope you find these examples useful, and I think they help to show the potential of the PHP item type available in Menubar 4.9. If you find other interesting applications, please share them in the comments!
Recent Comments