0

I am trying to add a menu item programmatically through a plugin to a menu based on location and i want this to be added to the location that is attached to the homepage's menu.

This is my code:

//Add user menu item
add_filter( 'wp_nav_menu_items', 'add_user_link', 10, 2);

/**
* Add a login link to the members navigation
*/
 function add_user_link( $items, $args ){
if($args->theme_location == 'menu' )
{

        $items .= '<li><a href="'. site_url() .'/user'.'">'.__("User Control","user-control").'</a></li>';

}

return $items;
}

which i want to replace the menu value with the homepage menu location, so is there any way to know to which location the homepage menu is assigned.

  • There's no standard for naming or identifying theme menus in WordPress. The first location in the global $_wp_registered_nav_menus variable might be the primary navigation, but you can't guarantee that because themes can register menus however they like and in whatever order they like. If you want to selectively add something to a particular menu you'll probably need to let the user select which menu is their primary menu. – Jacob Peattie Oct 5 '17 at 13:51
  • I thought that I may get the id of home page and get all registered menus and loop through them to find which one has a menu item with this id – Mohamed Omar Oct 5 '17 at 15:03
1

You can use get_nav_menu_locations() to get an array of locations that are being used (a menu has been assigned to it), it shows like this:

Array
(
    [header-menu] => 4
    [extra-menu] => 6
)

the 4 and 6 are the menu IDs, i think you already have an ID so this should be enough, now the thing is, if there is no menu location being used or there is no location at all the array will show empty, that doesnt mean a menu isnt being shown, because in the theme there could be a wp_nav_menu( array( 'theme_location' => 'whatever' ) ) being used and Wordpress will:

By default, WordPress displays the first non-empty menu when the specified menu or location is not found

in that case you can get a list of menus using get_terms( 'nav_menu' ), it will show an array like this:

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 6
            [name] => Another Menu
            [slug] => another-menu
            [term_group] => 0
            [term_taxonomy_id] => 6
            [taxonomy] => nav_menu
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

    [1] => WP_Term Object
        (
            [term_id] => 4
            [name] => Main Navigation Menu
            [slug] => main-navigation-menu
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => nav_menu
            [description] => 
            [parent] => 0
            [count] => 12
            [filter] => raw
        )

)

you can check which one is the non-empty menu checking the [count] value, or you can find the first empty one and add your menu item since i see that is your objective.

Here is a code to loop all menu items of all menus:

$menus = get_terms('nav_menu');

foreach ($menus as $menu) {//we loop all menus
    $menu_id = $menu->term_id;//we get the ID

    $menu_items_array = wp_get_nav_menu_items($menu_id);//we get the menu

    foreach ($menu_items_array as $menu_item) {//we loop all the menu items of the menu
        if($menu_item->post_name == "home") { //you can add more conditions here to check if its the home link


            //THIS IS THE HOME MENU ITEM

            echo "<pre>";
            print_r($menu_item);//here a list of values that you can use 
            echo "</pre>";
        }
    }
}
  • I am trying to do so but i want to search which menu has a menu item of home the page_for_posts – Mohamed Omar Oct 5 '17 at 16:05
  • you mean to loop the menu items and search which one has the HomePage as the link? – David Lee Oct 5 '17 at 16:07
  • yes because wordpress has a home menu item and i want to search which one of menus has this link added – Mohamed Omar Oct 5 '17 at 16:09
  • i updated my answer – David Lee Oct 5 '17 at 16:45
  • 1
    yeah i noticed that too, so i commented in the code where you can add more conditions like the link to homepage like you say, also i added the list of the values print_r($menu_item); that you can use to search for this home page menu item. – David Lee Oct 6 '17 at 12:36
0

actually the way i was thinking is not all correct but @David Lee posted a solution according to this way and it worked but what if the post name changed to another name, then this method won't work, so i though to do the job according to the item that has a link to the homepage which will be constant, and here is what i have done

//Add user menu item
add_filter( 'wp_nav_menu_items', 'add_user_link', 10, 2);

/**
 * Add a login link to the members navigation
 */
function add_user_link( $items, $args )
{
    $locs = get_nav_menu_locations();           
    $homeitemlocs = array();
    foreach($locs as $loc => $v){
    $locmenu = wp_get_nav_menu_object( $v );
    $locitems = wp_get_nav_menu_items($locmenu->term_id);
        foreach($locitems as $locitem){
            if($locitem->url == get_bloginfo('url').'/' ){
                $homeitemlocs[]= $loc;      
            }
        }
    }
    foreach ($homeitemlocs as $homeitemloc){

        if($args->theme_location == $homeitemloc )
        {

                $items .= '<li><a href="'. site_url().'/user'.'">'.__("User Control","user-control").'</a></li>';

        }

    }

    return $items;
}

With that my custom link will be added to any menu that has an item linked to homepage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.