WordPress.org

Ready to get started?Download WordPress

Forums

Filter on nav_menu_item_id is not working (2 posts)

  1. spstieng
    Member
    Posted 2 years ago #

    I'm using Members plugin created by Justin Tadlock.
    This plugin allows me to restric specific pages based on user role.

    This works fine. The only problem is that the pages are still shown in my menu.

    I've added the following code to the content-permission.php file in the Members plugin:

    add_filter( 'nav_menu_item_id', 'members_content_permissions_protect' );
    
        function members_page_permissions_protect($id) {
    
          /* Remove 'menu-item-' from 'menu-item-172' and only keep menu ID */
          $menu_id = preg_replace("/[^0-9]/", '', $id);
    
          /* Get page ID based on menu ID */
          $page_id = get_post_meta( $menu_id, '_menu_item_object_id', true );
    
          /* If the current user can view the page, return menu ID. */
          if ( members_can_current_user_view_post( $page_id ) )
            return $id;
        }

    The function members_can_current_user_view_post works as it should (after a tiny tweek), but nothing is done to the menu. I've also tried returning 'true/false'.

    Why isn't this filter working?

  2. spstieng
    Member
    Posted 2 years ago #

    I've finally found a solution, but I'm not sure if it's the best one.
    The solution is based on this code: http://wordpress.stackexchange.com/questions/14037/menu-items-description/14039#14039

    [Code moderated as per the Forum Rules. Please use the pastebin]

Topic Closed

This topic has been closed to new replies.

About this Topic