0

[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:

enter image description here

===================

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':

dashboard interaction

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

menu positions

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.

  • Well, you do miss it a little bit. add_menu_page adds a page to menu in backend... What exactly are you trying to achieve? What do you mean by "main menu"? – Krzysiek Dróżdż Apr 4 '18 at 17:42
  • please take a look at updated question – assistbss Apr 5 '18 at 7:16
  • OK, and what is the name of this menu location? Where is this menu assigned to? – Krzysiek Dróżdż Apr 5 '18 at 7:49
  • Main Menu, Main Menu, provided further information – assistbss Apr 5 '18 at 9:04
0

I asume you want to add a new item to the main menu on a given page:

add_filter( 'wp_nav_menu_items', 'your_custom_menu_item', 10, 2 );
function your_custom_menu_item ( $items, $args ) {
    // is_page() params: Page ID, title, slug, or array of such.
    if (is_page( 'page-slug' ) && $args->theme_location == 'primary') {
        $items .= '<li>Show whatever</li>';
    }
    return $items;
}

Source: http://www.wpbeginner.com/wp-themes/how-to-add-custom-items-to-specific-wordpress-menus/

0

In order to know what are locations provided by theme for menus:

$locations = get_nav_menu_locations();

This command will return an associative array that has for keys the locations and for values the ids of menu assigned to that location.

To know what pages are associated to a menu:

$idmenu = $locations['main-menu'];
$menu = wp_get_nav_menu_object( $idmenu );
$pagesItem = wp_get_nav_menu_items( $menu, ["object"=>"page"] );

In order to generate a link to a page in a menu

    $pageItem = array(
        "menu-item-object-id" => $page->ID,
        "menu-item-object" => "page",
        "menu-item-type" => "post_type",
        "menu-item-title" => $page->post_title,
        "menu-item-url" => get_permalink( $page->ID ),
        "menu-item-status" => "publish",
    );

    wp_update_nav_menu_item($idmenu, 0, $pageItem);

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.