# MemberPress: Show Menu Items Only to Certain Memberships

> Hides navigation menu items from people who don't hold a given MemberPress membership, using a CSS class like wpide-mepr-123 on the menu item.

- Type: PHP
- Runs: Frontend only
- Category: [Memberships](https://wpide.com/library/category/memberships/)
- Requires: MemberPress
- Tags: memberpress, menus, access
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/memberpress-menu-items-by-membership/

## Code

```php
if ( ! class_exists( 'MeprUser' ) ) {
    return;
}

add_filter( 'wp_nav_menu_objects', function ( $items ) {
    $member  = is_user_logged_in() ? new MeprUser( get_current_user_id() ) : null;
    $removed = array();

    foreach ( $items as $key => $item ) {
        $wanted = array();

        foreach ( (array) $item->classes as $class ) {
            if ( preg_match( '/^wpide-mepr-(\d+)$/', $class, $m ) ) {
                $wanted[] = (int) $m[1];
            }
        }

        $hide = in_array( (int) $item->menu_item_parent, $removed, true );

        if ( $wanted && ! current_user_can( 'manage_options' ) ) {
            $allowed = false;

            foreach ( $wanted as $membership_id ) {
                if ( $member && $member->is_already_subscribed_to( $membership_id ) ) {
                    $allowed = true;
                    break;
                }
            }

            $hide = $hide || ! $allowed;
        }

        if ( $hide ) {
            $removed[] = (int) $item->ID;
            unset( $items[ $key ] );
        }
    }

    return $items;
} );
```

## Notes

Enable "CSS Classes" under Screen Options in Appearance > Menus, then add wpide-mepr-{membership ID}. Several classes mean "any of these".

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "MemberPress: Show Menu Items Only to Certain Memberships" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
