Skip to:
Content
Pages
Categories
Search
Top
Bottom

Dynamically Display HTML depending on User


  • cameck
    Participant

    @cameck

    WordPress Version 4.3.1 – BuddyPress Version 2.2.4

    Hi,

    I’m trying to find a function I can hook into to.

    I have two types of users on my BuddyPress site, employees and employers. These are defined in the user area of WordPress.

    I have made a custom profile page in my child theme labeled buddypress.php.

    Everything is working fine, but I want to hide the make a payment button if the user’s profile is an employer profile.

    I can only find code for checking the current logged in user status.

    Any help is appreciated!

Viewing 8 replies - 1 through 8 (of 8 total)

  • Henry Wright
    Moderator

    @henrywright

    You need a way of getting the user’s type, then you can do this:

    if ( $type === 'employee' ) {
        // Show button.
    } elseif ( $type === 'employer' ) {
        // Hide button.
    }

    cameck
    Participant

    @cameck

    OK, that helps, I feel I’m getting closer.

    Here’s what I have:

    <?php if(is_user_logged_in() && $type === $s2member_level1){ ?>
    
    <div class="paybox">
    <h3>Make Payment with PayPal&reg;</h3>
    <?php $uemail = getDisplayedUserEmail(); ?>
    <?php echo do_shortcode('[gravityforms id="7" field_values="recip-email=' . $uemail . '"]'); ?>
    </div><!-- .paybox -->
    <?php } ?>

    Now, it is still displaying on both users pages (employee and employer)…


    shanebp
    Moderator

    @shanebp

    Your question now seems to be about s2member, not BuddyPress.

    How are you setting this variable: $s2member_level1 ?

    If you want to display the paybox based on s2member level, then use an s2member function to set $s2member_level1 or use a constant.

    if( 2 == S2MEMBER_CURRENT_USER_ACCESS_LEVEL ) // etc
    https://www.s2member.com/codex/stable/s2member/api_constants/package-globals/#src_doc_S2MEMBER_CURRENT_USER_ACCESS_LEVEL


    cameck
    Participant

    @cameck

    You’re right! I didn’t realize it at first…. I appreciate all the help you’ve given so far. I can continue this on the S2 Member Forum if you like, you’re just doing such an awesome job though :D.

    Regarding the $s2member_level1 , I was just pulling that out of my butt and it was only thing I put there that didn’t break the pages.

    But if you’re curious, this is what I have now:

    <?php if(is_user_logged_in() && 4 == S2MEMBER_CURRENT_USER_ACCESS_LEVEL){ ?>
    
    <div class="paybox">
    <h3>Make Payment with PayPal&reg;</h3>
    <?php $uemail = getDisplayedUserEmail(); ?>
    <?php echo do_shortcode('[gravityforms id="7" field_values="recip-email=' . $uemail . '"]'); ?>
    </div><!-- .paybox -->
    
    <?php } ?>

    This is working for the most part, but oddly enough there are a few more employers that are still showing the paypal button. So, I started using this:
    <?php echo S2MEMBER_CURRENT_USER_ACCESS_LEVEL; ?>
    to see the output and most employers are the number 2, but a few are listed as 4 – which creates the problem. Ideally, I could just do it like in the previous code I had put up $type === $s2member_level1 as that’s how they are listed in the backend of WordPress.


    Henry Wright
    Moderator

    @henrywright

    Maybe try echo c_ws_plugin__s2member_user_access::user_access_level( $user ); to see what that outputs?

    Note: $user should be a WP_User object.


    cameck
    Participant

    @cameck

    Ok I think I’m realizing the problem here, all of theses codes are outputting the user access level of the person who is logged in, not the profile. I need to check the user access level of the user who’s profile is being viewed. Do I make sense? I feel I might be being a little confusing


    Henry Wright
    Moderator

    @henrywright

    Yes, that sounds right. Getting the user ID is slightly different in BuddyPress depending on the context. See the Playing with the user’s ID in different contexts article for details.


    cameck
    Participant

    @cameck

    All right thank you everyone, I finally figured it out.

    I was not able to find an S2 Member function that worked for my specific task.

    Luckily I had a custom BuddyPress field that also designated the user Level.

    I got the field ID of “55” by hovering over the edit portion of the custom profile field.

    This is the final working code:

    <?php if(is_user_logged_in() && xprofile_get_field_data( 55 ) == 'Employee'){ ?>
    
    <div class="paybox">
    <h3>Make Payment with PayPal&reg;</h3>
    <?php $uemail = getDisplayedUserEmail(); ?>
    <?php echo do_shortcode('[gravityforms id="7" field_values="recip-email=' . $uemail . '"]'); ?>
    </div><!-- .paybox -->
    <?php } ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Dynamically Display HTML depending on User’ is closed to new replies.
Skip to toolbar