0

I am trying to implement a feature on our site where a user is able to join a team by clicking a button on that teams page (teams being custom roles set up in User Role Editor). However, if the user is already a member of that team, I don't want the button to be displayed for them.

I already have the code working to enable someone to join a team:

<?php

add_shortcode( 'select_marketing', 'select_marketing' );
function select_marketing() {

    // Stop if user is not logged in.
    if ( ! is_user_logged_in() )
        return;

    ob_start();

    ?>

<form method="post" action="">
    <button class="btn btn-default" type="submit" name="role" value="marketing">Join this Team</button>
</form>

    <?php

    // Do not process anything if it's not $_POST
    if ( ! isset( $_POST['role'] ) )
        return;

    // Never trust user input.
    $role = sanitize_key( $_POST['role'] );
    if ( ! in_array( $role, array( 'marketing', ) ) )
        return;

    // Get the user object
    $user = new WP_User( get_current_user_id() );
    $index = key( $user->roles );
    $user_role = $user->roles[ $index ];

    // User already got that user
    if ( $user_role == $role ) {

        echo sprintf( __( 'You already have %s role.' ), $role );

    } else {

        // update user role
        $user->set_role( $role );
        echo sprintf( __( 'Your role was changed to %s.' ), $role );

    }

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}

?>

However I can't for the life of me figure out how to hide the button for just one specific role, in the example above it would be the marketing role.

If anyone could give me a hand, that would be highly appreciated.

Oh, and if there is a more elegant approach to doing what I am trying achieve, please feel free to let me know. I'm quite new to wordpress and php.

The above code is slightly modified from this post: How to allow registered users to change their user role through frontend?

0

Try to change this code

 // Get the user object
    $user = new WP_User( get_current_user_id() );
    $index = key( $user->roles );
    $user_role = $user->roles[ $index ];

// User already got that user
if ( $user_role == $role ) {

    echo sprintf( __( 'You already have %s role.' ), $role );

} else {

    // update user role
    $user->set_role( $role );
    echo sprintf( __( 'Your role was changed to %s.' ), $role );

}

to the following

//Get the user
$user = wp_get_current_user();

// User already got this role
if( $user->has_cap( $role ) ) {

  echo sprintf( __( 'You already have %s role.' ), $role );

  // Do whatever you want

} else {

  // update user role
  $user->set_role( $role );
  echo sprintf( __( 'Your role was changed to %s.' ), $role );

}
  • Hey thanks for getting back to me, however this doesn't seem to have worked and I can still see the button when I have the marketing role. – J.L Jul 7 '17 at 10:01
  • How is the exacly code you are using to hide the button ? can you post it? – Cesar Henrique Damascena Jul 7 '17 at 12:15
  • I've entered it exactly as I posted in my original question, except where you'd said to change the code above, I've changed it. Sorry I don't have enough characters to post the entire code in a reply either. – J.L Jul 10 '17 at 6:44
  • I saw your updated question, you're putting the form before the validations, you have to run all validations, and only if they check, you will show the form – Cesar Henrique Damascena Jul 10 '17 at 11:59
  • Hey, thanks for the response yesterday. We ended up cracking it ourselves using a different method, cheers though! – J.L Jul 11 '17 at 8:12
0

Here is the code we ended up using which solved our problem:

<?php

add_shortcode( 'change_department', 'change_department_func' );
/* This would be called from page content with a parameter for 'change_role' 
e.g [change_department change_role="fnc"] */
function change_department_func( $atts )  
{   
    //the parameters seem to work is that they all get 
    //put into an array - so here we extract the change_role param.
    //this variable holds the value of the role that we will change the 
    //user to when they click the change role button.
    //e.g. fnc  - would be the role for finance.
    $change_role = $atts['change_role'];

    // Stop if user is not logged in.
    if ( ! is_user_logged_in() )
        return;

    ob_start();

    // Get the user object
    $user = new WP_User( get_current_user_id() );

    //get the users current role
    $index = key( $user->roles );
    $user_role = $user->roles[ $index ];


    if ( $user_role == $change_role || isset($_POST['role']) ) { 
        // User already got that role so no need for a button
    } else {
        //user doesn't already have the role so we create a role setting button 
        //on the page for them to click if they want  
        echo "
        <form id='join' method='post' action=''>
        <button class='btn btn-default' type='submit' name='role' >Join this Team</button>
        </form>";
}
    if(isset($_POST['role']))
    {    
        //this will be set if the page is loaded after the user clicks the button
        //so in this case we set the users role to be the value we got from
        //the parameter array
        $user = new WP_User( get_current_user_id() );
        $index = key( $user->roles );
        $user_role = $user->roles[ $index ];
        $user->set_role( $change_role );    
    }

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}

?>

Hopefully someone else will find it useful if they encounter the same issue we had.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.