Adding a Menu item and Restricting it per Memberships

Hello! I viewed the topic “Add Menu Item to trigger a shortcode” and found a snippet to add a custom menu item. I would like to restrict this menu item to a certain membership. What snippet format can I use for that??

add_filter(
    'hivepress/v1/menus/user_account',
    function( $template ) {
		
		if(is_user_logged_in()){
			if(hivepress()->get_version('memberships')){
				$membership = \HivePress\Models\Membership::query()->filter(['user' => get_current_user_id()])->get_first();

        		if(!$membership || 549 === $membership->get_plan__id()){
            		$template = hivepress()->helper->merge_trees(
						$template,
						[
							'widget_nav_menu' => [
								'liability_waivers' => [
									'type' => 'content',
								],
							],
						]
					);
        		}
    		}
		}else{
			$template = hivepress()->helper->merge_trees(
				$template,
				[
					'widget_nav_menu' => [
						'liability_waivers' => [
							'type' => 'content',
						],
					],
				]
			);
		}

    return $template;
    },
    1000
);

I tried this, but it does not do the trick.

You want to add a custom menu item to the user account menu with a restriction to show this menu item only if a user has the specific membership plan, do I correctly understand you?

Correct, that’s what I’m trying to achieve!

Please try this PHP snippet

add_filter(
    'hivepress/v1/menus/user_account',
    function( $menu ) {
		if( is_user_logged_in() ){
			if(hivepress()->get_version('memberships')){
				$membership = \HivePress\Models\Membership::query()->filter(['user' => get_current_user_id()])->get_first();

        		if($membership && 174 === $membership->get_plan__id()){
					$menu['items']['custom_item_add_listing'] = [
						'label'  => 'Custom Menu Item',
		    			'url'    => 'custom url',
		    			'_order' => 100,
					];
        		}
    		}
		}

    	return $menu;
    },
    1000
);
1 Like

Thank you, I will try this!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.