如果用户角色是某个角色,则隐藏HTML块

时间:2017-07-06 作者:J.L

我正在尝试在我们的网站上实现一个功能,用户可以通过单击团队页面上的按钮加入团队(团队是在用户角色编辑器中设置的自定义角色)。但是,如果用户已经是该团队的成员,我不希望为他们显示该按钮。

我已经有了使某人能够加入团队的代码:

<?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;
}

?>
然而,就我的一生而言,我无法想出如何隐藏一个特定角色的按钮,在上面的示例中,它将是营销角色。

如果有人能帮我一把,我将不胜感激。

哦,如果有更优雅的方法来完成我正在努力实现的目标,请随时告诉我。我对wordpress和php很陌生。

以上代码在此帖子中稍作修改:How to allow registered users to change their user role through frontend?

2 个回复
最合适的回答,由SO网友:J.L 整理而成

下面是我们最终用来解决问题的代码:

<?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;
}

?>
如果其他人遇到与我们相同的问题,希望他们会发现这很有用。

SO网友:Cesar Henrique Damascena

尝试更改此代码

 // 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 );

}
至以下内容

//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 );

}

结束

相关推荐