Meta links with Menubar 4.9
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!
I’m trying to use the php option but the menu item never shows up. I’ve tries simplifying it to this:
return array(‘a’,'b’); /* if (!current_user_can( ‘manage_options’ )) return array(‘aaa’, ‘http://www.mysimplehomegarden.com/garden/‘); else return false;*/
obviously the stuff in comments is what I want to use, but the stuff outside of the comments doesn’t work either. Am I doing something wrong? All my other menu items work.
Hi Tom,
your simple PHP menu item should work fine, and I can’t understand why it doesn’t. If you wish, I could try and debug the issue on your site.
sure. email me. I created an account for you to ssh in to the box if you need. I will also create an account for you on wordpress to do administration stuff
I don’t see a case: PHP in function wpm_output. Should there be one?
I finally got it working. I don’t know if just magically started working or if it was because I put
case ‘PHP’:
list($output, $url) = eval($item->selection);
break;
in function wpm_menu.
BTW, my system was upgraded from 3.x (don’t know the exact version.) I moved the old menubar directory out of the plugins directory and unziped a fresh 4.9 but it didn’t fix the problem.
Hi Tom,
I forgot to mention that the PHP type works only with the latest version of the Menubar templates, but you found the way to support older versions too. I’ll add your code to the next Menubar release. Thank you!
So my mistake was that I didn’t copy the templates over to the menubar-template directory?
When you copy the new template versions to the menubar-templates directory you also lose your template customizations.
So if you didn’t customize your templates you can safely upgrade them, otherwise you need to reapply your customizations to the new template versions.
So it’s not always a mistake to keep the old templates, sometimes that could be a very reasonable choice.
@andrea
Hi Andrea,
When I used the code from Tom Baleno, it did not use the generated name. Instead, I had to use this:
case ‘PHP’:
list($name, $url) = eval($item->selection);
break;
Hi Mac,
yes, your version takes care of the label generation too, and probably Tom didn’t need it. By the way, I forgot to add this bug fix to Menubar 4.10, so I’ll add it to the next version.
Thank you for your feedback!
it would be great if there were a way to return a multi-dimensional array – to create automatic drop down menus
That’s a good idea, I’ll think about that!
@andrea
any chance we’ll see this sometime soon?
@yourmanstan
Unfortunately I’ve not had the time to work on that feature yet, but I have not lost all hope!
@andrea
i may be able to help if you get it started or point me in the right direction i’d be happy to contribute code
Thank you, but before starting the implementation I need the right idea, and that’s the hard part!
here’s what i would see happening. instead of accepting a 2 item array as you do now, make it 2 items, with optional third item.
returnarray ('Custom Pages Menu', "custompages.html",
array(array("Item 1", "item1.html"),
array("Item 2", "item2.html"),
array("Item 3","item3.html",
array("Third Level", "item4.html")
),
array("Item 5", "item5.html")
),
array("Item 6", "item6.html")
);
that code would give something like this
-Custom Pages Menu
–Item 1
–Item 2
–Item 3
—Item 4
–Item 5
-Item 6
actually there is an error in my code there, but i don’t have the ability to edit it.
here is what i mean more clearly:
http://www.pastie.org/2173217