WordPressのプラグイン作成にのめりこんでしまっている感ではあるのだが・・・。今回作ったのはユーザを一括登録するためのプラグイン。カンマ区切りのユーザデータを用意しておくと、それを使って一括でユーザ登録ができる。会員専用のページを作る際などに、ユーザ一括登録ができるので便利だと思う(今回もそういう思惑で作った)。ただし・・・ひとつのサイトで何回も使うようなプラグインではないので、使用後は削除したほうがいいかもしれない。
CWBR batch registration plugin for WordPress
ソースコードはこんな感じ。
<?php
/*
Plugin Name: CW Batch Register
Plugin URI: http://php.tekmemo.com/
Description: Regist several users at one process.
Author: kaorun
Author URI: http://php.tekmemo.com/
Version: alpha
*/
class cwbr
{
/*——————————————————————–
|
| Create administration menu
| Called by add_filter()
|
——————————————————————–*/
function admin()
{
add_options_page(‘options-general.php’, ‘CWBR’, 8, basename(__FILE__), array(&$this,’processAdmin’));
}
/*——————————————————————–
|
| Provides administration tools
| Called by above function “admin()”
|
——————————————————————–*/
function processAdmin()
{
global $wpdb;
// [+] Batch registration processing
$params=array(‘mail’,’list’);
$errors=array();
if(strtolower($_SERVER[‘REQUEST_METHOD’])==’post’){
foreach($params as $param){
$values[‘cwbr_’.$param]=(isset($_POST[‘cwbr_’.$param])? trim($_POST[‘cwbr_’.$param]):”);
}
extract($values);
$cwbr_list=str_replace(“¥r¥n”,”¥n”,trim($cwbr_list));
$cwbr_list=str_replace(“¥r”,”¥n”,$cwbr_list);
$rows=explode(“¥n”,$cwbr_list);
$already=array();
$already[’email’]=array();
$already[‘userid’]=array();
$users=array();
foreach($rows as $row){
//$person=explode(‘,’,$row);
list($user_email, $user_login, $user_pass, $user_url, $first_name, $last_name, $display_name, $nickname, $jabber, $aim, $yim)=explode(‘,’,$row);
if(!is_email($user_email)){
$errors[]=$user_email.’ … This email address is invalid.';
break;
}elseif($cwbr_mail && (in_array($user_email,$already[’email’]) || ($wpdb->get_row(“SELECT user_email FROM $wpdb->users WHERE user_email = ‘$user_email'”)))){
$errors[]=$user_email.’ … This email address is used or already registered.';
break;
}else{
$already[’email’][]=$cwbr_mail;
}
if(!validate_username($user_login)){
$errors[]=$user_login.’ … This user id is invalid.';
break;
}elseif(username_exists($user_login)){
$errors[]=$user_login.’ … This user id is already registered.';
break;
}elseif(in_array($user_login,$already[‘userid’])){
$errors[]=$user_login.’ … The same user id is used.';
break;
}else{
$already[‘userid’][]=$user_login;
}
if(!strlen($user_pass)){
$errors[]=$user_email.'(‘.$user_login.’)’.’ … Password is always required.';
break;
}
$users[]=compact(‘user_email’, ‘user_login’, ‘user_pass’, ‘user_url’, ‘first_name’, ‘last_name’, ‘display_name’, ‘nickname’, ‘jabber’, ‘aim’, ‘yim’);
}
if(!$errors){
foreach($users as $user){
if(!wp_insert_user($user)){
$errors[]=$user[’email’].'(‘.$user[‘userid’].’)’.’ … This user is not registered. I do not know why.';
&
nbsp; }
}
if($errors){
$errors[]=’Some errors are occurred.';
}else{
$errors[]=’Users are successfully registered.';
}
}
}else{
$values=array();
foreach($params as $param){
$values[‘cwbr_’.$param]=”;
}
}
// [+] Get user list
$userids = $wpdb->get_col(“SELECT ID FROM $wpdb->users;”);
$roleclasses=array();
foreach($userids as $userid) {
$tmp_user = new WP_User($userid);
$roles = $tmp_user->roles;
$role = array_shift($roles);
$roleclasses[$role][$tmp_user->user_login] = $tmp_user;
}
// [+] Output
$cwbrOutput=”;
$cwbrOutput.='<div class=”wrap”>';
$cwbrOutput.='<h2>CW Batch Registration</h2>';
if($errors){
$cwbrOutput.='<ul>';
foreach($errors as $error){
$cwbrOutput.='<li>’.$error.'</li>';
}
$cwbrOutput.='</ul>';
}
$cwbrOutput.='<form name=”form1″ method=”post” action=”‘.$_SERVER[‘PHP_SELF’].’?page=cwbr.php”>';
$cwbrOutput.='<div>[+] Do you need the check of duplication of e-mail addresses ?</div>';
$cwbrOutput.='<div><input type=”checkbox” name=”cwbr_mail” value=”1″ ‘.(($values[‘cwbr_mail’])? ‘ checked':”).’/> I need check.</div>';
$cwbrOutput.='<br />';
$cwbrOutput.='<div>[+] Input the list separated with comma “,”.<br />EX) hoge@example.com, userid, password, website_url, first_name, last_name, display_name, nickname, jabber, aim, yim</div>';
$cwbrOutput.='<div><textarea name=”cwbr_list” cols=”100″ rows=”10″>’.$values[‘cwbr_list’].'</textarea></div>';
$cwbrOutput.='<input type=”submit” value=”submit” />';
$cwbrOutput.='</form>';
$cwbrOutput.='</div>';
echo $cwbrOutput;
}
}
$cwbrObj=& new cwbr;
add_action(‘admin_menu’, array(&$cwbrObj,’admin’));