• Resolved lijitimit

    (@lijitimit)


    Hi there, love this plugin!!!

    I’ve added a few custom conditions using the code you supply on the plugin page. The first (Documentation) works fine. The rest end up all being ignored when saving on the dashboard menu screen. For instance, if I save it to “Is 3A Member” or “Is 3A Candidate”, it always defaults to the last item “Is Instructor”. I can see the settings and select them, but when pressing save, it always defaults back to “Is Instructor”.

    // Document
    add_filter( 'if_menu_conditions', 'my_new_menu_condition' );
    
    function my_new_menu_condition( $conditions ) {
      $conditions[] = array(
        'name'    =>  'Single Document', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return is_singular( 'documentation' );
        },
        'group'     =>  'Page type'
      );
    
      return $conditions;
    }
    
    // 3A Candidate
    add_filter( 'if_menu_conditions', 'menu_candidate_membership_condition' );
    
    function menu_candidate_membership_condition( $conditions ) {
      $conditions[] = array(
        'name'    =>  'Is 3A Candidate', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-candidate' );
        },
        'group'     =>  'Memberships'
      );
    
      return $conditions;
    }
    
    // 3A Member
    add_filter( 'if_menu_conditions', 'menu_3a_member_membership_condition' );
    
    function menu_3a_member_membership_condition( $conditions ) {
      $conditions[] = array(
        'name'    =>  'Is 3A Member', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-member' );
        },
        'group'     =>  'Memberships'
      );
    
      return $conditions;
    }
    
    // 3A Student
    add_filter( 'if_menu_conditions', 'menu_3a_student_membership_condition' );
    
    function menu_3a_student_membership_condition( $conditions ) {
      $conditions[] = array(
        'name'    =>  'Is 3A Student', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-student' );
        },
        'group'     =>  'Memberships'
      );
    
      return $conditions;
    }
    
    // 3A Instructor
    add_filter( 'if_menu_conditions', 'menu_membership_condition' );
    
    function menu_membership_condition( $conditions ) {
      $conditions[] = array(
        'name'    =>  'Is Instructor', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-instructor' );
        },
        'group'     =>  'Memberships'
      );
    
      return $conditions;
    }

    Is it better to wrap them all in one function in one array?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Andrei

    (@andreiigna)

    Hi,

    After taking a look of the code, I saw a few things which can be improved. Yes, it would be better to have all conditions in one function. Also the required field id is not added for conditions, see the example here https://github.com/AndreiIgna/if-menu#adding-custom-visibility-rules-in-a-plugin-or-theme

    Hope this helps

    Thread Starter lijitimit

    (@lijitimit)

    Adding the ID did the trick! Code is working now. Thanks for having a look, can’t believe I missed that.

    I’m still learning PHP, would you mind showing me quickly how to wrap it all into one function or point me in the right direction (what can I google?). I’m not sure what this process would be called to find a tutorial. Updated code doesn’t throw an error, but basically makes your entire plugin useless ?? i.e nothing shows up in the selector on the menu page.

    I suspect I’m missing referencing/ tying together the initial $conditions and $conditions[1] etc.

    add_filter( 'if_menu_conditions', 'my_new_menu_condition' );
    
    function my_new_menu_condition( $conditions ) {
      $conditions[1] = array(
        'id'        =>  'single-document',           // unique ID for the condition	
        'name'    =>  'Single Document', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return is_singular( 'documentation' );
        },
        'group'     =>  'Page type'
      );
    
      $conditions[2] = array(
        'id'        =>  'is-3a-candidate',           // unique ID for the condition	
        'name'    =>  'Is 3A Candidate', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-candidate' );
        },
        'group'     =>  'Memberships'
      );
    
      $conditions[3] = array(
        'id'        =>  'is-3a-member',           // unique ID for the condition	
        'name'    =>  'Is 3A Member', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-member' );
        },
        'group'     =>  'Memberships'
      );
    
      $conditions[4] = array(
        'id'        =>  'is-3a-student',           // unique ID for the condition	
        'name'    =>  'Is 3A Student', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-student' );
        },
        'group'     =>  'Memberships'
      );
    
      $conditions[5] = array(
        'id'        =>  'is-instructor',           // unique ID for the condition	
        'name'    =>  'Is Instructor', // name of the condition
        'condition' =>  function($item) {          // callback - must return TRUE or FALSE
          return wc_memberships_is_user_active_member( $user_id, '3a-instructor' );
        },
        'group'     =>  'Memberships'
      );
    
      return $conditions[1] . $conditions[2] . $conditions[3] . $conditions[4] . $conditions[5];
    }
    • This reply was modified 7 years, 6 months ago by lijitimit.
    Plugin Author Andrei

    (@andreiigna)

    It’s going in good direction, needs just a few small changes ??

    • when adding new items to a existing array there’s no need to specify the key
    • the return statement is not correct and it removes present conditions

    You can find more about this here https://php.net/manual/en/function.array-push.php

    The updated code is here https://paste.ofcode.org/4kMyQB2aVPSLHNYrj5YARV

    Thread Starter lijitimit

    (@lijitimit)

    Ahh ok I get it now! *tips hat
    Cheers

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Having issues adding multiple if menu conditions’ is closed to new replies.