[LAST UPDATE]
Using get_nav_menu_locations() I'm able to find all menu locations provided by theme and relative menu assigned.
I'm already able to see if a specific menu contains an item related to a page.
Now the problem is to extract a valid menu item from an istance of WP_Post.
Tried this
$item = array(
"menu-item-title" => $page->post_title,
"menu-item-object" => "page",
"menu-item-type" => "post_type",
"menu-item-object_id" => $page->ID,
"menu-item-parent_id" => "0",
"menu-item-db_id" => "0",
"menu-item-url" => get_permalink( $page->ID )
);
wp_update_nav_menu_item($idmenu, 0, $item);
But the new item causes some error and has no reference to page:
===================
I'm trying to add a page to main menu (frontend).
Here is what I tried:
public function paginaAMenu($page){
$menu_check = menu_page_url($page->slug, 0);
if($menu_check == ""){
$menu = add_menu_page($page->title, $page->title, 'read', $page->slug);
}
}
But nothing happens. Maybe I miss what add_menu_page is for and I would like to know how to achieve my goal.
To be clearer this is what I've to do by dashboard in section Appearence > Menu in order to add standard page link to 'Main Menu':
I'd like to do the exact same thing but programmatically in a plugin that create some shortcodes, create some pages containing these shortcodes and should link these pages in the main menu of the website. I just miss this last thing.
===================
In this specific case menu position provided by theme is Main Menu and menu assigned is Main Menu
I'd like to do it dinamically, As I do in dashboard where I don't need to manually select theme position or menu assigned.
Taking a look at network tab in Chrome developer tools I can see the ajax call that add item to menu:
POST: http://wpsped.svi/wp-admin/admin-ajax.php
action: add-menu-item
menu: 3
menu-settings-column-nonce: 9f6fe75dca
menu-item[-1][menu-item-object-id]: 79
menu-item[-1][menu-item-db-id]: 0
menu-item[-1][menu-item-object]: page
menu-item[-1][menu-item-parent-id]: 0
menu-item[-1][menu-item-type]: post_type
menu-item[-1][menu-item-title]: Accedi
menu-item[-1][menu-item-url]: http://wpsped.svi/accedi/
menu-item[-1][menu-item-target]:
menu-item[-1][menu-item-classes]:
menu-item[-1][menu-item-xfn]:
I hope for a less hackish way to do it that doesn't involve ajax requests.