Support » Plugin: If Menu - Visibility control for Menus » If condition login for custom user roles

  • Resolved Pavan789

    (@pavan789)


    Hey,

    I found this plugin to be useful, does exactly what it says. However can I extend the functionality to custom defined roles? Like apart from Administrator, Subscriber, Contributor, Blah, Blah, I have created four additional roles using this plugin, for my wordpress website and would like to limit the menu item to those roles as well. Thanks in advance.

    https://wordpress.org/plugins/if-menu/

Viewing 2 replies - 1 through 2 (of 2 total)
  • I would also like to do this. Would appreciate any help.

    So far I know how to add a new option title and where its meant to hook in, but I’m a bit unsure what to put in the conditions field to hide the content if they are a shopkeeper.

    add_filter( 'if_menu_conditions', 'wpb_new_menu_conditions' );
    
    function wpb_new_menu_conditions( $conditions ) {
      $conditions[] = array(
        'name'    =>  'shopkeeper', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return is_post_type_archive();
        }
      );
    
      return $conditions;
    }
    Plugin Author Andrei

    (@andreiigna)

    Hi,

    Please take a look at how the default conditions are added https://github.com/AndreiZzz/if-menu/blob/master/conditions.php conditions for custom roles are added in the same way.

    Example for ‘shopkeeper’:

    add_filter('if_menu_conditions', 'wpb_new_menu_conditions_shopkeeper');
    
    function wpb_new_menu_conditions_shopkeeper($conditions) {
      $conditions[] = array(
        'name'    =>  'Shopkeeper', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          global $current_user;
          return is_user_logged_in() && in_array('shopkeeper', $current_user->roles);
        }
      );
    
      return $conditions;
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘If condition login for custom user roles’ is closed to new replies.