WordPress 3 comes with integrated menu management. Although still a bit quirky at times, it makes creating and managing (navigation) menus a breeze. However, now that creating and displaying a menu takes nothing more than one line of code (wp_nav_menu), we appear to have lost the ability to “manually” add our own stuff.
For instance, by design, there is no “home” link on any menu. Though it is easy to create a custom menu item in the menu management, there is an easier way, using WordPress filters.
The navigation menu can be “filtered”, and so can the navigation menu items. This allows us to add menu items at will.
Inspired by a support request, I did a bit of digging, the result are the following three code snippets, that you can add to your themes functions.php file and will add a login/logout link, add a search field or a Home Page link to your WordPress 3 navigation menu.
- Add a login/logout link to your navigation menu
- Add a search field to your navigation menu
- Add a Home Page link to your navigation menu
Add a login/logout link to your navigation menu
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
}
Explanation:
First we add a function add_login_logout_link to the wp_nav_menu_items filter. Then, the ob_start, ob_get_contents and ob_end_clean (lines 4, 6 and 7) functions are “output Buffering” PHP functions that will “intercept” the information that would otherwise be sent to the browser. wp_loginout('index.php'); will add the logic and html code to login (if not logged in yet), or logout (if logged in). Since we don’t want to send that code to the browser yet, we “capture” the output (using ob_get_contents) in a variable ($searchform), and finally include that variable as a list item in the menu.
Add a search field to your navigation menu
add_filter('wp_nav_menu_items','add_search_box', 10, 2);
function add_search_box($items, $args) {
ob_start();
get_search_form();
$searchform = ob_get_contents();
ob_end_clean();
$items .= '<li>' . $searchform . '</li>';
return $items;
}
Create your own searchform template
The add_search_box function will include the default searchform in the menu bar. This however may not be the desired layout (perhaps it contains preceding text “search:” and a “search” button), so you should create a template file searchform.php in your theme template directory, and add the following code:
<form method="get" class="search-form" id="search-form" action="<?php bloginfo( 'home' ); ?>/">
<div class="formfield">
<input class="formInputText" type="text" name="s" id="search-text" value="Search ..." size="12" maxlength="16" tabindex="1" onfocus="if (this.value == 'Search ...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search ...';}" />
</div>
</form>
Optionally, you can add styling to your form to match your navigation style, using (for example!):
input.formInputText {
margin-top: 7px;
color: #666;
padding: 3px;
background: #ccc;
}
input.formInputText:hover {
cursor: help;
color: 555;
background: #ccc;
}
Add a Home Page link to your navigation menu
add_filter( 'wp_nav_menu_items', 'add_home_link', 10, 2 );
function add_home_link($items, $args) {
if (is_front_page())
$class = 'class="current_page_item"';
else
$class = '';
$homeMenuItem =
'<li ' . $class . '>' .
$args->before .
'<a href="' . home_url( '/' ) . '" title="Home">' .
$args->link_before . 'Home' . $args->link_after .
'</a>' .
$args->after .
'</li>';
$items = $homeMenuItem . $items;
return $items;
}
props to Division by Zero
Add these to a specific menu only
The filter will run for each menu you have defined, so the samples above will be added to each one of those. This may not always be what you want, so to add the desired code to a specific menu location only, add a condition to the code, to make sure the code only executes for a specific menu location:
function add_login_logout_link($items, $args) {
if($args->theme_location == 'Primary') {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= '<li>'. $loginoutlink .'</li>';
}
return $items;
}
Pingback: wp-popular.com » Blog Archive » Enhancing your WordPress 3 navigation menus :: van Weerd
Hi, I’m getting error when I add your login and search functions to the funtions.php
for the login i get the error
Fatal error: Call to undefined function add_filter() in C:\xampp\htdocs\jameswp\wp-includes\functions.php on line 4304
that on the 1st line of your code
and then on the search function I get this error I get the same error
found the answer to my question at
http://www.howtodowebmarketing.com/2010/02/08/fatal-error-call-to-undefined-function-add_filter/
Thanks
Pingback: How to automatically add a search field to your navigation menu
I saw your trick for adding the search form to the nav menu on WPRecipes.com, and noticed right away that it can be simplified a bit.
The login/out link code may be simplified as well:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);function add_login_logout_link($items, $args) {
$loginoutlink = wp_loginout('index.php', false);
$items .= '<li>'. $loginoutlink .'</li>';
return $items;
}
Pingback: How to automatically add a search field to your navigation menu | The best Tutorials
Pingback: Website Design, Web Development, Content Management System, CMS Web Sites, Ecommerce Websites – Broadys Web Design | How to automatically add a search field to your navigation menu
Pingback: Automatically Add Search Field On Wordpress 3.0 Navigation Menu Easily
Pingback: How to Add a Login/Logout button to your Wp Menu | WpCode
Pingback: How To Add Login/Logout link To Your Navigation Menu
Pingback: How To Add WP Login Link to Your Navigation Menu Automatically # WordPress Tricks & Tips
Pingback: Cara Menambah Menu Home pada Menu Navigasi WordPress
Thanks for this info. Is there a way to style the loginout link in the menu? I’ve tried .menu-item-logoutlink and variations but I can’t figure it out. I’m also unable to see how to do it through firebug.
I see you managed
With the loginout link in the navigation, is it possible to also show the “Dashboard” link?
Why not wait till WordPress 3.1 is released: http://vanweerd.com/a-quick-look-at-some-new-features-in-wordpress-3-1/#admin_bar
I forgot about the admin bar,thanks
Pingback: Add login link Automatically to Twenty Ten Menu | Zeaks Blog
Pingback: How To Add a login/logout link to Your Navigation Menu / WordPress Themes, Plugins & Development
Pingback: How to automatically add a search field to your navigation menu | Wordpress Farm
Pingback: Add login link Automatically to Twenty Ten Menu
Pingback: How to add a search field to your navigation menu | Fazreen
Pingback: How To Add Login/Logout link To Your Navigation Menu | Fazreen
Pingback: How to automatically add a search field to your navigation menu - WPInsite
Hi,
Great article!
Having a bit of a problem, though, trying to add a login/out link to a specific menu in Headway. Headway doesn’t use standard menu areas (primary, secondary, etc), so I’m trying to identify based on the menu name using the line
if($args->menu == ‘Account’) {
I have also tried using the menu id ’7′
This isn’t working though
Can you help?
remove the conditional code, and try
add_filter('wp_nav_menu_Account_items', 'add_login_logout_link', 10, 2);Hi,
thx for the tips.
cheers
hi .. i still don’t understand on how to add homepage link into my navigation menu .. just copy all the codes or i must edit it first ? how about the color codes does it mean same with others or i must edit the blue colors? sorry i’m newbie on this..
Follow the instructions here: http://vanweerd.com/enhancing-your-wordpress-3-menus/#add_home
Hoi Ronald.
Quick question. Seems like the logout link is being added the every custom menu I create. I only want it to show up in the nav bar. Can the code easily be tweaked for this to be the case?
Kind regards
Bart
Hi Bart,
check the last paragraph of the article, titled: “Add these to a specific menu only”
Bit embarrassed here, I didn’t see that part.
Anyway I tried everything but somehow I am missing something. My main navigation menu is called ‘Main’ so I suppose the code should look like this:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
if($args->theme_location == 'Main') {
ob_start();
wp_loginout('index.php');
$loginoutlink = ob_get_contents();
ob_end_clean();
$items .= ''. $loginoutlink .'';
}
return $items;
}
I don’t get an error, I just don’t see the link anymore. Confused here
Is that the theme location as can be see in wp-dashboard > Appearance > Menus?
Yup it’s called ‘Main’
Strange…
Pingback: Aggiungere il campo cerca nel menù di navigazione wp » paologironiblog
Pingback: How to Add a Login/Logout Link to Your WordPress Menu | SNS Online
Pingback: wp-coder.net » How to Add a Login/Logout Link to Your WordPress Menu
Pingback: A Free wordpress newsletter » How to Add a Login/Logout Link to Your WordPress Menu
Thanks the login/out code worked. But what about a register/admin sub-menu to it?
HI,
this is great and works well, but we’d like it to redirect to a specific page on our website e.g. /?page_id=12 instead of redirecting to the default WP login. is this possible?
many thanks
you don’t need this code for that. Add a custom menu item via wp-dashboard > Appearance > Menus, and specify the target URL
We’ve tried that but how do we make the same menu link switch between Logout and Login i.e. when logged in it says Logout and when logged out it says login. There seems to only be one option to set the label?
Pingback: Aggiungere il campo cerca nel menù di navigazione wp » paologironiblog