生命不息,奋斗不止/创造价值-传递价值-获得价值
所谓迷茫,就是才华配不上梦想 每一个让你难堪的现在,都有一个不够努力的曾经

允许用户注册时输入更多资料

在文章《WordPress注册界面:用户填写密码》中介绍了如何允许用户填写密码,今天还想练习一下添加自定义注册字段,比如qq、新浪微博、google+,另外还想让用户填写网站,很多用户注册后想不起来去更新资料,不如让他们注册的时候就填一下,以后制作会员列表会用得到。

 

向注册表单添加自定义字段

添加QQ、新浪微博、Google+和网站四项,前三项存储在wp_usermeta表中,网站是WordPress Profile的默认字段,存储在wp_user表中,有固定的id叫做user_url。这四项全部设置为可选,用户不填也无所谓。

首先改一下注册界面,通过action register_form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
add_action( 'register_form', 'ugp_show_extra_register_fields' );
function ugp_show_extra_register_fields(){
    ?>
    <p>
    <label for="password"><?php _e( 'Password', 'ugp-domain' );?><br/>
    <input id="password" class="input" type="password" tabindex="30"  size="25" value="" name="password" />
    </label>
    </p>
    <p>
    <label for="repeat_password"><?php _e( 'Repeat password', 'ugp-domain' );?><br/>
    <input id="repeat_password" class="input" type="password" tabindex="40" size="25" value="" name="repeat_password" />
    </label>
    </p>
    <p>
    <label for="url"><?php _e( 'Your Website', 'ugp-domain');?> <?php _e('(Optional)', 'ugp-domain' ); ?><br/>
    <input id="url" class="input" type="text" tabindex="50" size="25" value="<?php if( empty($_POST['url']) ) echo 'http://'; else echo $_POST['url']; ?>" name="url" />
    </label>
    </p>
    <p>
    <label for="qq">QQ <?php _e('(Optional)', 'ugp-domain' ); ?><br/>
    <input id="qq" class="input" type="text" tabindex="60"  size="25" value="<?php echo $_POST['qq']?>" name="qq" />
    </label>
    </p>
    <p>
    <label for="sina_weibo"><?php _e( 'Sina Weibo', 'ugp-domain' ); ?> <?php _e('(Optional)', 'ugp-domain' ); ?><br/>
    <input id="sina_weibo" class="input" type="text" tabindex="70" size="25" value="<?php echo $_POST['sina_weibo']?>" name="sina_weibo" />
    </label>
    </p>
    <p>
    <label for="googleplus">Google+ <?php _e('(Optional)', 'ugp-domain' ); ?><br/>
    <input id="googleplus" class="input" type="text" tabindex="80" size="25" value="<?php echo $_POST['googleplus']?>" name="googleplus" />
    </label>
    </p>
    <p>
    <label for="are_you_human" style="font-size:12px"><?php _e( 'Sorry, but we must check if you are human. What is the name of website you are registering for?' , 'ugp-domain' ); ?><br/>
    <input id="are_you_human" class="input" type="text" tabindex="90" size="25" value="" name="are_you_human" />
    </label>
    </p>
    <?php
}

其次,改一下提交后的处理程序,更新自定义字段。如果用户填写了网址,需要简单的验证一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
add_action( 'user_register', 'ugp_register_extra_fields', 100 );
function ugp_register_extra_fields( $user_id ){
    $userdata = array();
    
    $userdata['ID'] = $user_id;
    if( $_POST['qq'] !== '' )     $userdata['qq'] = $_POST['qq'];
    if( $_POST['sina_weibo'] !== '' )    $userdata['sina_weibo'] = $_POST['sina_weibo'];
    if( $_POST['googleplus'] !== '' )    $userdata['googleplus'] = $_POST['googleplus'];
    if( $_POST['url'] !== '' )    $userdata['user_url'] = $_POST['url'];
    if( $_POST['url'] !== '' || $_POST['url'] !== 'http://') {
        $userdata['user_url'] = esc_url_raw( $_POST['url'] );
        $userdata['user_url'] = preg_match('/^(https?|ftps?|mailto|news|irc|gopher|nntp|feed|telnet):/is', $userdata['user_url']) ? $userdata['user_url'] : 'http://'.$userdata['user_url'];
    }
    
    if ( $_POST['password'] !== '' ) {
        $userdata['user_pass'] = $_POST['password'];
    }
    $new_user_id = wp_update_user( $userdata );
}

用户资料增加字段

既然注册的时候允许用户填写一些基本信息,在用户资料中也要有所体现,方法在文章《WordPress小技巧:为用户资料添加新的联系方式》中介绍过,不在赘述。

去掉WordPress默认的资料字段,添加自定义字段

1
2
3
4
5
6
7
8
9
10
add_filter( 'user_contactmethods', 'ugp_custom_contact_fields' );
function ugp_custom_contact_fields( $contactmethods ) {
    $contactmethods['qq'] = 'QQ';
    $contactmethods['sina_weibo'] = __('Sina Weibo', 'ugp-domain');
    $contactmethods['googleplus'] = 'Google+';
    unset( $contactmethods['yim'] );
    unset( $contactmethods['aim'] );
    unset( $contactmethods['jabber'] );
    return $contactmethods;
}

最终效果

注册时填写更多的资料

插件下载

把这些代码封装成了小插件,仅供测试。user-generate-password-more-fields

User Generate Password 添加更多注册字段 已下载 94 次

赞(0)
未经允许不得转载:jack361博客 » 允许用户注册时输入更多资料

评论 抢沙发

评论前必须登录!

 

如果你爱我或恨我,请↓

联系我