wordpress のマルチサイトでサイトとユーザを自動的(スクリプト)に追加、削除する方法
■マルチサイトでスクリプトでサイト、ユーザを追加する。
http://blog.chobi.net/ をネットワークサイトとして・・・
add.php
にアクセスするとサイトとユーザが作れます。
test1がサブディレクトリ名
kanri@chobi.netがメール名なので
ここらへんを変数として
add.phpを生成してアクセスすれば自動化できるかな。
add.php の中身は・・・
<html>
<body>
<form method="post" action="http://blog.chobi.net/wp-admin/ms-edit2.php?action=addblog">
<input type="hidden" id="_wpnonce_add-blog" name="_wpnonce_add-blog"
value="2a56839191" />
<input type="hidden" name="_wp_http_referer" value="/wp-admin/ms-sites.php" />
<input type="hidden" name="blog[domain]" class="regular-text" type="text"
title="ドメイン" value="test2" />
<input type="hidden" name="blog[email]" type="text" class="regular-text"
title="メールアドレス" value="kanri@chobi.net" />
<input class="button" type="submit" name="go" value="サイトを追加" />
</form>
</body>
</html>
これで追加をするとメールも来ない
っていうかms-edit2.phpでメールをこなくさせてる。
↓ここらへんがメールの処理
//wp_new_user_notification( $user_id, $password );
//$content_mail = sprintf( __( "New site created by %1s\n\nAddress: http://%2s\nName:
%3s"), $current_user->user_login , $newdomain . $path, stripslashes( $title ) );
//wp_mail( get_site_option('admin_email'), sprintf( __( '[%s] New Site Created'
), $current_site->site_name ), $content_mail, 'From: "Site Admin" <' .
get_site_option( 'admin_email' ) . '>' );
//wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public'
=> 1 ) );
_wpnonce_add-blog _wp_http_refererについてはちゃんと調べてない・・・。
■マルチサイトで、サイト、ユーザを削除する。
http://blog.chobi.net/wp-admin/ms-edit2.php?action=deleteblog&id=2
http://blog.chobi.net/wp-admin/ms-edit2.php?action=dodelete&id=2
ポイントは
deleteblog →
dodelete
の順でやること。
idが変数になり、
サイトはwp_blogsテーブルのid
ユーザのほうはidはwp_usersのテーブルid
から引っこ抜く
ちなみに、ここらへんのテーブルは
AUTO_INCREMENTになっており
かってにIDがインクリメントされるのが
ちょっといただけない・・・。
actionのdeleteblogにだまされた。
■マルチサイトでのパーマリンク設定
なんかマルチサイトだと
日付と投稿名
http://blog.chobi.net/blog/2011/01/01/sample-post/
がデフォルトで気持ち悪い。
こいつを直すには、
schema.php
を以下のように修正したらしたら
デフォルト http://blog.chobi.net/?p=123
の表示となる。
// 3.0 multisite
if ( is_multisite() ) {
/* translators: blog tagline */
$options[ 'blogdescription' ] = sprintf(__('Just another %s site'), $current_site->site_name
);
$options[ 'permalink_structure' ] = '';
//$options[ 'permalink_structure' ] = '/%year%/%monthnum%/%day%/%postname%/';
■ms-edit2.php の中身
基本 ms-edit.php そのままで
ただそのままだとTOPにリダイレクションされたりするので
がつがつコメント。
ちなみに処理を全部追ってはないので、ちゃんと追ったら
もっと不要でコメントできるところあるかも。始めのほうとかかなり不要そうな感じのがある。
<?php
/**
* WordPress Administration Bootstrap
*
* @package WordPress
* @subpackage Administration
*/
/**
* In WordPress Administration Panels
*
* @since unknown
*/
if ( !defined('WP_ADMIN') )
define('WP_ADMIN', TRUE);
if ( isset($_GET['import']) && !defined('WP_LOAD_IMPORTERS') )
define('WP_LOAD_IMPORTERS', true);
require_once(dirname(dirname(__FILE__)) . '/wp-load.php');
if ( get_option('db_upgraded') ) {
$wp_rewrite->flush_rules();
update_option( 'db_upgraded', false );
/**
* Runs on the next page load after successful upgrade
*
* @since 2.8
*/
do_action('after_db_upgrade');
} elseif ( get_option('db_version') != $wp_db_version ) {
if ( !is_multisite() ) {
wp_redirect(admin_url('upgrade.php?_wp_http_referer=' .
urlencode(stripslashes($_SERVER['REQUEST_URI']))));
exit;
} elseif ( apply_filters( 'do_mu_upgrade', true ) ) {
/**
* On really small MU installs run the upgrader every time,
* else run it less often to reduce load.
*
* @since 2.8.4b
*/
$c = get_blog_count();
if ( $c <= 50 || ( $c > 50 && mt_rand( 0, (int)( $c / 50 ) ) == 1 ) ) {
require_once( ABSPATH . WPINC . '/http.php' );
$response = wp_remote_get( admin_url( 'upgrade.php?step=1' ), array( 'timeout'
=> 120, 'httpversion' => '1.1' ) );
do_action( 'after_mu_upgrade', $response );
unset($response);
}
unset($c);
}
}
require_once(ABSPATH . 'wp-admin/includes/admin.php');
//auth_redirect();
nocache_headers();
update_category_cache();
// Schedule trash collection
if ( !wp_next_scheduled('wp_scheduled_delete') && !defined('WP_INSTALLING') )
wp_schedule_event(time(), 'daily', 'wp_scheduled_delete');
set_screen_options();
$date_format = get_option('date_format');
$time_format = get_option('time_format');
wp_reset_vars(array('profile', 'redirect', 'redirect_url', 'a', 'text',
'trackback', 'pingback'));
wp_enqueue_script( 'common' );
wp_enqueue_script( 'jquery-color' );
$editing = false;
if ( isset($_GET['page']) ) {
$plugin_page = stripslashes($_GET['page']);
$plugin_page = plugin_basename($plugin_page);
}
if ( isset($_GET['post_type']) )
$typenow = sanitize_key($_GET['post_type']);
else
$typenow = '';
if ( isset($_GET['taxonomy']) )
$taxnow = sanitize_key($_GET['taxonomy']);
else
$taxnow = '';
require(ABSPATH . 'wp-admin/menu.php');
if ( current_user_can( 'manage_options' ) )
@ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', '256M' ) );
do_action('admin_init');
if ( isset($plugin_page) ) {
if ( !empty($typenow) )
$the_parent = $pagenow . '?post_type=' . $typenow;
else
$the_parent = $pagenow;
if ( ! $page_hook = get_plugin_page_hook($plugin_page, $the_parent) ) {
$page_hook = get_plugin_page_hook($plugin_page, $plugin_page);
// backwards compatibility for plugins using add_management_page
if ( empty( $page_hook ) && 'edit.php' == $pagenow && '' !=
get_plugin_page_hook($plugin_page, 'tools.php') ) {
// There could be plugin specific params on the URL, so we need the whole query
string
if ( !empty($_SERVER[ 'QUERY_STRING' ]) )
$query_string = $_SERVER[ 'QUERY_STRING' ];
else
$query_string = 'page=' . $plugin_page;
wp_redirect( 'tools.php?' . $query_string );
exit;
}
}
unset($the_parent);
}
$hook_suffix = '';
if ( isset($page_hook) )
$hook_suffix = $page_hook;
else if ( isset($plugin_page) )
$hook_suffix = $plugin_page;
else if ( isset($pagenow) )
$hook_suffix = $pagenow;
set_current_screen();
// Handle plugin admin pages.
if ( isset($plugin_page) ) {
if ( $page_hook ) {
do_action('load-' . $page_hook);
if (! isset($_GET['noheader']))
require_once(ABSPATH . 'wp-admin/admin-header.php');
do_action($page_hook);
} else {
if ( validate_file($plugin_page) )
wp_die(__('Invalid plugin page'));
if ( !( file_exists(WP_PLUGIN_DIR . "/$plugin_page") && is_file(WP_PLUGIN_DIR .
"/$plugin_page") ) && !( file_exists(WPMU_PLUGIN_DIR . "/$plugin_page") &&
is_file(WPMU_PLUGIN_DIR . "/$plugin_page") ) )
wp_die(sprintf(__('Cannot load %s.'), htmlentities($plugin_page)));
do_action('load-' . $plugin_page);
if ( !isset($_GET['noheader']))
require_once(ABSPATH . 'wp-admin/admin-header.php');
if ( file_exists(WPMU_PLUGIN_DIR . "/$plugin_page") )
include(WPMU_PLUGIN_DIR . "/$plugin_page");
else
include(WP_PLUGIN_DIR . "/$plugin_page");
}
include(ABSPATH . 'wp-admin/admin-footer.php');
exit();
} else if (isset($_GET['import'])) {
$importer = $_GET['import'];
if ( ! current_user_can('import') )
wp_die(__('You are not allowed to import.'));
if ( validate_file($importer) )
wp_redirect( admin_url( 'import.php?invalid=' . $importer ) );
// Allow plugins to define importers as well
if ( !isset($wp_importers) || !isset($wp_importers[$importer]) || !
is_callable($wp_importers[$importer][2])) {
if (! file_exists(ABSPATH . "wp-admin/import/$importer.php"))
wp_redirect( admin_url( 'import.php?invalid=' . $importer ) );
include(ABSPATH . "wp-admin/import/$importer.php");
}
$parent_file = 'tools.php';
$submenu_file = 'import.php';
$title = __('Import');
if (! isset($_GET['noheader']))
require_once(ABSPATH . 'wp-admin/admin-header.php');
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
define('WP_IMPORTING', true);
if ( is_multisite() )
kses_init_filters(); // Always filter imported data with kses.
call_user_func($wp_importers[$importer][2]);
include(ABSPATH . 'wp-admin/admin-footer.php');
// Make sure rules are flushed
global $wp_rewrite;
$wp_rewrite->flush_rules(false);
exit();
} else {
do_action("load-$pagenow");
// Backwards compatibility with old load-page-new.php, load-page.php,
// and load-categories.php actions.
if ( $typenow == 'page' ) {
if ( $pagenow == 'post-new.php' )
do_action( 'load-page-new.php' );
elseif ( $pagenow == 'post.php' )
do_action( 'load-page.php' );
} elseif ( $taxnow == 'category' && $pagenow == 'edit-tags.php' ) {
do_action( 'load-categories.php' );
}
}
if ( !empty($_REQUEST['action']) )
do_action('admin_action_' . $_REQUEST['action']);
if ( ! is_multisite() )
wp_die( __( 'Multisite support is not enabled.' ) );
if ( empty( $_GET['action'] ) )
wp_redirect( admin_url( 'ms-admin.php' ) );
do_action( 'wpmuadminedit' , '');
if ( isset( $_GET['id' ]) )
$id = intval( $_GET['id'] );
elseif ( isset( $_POST['id'] ) )
$id = intval( $_POST['id'] );
switch ( $_GET['action'] ) {
case 'addblog':
// if ( ! current_user_can( 'manage_sites' ) )
// wp_die( __( 'You do not have permission to access this page.' ) );
if ( is_array( $_POST['blog'] ) == false )
wp_die( __( 'Can’t create an empty site.' ) );
$blog = $_POST['blog'];
$domain = '';
if ( ! preg_match( '/(--)/', $blog['domain'] ) && preg_match(
'|^([a-zA-Z0-9-])+$|', $blog['domain'] ) )
$domain = strtolower( $blog['domain'] );
// If not a subdomain install, make sure the domain isn't a reserved word
if ( ! is_subdomain_install() ) {
$subdirectory_reserved_names = apply_filters( 'subdirectory_reserved_names',
array( 'page', 'comments', 'blog', 'files', 'feed' ) );
if ( in_array( $domain, $subdirectory_reserved_names ) )
wp_die( sprintf( __('The following words are reserved for use by WordPress
functions and cannot be used as blog names: <code>%s</code>' ), implode(
'</code>, <code>', $subdirectory_reserved_names ) ) );
}
$email = sanitize_email( $blog['email'] );
$title = $blog['title'];
if ( empty( $domain ) )
wp_die( __( 'Missing or invalid site address.' ) );
if ( empty( $email ) )
wp_die( __( 'Missing email address.' ) );
if ( !is_email( $email ) )
wp_die( __( 'Invalid email address.' ) );
if ( is_subdomain_install() ) {
$newdomain = $domain . '.' . preg_replace( '|^www\.|', '', $current_site->domain
);
$path = $base;
} else {
$newdomain = $current_site->domain;
$path = $base . $domain . '/';
}
$password = 'test123';
$user_id = email_exists($email);
if ( !$user_id ) { // Create a new user with a random password
//$password = wp_generate_password();
$user_id = wpmu_create_user( $domain, $password, $email );
if ( false == $user_id )
wp_die( __( 'There was an error creating the user.' ) );
//else
//wp_new_user_notification( $user_id, $password );
}
$wpdb->hide_errors();
$id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' =>
1 ), $current_site->id );
$wpdb->show_errors();
//if ( !is_wp_error( $id ) ) {
$dashboard_blog = get_dashboard_blog();
//if ( !is_super_admin( $user_id ) && get_user_option( 'primary_blog', $user_id
) == $dashboard_blog->blog_id )
// update_user_option( $user_id, 'primary_blog', $id, true );
//$content_mail = sprintf( __( "New site created by %1s\n\nAddress: http://%2s\nName:
%3s"), $current_user->user_login , $newdomain . $path, stripslashes( $title ) );
//wp_mail( get_site_option('admin_email'), sprintf( __( '[%s] New Site Created'
), $current_site->site_name ), $content_mail, 'From: "Site Admin" <' .
get_site_option( 'admin_email' ) . '>' );
//wpmu_welcome_notification( $id, $user_id, $password, $title, array( 'public'
=> 1 ) );
//wp_redirect( add_query_arg( array( 'updated' => 'true', 'action' => 'add-blog'
), wp_get_referer() ) );
// exit();
//} else {
// wp_die( $id->get_error_message() );
//}
break;
case 'deleteblog':
//check_admin_referer('deleteblog');
//if ( ! current_user_can( 'manage_sites' ) )
// wp_die( __( 'You do not have permission to access this page.' ) );
if ( $id != '0' && $id != $current_site->blog_id )
wpmu_delete_blog( $id, true );
//wp_redirect( add_query_arg( array( 'updated' => 'true', 'action' => 'delete'
), wp_get_referer() ) );
exit();
break;
case 'dodelete':
//check_admin_referer( 'ms-users-delete' );
//if ( ! current_user_can( 'manage_network_users' ) )
// wp_die( __( 'You do not have permission to access this page.' ) );
//if ( ! empty( $_POST['blog'] ) && is_array( $_POST['blog'] ) ) {
// foreach ( $_POST['blog'] as $id => $users ) {
// foreach ( $users as $blogid => $user_id ) {
// if ( ! empty( $_POST['delete'] ) && 'reassign' == $_POST['delete'][$blogid][$id]
)
// remove_user_from_blog( $id, $blogid, $user_id );
// else
// remove_user_from_blog( $id, $blogid );
// }
// }
//}
//$i = 0;
//if ( is_array( $_POST['user'] ) && ! empty( $_POST['user'] ) )
// foreach( $_POST['user'] as $id ) {
wpmu_delete_user( $id );
// $i++;
// }
//if ( $i == 1 )
// $deletefunction = 'delete';
//else
// $deletefunction = 'all_delete';
//wp_redirect( add_query_arg( array( 'updated' => 'true', 'action' => $deletefunction
), admin_url( 'ms-users.php' ) ) );
break;
}
?>