如何使用挂钩更改用户角色

时间:2015-11-20 作者:Roland Allla

我已经创建了一些自定义用户字段,所以我可以在我的网站上作为经销商管理我的用户。现在我的问题是这个用户角色,我想在我的函数中做点什么。php来检查我的自定义字段是否不为空,以便用户角色可以更改为经销商,否则让我们成为一个简单的用户。

我希望有人能帮忙。

function fb_add_custom_user_profile_fields( $user ) { ?>

<h3 id="dealer_button" ><?php _e(\' DEALER SECTION \', \'racecar.ch\'); ?></h3>
<div id="dealerpackage"></div>

<table class="form-table">
    <tr>
        <th>
            <label for="address"><?php _e(\'NR. OF FEATURED PACKAGE \', \'racecar.ch\'); ?>
        </label></th>
        <td>
            <input type="text" name="feature_package" id="feature_package" value="<?php echo esc_attr( get_the_author_meta( \'feature_package\', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e(\'Please enter number of featured packages.\', \'Racecar.ch\'); ?></span>
        </td>
    </tr>

    <tr>
        <th>
            <label for="address"><?php _e(\'NR. OF PREMIUM PACKAGE \', \'racecar.ch\'); ?>
        </label></th>
        <td>
            <input type="text" name="premium_package" id="premium_package" value="<?php echo esc_attr( get_the_author_meta( \'premium_package\', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e(\'Please enter number of premium packages.\', \'Racecar.ch\'); ?></span>
        </td>
    </tr>

    <!-- <tr>
        <th>
            <label for="address"><?php _e(\' Dealer Contact URL  \', \'racecar.ch\'); ?>
        </label></th>
        <td>
            <input type="text" name="dealer_url" id="dealer_url" value="<?php echo esc_attr( get_the_author_meta( \'dealer_url\', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e(\'Please enter the url of contact form .\', \'Racecar.ch\'); ?></span>
        </td>
    </tr> -->
</table>
   function fb_save_custom_user_profile_fields( $user_id ) {

    if ( !current_user_can( \'edit_user\', $user_id ) )
        return FALSE;

       update_usermeta( $user_id, \'feature_package\', $_POST[\'feature_package\'] );
       update_usermeta( $user_id, \'premium_package\', $_POST[\'premium_package\'] );
       //update_usermeta( $user_id, \'dealer_url\', $_POST[\'dealer_url\'] );

     }

add_action( \'show_user_profile\', \'fb_add_custom_user_profile_fields\',100 );
add_action( \'edit_user_profile\', \'fb_add_custom_user_profile_fields\',100);

add_action( \'personal_options_update\', \'fb_save_custom_user_profile_fields\',100 );
add_action( \'edit_user_profile_update\', \'fb_save_custom_user_profile_fields\',100 );

1 个回复
最合适的回答,由SO网友:jas 整理而成

请编辑您的函数并添加下面用于在中添加用户角色的代码functions.php

这是如何添加和删除角色以及如何检查字段值的基本概念。请根据您的要求使用。

add_action( \'profile_update\', \'my_profile_update\', 10, 2 );

function my_profile_update( $user_id, $old_user_data ) {

// here you need to update your required conditions, ie: check if meta values are set or not. I haven\'t checked your meta values but its supposed to like condition which is written in if condition, otherwise thr role update will work but take care of your condition

  if(get_the_author_meta( "feature_package", $user_id ) && get_the_author_meta( "premium_package", $user_id )){
     $u = new WP_User( $user_id );
     $u->set_role(\'author\'); // please replace with your required role ie: delar 
   }else{
     $u = new WP_User( $user_id );
     $u->set_role(\'subscriber\');
   }
}
WP _User class

有关更多详细信息,请访问以下链接:

profile_update developer

profile_update codex

只是为了解释一下,上面的代码将要做什么。任何用户何时更新其个人资料。该条件将检查用户自定义字段中是否设置了值,然后用户的角色将根据传递的值进行更改,如本例中的“author”。

谢谢

相关推荐