How to add new menu items

Hi,

According to the example Menus | Developer Docs i have put this code snippet

<?php
namespace HivePress\Menus;

use HivePress\Helpers as hp;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

class Foo_Bar extends Menu {
	public function __construct( $args = [] ) {
                error_log('menu');
		$args = hp\merge_arrays(
			[
				// Define the menu items.
				'items' => [
					'first_item'  => [
						'label'  => 'First Item',
						'route'  => 'listings_view_page',
						'_order' => 123,
					],

					'second_item' => [
						'label'  => 'Second Item',
						'url'    => 'https://example.com',
						'_order' => 321,
					],
				],
			],
			$args
		);

		parent::__construct( $args );
	}
}

at /includes/class-foo-bar.php however, nothing shows up in the user account menu.

To make sure it is instantiated i have put error_log(‘menu’) at the first line of the constructor, this shows in the log file:
image

Am i missing something?

Hi @JSHBV,

I’m not HivePress staff, just trying to help!

Please use the following example to add custom menu links:

<?php
add_filter(
	'hivepress/v1/menus/user_account',
	function ( $menu ) {
		if ( is_user_logged_in() ) {
			$vendor_id = HivePress\Models\Vendor::query()->filter(
				[
					'user' => get_current_user_id(),
				]
			)->get_first_id();

			if ( $vendor_id ) {
				$menu['items']['vendor_view'] = [
					'label'  => 'Vendor Profile',
					'url'    => hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor_id ] ),
					'_order' => 123,
				];
			}
		}

		return $menu;
	},
	1000
);

I hope this helps!

Cheers,
Chris :victory_hand:

1 Like

Thank you Chris for the answer.

I also managed to do it through the add_filter() method so i am sure your example will also work.

The original question remains though. Why the example of the official documentation does not work?

Hi @JSHBV,

The code I shared above is from the HivePress Snippets re: adding menu items, so this is probably the best way to go about it.

Re: why the documentation version didn’t work - I’m not entirely sure, but did you use a child theme to do this?

If not, you should be.

Cheers,
Chris :victory_hand: