Support » Plugin: If Menu - Visibility control for Menus » Memberpress’ rules don’t work correctly with membership

  • Resolved macemmek

    (@emmek)


    I’m using the latest Memberpress plugin and I want to display a menu item for all logged in users except to those having the given membership, let’s call it “Staff”.

    So, I’ve enabled the following visibility rules:

    Hide if Staff
    AND
    Show if Is logged in

    Unfortunately, after that the menu item disappears for all users (including administrators!).
    It looks like setting “Hide if Membership” expects the user having any other membership, hiding the item otherwise.

Viewing 1 replies (of 1 total)
  • Thread Starter macemmek

    (@emmek)

    Anyway, although the problem with the rules logic expressed above seems real, the membership-based rules are added by Memberpress plugin through the filter, so I’ve taken the path of defining own rules.

    add_filter( 'if_menu_conditions', 'my_mp_if_menu_exclude_membership', 10, 1 );
    /**
     * Add Memberpress-based rules to If Menu plugin based on active product subscriptions.
     *
     */
    function my_mp_if_menu_exclude_membership( $conditions ) {
    	if ( ! class_exists( 'MeprCptModel' ) ) :
    		return $conditions;
    	endif;
    
    	$memberships = MeprCptModel::all( 'MeprProduct' );
    
    	if ( ! empty( $memberships ) ) :
    
    		$user = MeprUtils::get_currentuserinfo();
    		if ( $user === false ) :
    			return $conditions;
    		endif;
    
    		$subs = $user->active_product_subscriptions( 'ids' );
    
    		foreach( $memberships as $m ) :
    			
    			$conditions[] = [
    				'id'        => "my-mp-active-membership-{$m->ID}",
    				'name'      => sprintf( _x( 'Subscribed %s', 'if-menu' , 'my' ), $m->post_title ),
    				'group'     => __( 'Active on Membership', 'memberpress' ),
    				'condition' => function( $item ) use ( $m, $subs ) {
    					return in_array( $m->ID, $subs );
    				}
    			];
    		endforeach;
    	endif;
    
    	return $conditions;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Memberpress’ rules don’t work correctly with membership’ is closed to new replies.