Home > WordPress Menubar > Meta links with Menubar 4.9

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!

Categories: WordPress Menubar
  1. October 26th, 2010 at 05:21 | #1363

    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.

  2. October 27th, 2010 at 00:05 | #1364

    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.

  3. October 27th, 2010 at 02:10 | #1365

    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

  4. October 27th, 2010 at 06:45 | #1368

    I don’t see a case: PHP in function wpm_output. Should there be one?

  5. October 27th, 2010 at 08:13 | #1369

    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.

  6. October 27th, 2010 at 14:33 | #1370

    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!

  7. October 27th, 2010 at 16:54 | #1372

    So my mistake was that I didn’t copy the templates over to the menubar-template directory?

  8. October 27th, 2010 at 19:05 | #1373

    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.

  9. Mac McDonald
    November 16th, 2010 at 02:43 | #1412

    @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;

  10. November 16th, 2010 at 02:50 | #1413

    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!

  11. yourmanstan
    May 3rd, 2011 at 21:13 | #1745

    it would be great if there were a way to return a multi-dimensional array – to create automatic drop down menus

  12. May 3rd, 2011 at 23:29 | #1746

    That’s a good idea, I’ll think about that!

  13. yourmanstan
    June 3rd, 2011 at 01:21 | #1777

    @andrea
    any chance we’ll see this sometime soon?

  14. June 3rd, 2011 at 23:40 | #1780

    @yourmanstan
    Unfortunately I’ve not had the time to work on that feature yet, but I have not lost all hope!

  15. yourmanstan
    June 6th, 2011 at 19:09 | #1787

    @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

  16. June 11th, 2011 at 14:50 | #1795

    Thank you, but before starting the implementation I need the right idea, and that’s the hard part!

  17. yourmanstan
    July 6th, 2011 at 18:42 | #1836

    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.

    return
    array ('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

  18. yourmanstan
    July 6th, 2011 at 18:49 | #1837

    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

  1. No trackbacks yet.

Spam Protection by WP-SpamFree