WordPress本身是没有等级系统的,只是通过权限来划分不同的用户角色,默认的分组如果觉得还不够细致,可以通过代码来添加一些自定义权限的用户组。这样子网站会员系统就能够无限展开了。
//从官网抄了个
function xx__update_custom_roles() {
//添加一项'custom_roles_version'保证代码只执行一次
if ( get_option( 'custom_roles_version' ) < 1 ) {
//添加‘注册会员’,权限方面完全拷贝“订阅者”。
add_role( 'custom_role', '注册会员', get_role( 'subscriber' )->capabilities );
//自定义权限
//add_role( 'custom_role', '注册会员', array( 'read' => true, 'level_0' => true ) );
//......
//
update_option( 'custom_roles_version', 1 );
}
}
add_action( 'init', 'xx__update_custom_roles' );
这种根据权限的划分方式更适合协作写作,如果从订阅者或者说读者的角度看没什么意义,所以可以加上一套更适合读者用户的等级系统来弥补这一点。
其实这本质上来说就是一种积分系统,根据用户的行为获得积分,然后在根据用户积分总量来划分等级。
获得积分的方式可以是签到,评论,点赞或者是其他什么操作。举个例子:
//评论获得积分+15
add_action('comment_post', 'add_point');
function add_point( ){
if ( is_user_logged_in() ) {
//如果是登录用户评论,则获取用户
$current_user = wp_get_current_user();
//查询当前积分点数
$current_point = get_user_meta($current_user->ID, 'point', true);
//+15点积分
$point = intval($current_point)+15;
update_user_meta($current_user->ID, 'point', $point);
}
}
//后台显示积分
add_action('show_user_profile','user_point_fields',10,1);
function user_point_fields($profileuser){
$point = get_user_meta($profileuser->ID, 'point', true);
//随便写两句
switch ($point){
case (intval($point)<10):
$level = '小会员';
break;
default:
$level = '大会员';
}
?>
<h3>积分</h3>
<table class="form-table">
<tr>
<th>
<label for="point">我的积分</label></th>
<td>
<p><?php echo $point; ?></p>
</td>
</tr>
<tr>
<th>
<label for="level">我的等级</label></th>
<td>
<p><?php echo $level; ?></p>
</td>
</tr>
</table>
<?php
}
文章部分内容限制用户访问这种也可以搞出来。
//用短代码实现[level_2]大会员可见内容[/level_2]
add_shortcode( 'level_2', 'level_2_only' );
function level_2_only( $atts, $content = "" ) {
if ( is_user_logged_in() ){
$current_user = wp_get_current_user();
$current_point = get_user_meta($current_user->ID, 'point', true);
if(intval($current_point)>10){
return $content;
}
else{
return '大会员限定';
}
}
else{
return '大会员限定';
}
}
上面的代码就基本实现了所需要的新会员系统。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。