wordpress默认的注册页面只有填写邮箱选项,如果需要,我们可以自行添加QQ、新浪微博、Google+和网站四项,前三项存储在wp_usermeta表中,网站是WordPressProfile的默认字段,存储在wp_user表中,有固定的id叫做user_url。这四项全部设置为可选,用户不填也无所谓。
首先改一下注册界面,通过action register_form:
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"><?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 }
其次,改一下提交后的处理程序,更新自定义字段。如果用户填写了网址,需要简单的验证一下。
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默认的资料字段,添加自定义字段:
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; }