search

Home  >  Q&A  >  body text

Restricting the menu to certain users: a way to filter based on the value of a specific field

In my main wp navbar I have a menu item called 'Research' that I only want to show up in researchers in WordPress.

Researchers will be defined by a user metadata field called wpum_relationship_to_lib, which is a multi-select field that includes options for researcher, student, employee, etc.

It is important that researcher must be one of the options the user chooses to access this menu, and wpum_relationship_to_lib does not define a WordPress role.

All menus are main menus. Also, I need to hide the menu until I log in. See my code which is not limiting the menu correctly.

function restrict_menu_to_researchers($items, $args) {
  
  // 检查菜单是否分配给所需位置
  if ($args->theme_location === 'primary') {

    // 检查用户是否已登录
    if (is_user_logged_in()) {

      $user_id = get_current_user_id();
      $relationship_values = get_user_meta($user_id, 'wpum_relationship_to_lib', true);
            
      // 检查用户是否为“researcher”
      if (is_array($relationship_values) && in_array('researcher', $relationship_values)) {

        // 允许研究人员访问菜单
        return $items; 

      } else {
        foreach ($items as $key => $item) {
          if ($item->title == 'Research') {
            // 隐藏非研究人员的“Research”菜单
            unset($items[$key]); 
          }
        } 
      }
    } else {
      foreach ($items as $key => $item) {
        if ($item->title == 'Research') {
          // 隐藏未登录用户的“Research”菜单
          unset($items[$key]);
        }
      }
    }
  }
  return $items;
}
add_filter('wp_nav_menu_objects', 'restrict_menu_to_researchers', 10, 2);

P粉563831052P粉563831052442 days ago488

reply all(2)I'll reply

  • P粉578680675

    P粉5786806752023-09-08 13:32:38

    The code provided is mostly correct. However, there is a small problem in the conditional statement when checking the user relationship value. I updated the code to ensure that the "Research" menu item only appears for users who have selected "researcher" as one of their relationship values ​​in the wpum_relationship_to_lib meta field. It also hides the Research menu for non-logged-in users. I haven't tested the code, so any comments are welcome. #joshmoto's code is valid, but in the foreach loop, the condition !in_array($menu_object->title, $relationship_array) checks if the menu object title is not in the relationship array. From what I understand from the question, you want to check if the value "researcher" is in the relational array. Therefore, you should update the condition to in_array('researcher', $relationship_array).

    function restrict_menu_to_researchers($items, $args) {
      // 检查菜单是否分配给所需位置
      if ($args->theme_location === 'primary') {
        // 检查用户是否已登录
        if (is_user_logged_in()) {
          $user_id = get_current_user_id();
          $relationship_values = get_user_meta($user_id, 'wpum_relationship_to_lib', true);
          
          // 检查用户是否选择了“researcher”作为其关系值之一
          if (is_array($relationship_values) && in_array('researcher', $relationship_values, true)) {
            // 允许研究人员使用菜单
            return $items;
          } else {
            // 隐藏非研究人员的“Research”菜单
            foreach ($items as $key => $item) {
              if ($item->title === 'Research') {
                unset($items[$key]);
              }
            }
          }
        } else {
          // 隐藏非登录用户的“Research”菜单
          foreach ($items as $key => $item) {
            if ($item->title === 'Research') {
              unset($items[$key]);
            }
          }
        }
      }
      return $items;
    }
    add_filter('wp_nav_menu_objects', 'restrict_menu_to_researchers', 10, 2);

    reply
    0
  • P粉799885311

    P粉7998853112023-09-08 13:31:29

    I got the answer. The interesting thing is that it is case sensitive. I just need to use the following code. But on the test platform, it works without this line of code. I didn't even know this could be for lowercase letters:

    (is_array($relationship_values) && in_array('researcher', array_map('strtolower', $relationship_values))) { ...

    Thank you everyone for your help and time. –

    reply
    0
  • Cancelreply