I have a custom menu from wordpress > Appearance > Menus and bind submenus from db using wp query. The issue i am facing that the submenus are binding in Default Languange (English) but when i translate site in Hebrew Language then submenus are not binding. I checked code and found that wp_get_nav_menu_items filter is not working in hebrew language.
Here is my code :
$menu_name = 'Main Navigation';
$name_of_menu_item_to_append_to = 'Where can I use it';
$id_of_menu_item_to_append_to = get_wp_object_id( $name_of_menu_item_to_append_to, 'nav_menu_item' );
global $wpdb;
$get_continents = $wpdb->get_results
(
"SELECT * FROM ".$wpdb->prefix . "continents ORDER BY continent_id ASC"
);
if(count($get_continents) > 0)
{
foreach ($get_continents as $continent)
{
$new_submenu_item = array(
'text' => $continent->continent_name,
'url' => '#'
);
add_subitems_to_menu($menu_name,$id_of_menu_item_to_append_to,array( $new_submenu_item ),$continent->continent_id);
}
}
function add_subitems_to_menu( $menu_name, $parent_object_id, $subitems, $continent_id ) {
if ( is_admin() ) {
return;
}
// Use wp_get_nav_menu_items filter, is used by Timber to get WP menu items
add_filter( 'wp_get_nav_menu_items', function( $items, $menu )
use( $menu_name, $parent_object_id, $subitems, $continent_id ) {
// If no menu found, just return the items without adding anything
if ( $menu->name != $menu_name && $menu->slug != $menu_name ) {
return $items;
}
// Find the menu item ID corresponding to the given post/page object ID
// If no post/page found, the subitems won't have any parent (will be on 1st level)
$parent_menu_item_id = 0;
foreach ( $items as $item ) {
if ( $parent_object_id == $item->object_id ) {
$parent_menu_item_id = $item->ID;
break;
}
}
$menu_order = count( $items ) + 1;
foreach ($subitems as $subitem) {
// Create objects containing all (and only) those properties from WP_Post
// used by WP to create a menu item
$items[] = (object) array(
'ID' => $continent_id, // ID that WP won't use
'title' => $subitem['text'],
'url' => $subitem['url'],
'menu_item_parent' => $parent_menu_item_id,
'menu_order' => $menu_order,
'post_name' => '',
// These are not necessary, but PHP warning will be thrown if undefined
'type' => '',
'object' => '',
'object_id' => $continent_id,
'db_id' => $continent_id,
'classes' => '',
);
$menu_order++;
}
return $items;
}, 10, 2);
}
I hope you understands what i am trying to explain.