สงสัยจะมีคนสนใจแค่ คนเดียว ดีๆ ทำเป็นกันน้อยๆ นี่แหละ เก็บค่าทำกันรวยไปเลย
วิธีทำให้ WP ที่ติดตั้งแยกกันในหลายซับโดเมน ซับไดเรคทอรี่ และตัวโดเมนหลัก ใช้ user เดียวกันได้
Multiple WP Share user to sign in
เริ่มแรกต้องดูก่อนเลยว่า WP ที่คุณต้องการจะแชร์ user กันนั้น มันติดตั้งอยู่ในฐานข้อมูลเดียวกันหรือเปล่า ถ้าคนละฐานข้อมูล มันจะทำไม่ได้
ถ้าฐานข้อมูลพร้อมแล้วก็เริ่ม
ขั้นตอนที่ 1 เปิดไฟล์ wp-config.php ของทุกไซต์ที่คุณต้องการแชร์ user (ทำทีละอันก็ได้ ค่อยๆ ทำ)
ไฟล์ wp-config.php จี้จะใช้ ไฟล์ sample ของ wp 3.0.x เป็นฐานนะ
จะเพิ่มโค้ดที่จำเป็นในการแชร์ฐานข้อมูล ทั้งหมด 5 โค้ด ตามนี้
<?php
$main_prefix = 'xx_';/*your Main WP prefix end with underscore _ */
define('CUSTOM_CAPABILITIES_PREFIX', $main_prefix);/* add this to share user capabilities*/
define('COOKIE_DOMAIN', '.yourdomain.xxx');/* add this for single sign on but now it buggie and do not use it on Your Main WP*/
define('CUSTOM_USER_TABLE', $main_prefix.'users');/* add this to share user table*/
define('CUSTOM_USER_META_TABLE', $main_prefix.'usermeta');/* add this to share user meta*/
?>
copy ไปวางในไฟล์ wp-config.php ตามตัวอย่างข้างล่างนี้ (copy ใส่ notepad ++ หรือโปรแกรม editor ดูเลยง่ายกว่าเยอะ)
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* @package WordPress
*/
$main_prefix = 'xx_';/*your Main WP prefix end with underscore _ */
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'your db name'); // The name of the database
define('DB_USER', 'your db user'); // Your MySQL username
define('DB_PASSWORD', 'your db pass'); // ...and password
define('DB_HOST', 'localhost'); // 99% chance you won't need to change this value
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', 'utf8_general_ci');
//define('WP_POST_REVISIONS', false);
//define('AUTOSAVE_INTERVAL', 86400);
//define('WP_CACHE', true);
//define('SAVEQUERIES', true);
define('CUSTOM_CAPABILITIES_PREFIX', $main_prefix);/* add this to share user capabilities*/
define('COOKIE_DOMAIN', '.yourdomain.xxx');/* add this for single sign on but now it buggie and do not use it on Your Main WP*/
/**#@+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* @since 2.6.0
*/
define('AUTH_KEY', 'use WP auto gen key');
define('SECURE_AUTH_KEY', 'use WP auto gen key');
define('LOGGED_IN_KEY', 'use WP auto gen key');
define('NONCE_KEY', 'use WP auto gen key');
define('AUTH_SALT', 'use WP auto gen key');
define('SECURE_AUTH_SALT', 'use WP auto gen key');
define('LOGGED_IN_SALT', 'use WP auto gen key');
define('NONCE_SALT', 'use WP auto gen key');
/**#@-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de.mo to wp-content/languages and set WPLANG to 'de' to enable German
* language support.
*/
define ('WPLANG', '');
define('CUSTOM_USER_TABLE', $main_prefix.'users');/* add this to share user table*/
define('CUSTOM_USER_META_TABLE', $main_prefix.'usermeta');/* add this to share user meta*/
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define('WP_DEBUG', false);
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
?>
สำหรับ ค่าที่ีจะต้องใส่ใน
<?php
define('AUTH_KEY', 'use WP auto gen key');
define('SECURE_AUTH_KEY', 'use WP auto gen key');
define('LOGGED_IN_KEY', 'use WP auto gen key');
define('NONCE_KEY', 'use WP auto gen key');
define('AUTH_SALT', 'use WP auto gen key');
define('SECURE_AUTH_SALT', 'use WP auto gen key');
define('LOGGED_IN_SALT', 'use WP auto gen key');
define('NONCE_SALT', 'use WP auto gen key');
?>
โค้ดพวกนี้ ก็ให้คุณเข้า
https://api.wordpress.org/secret-key/1.1/salt/
เพื่อสร้างโค้ด จากนั้น copy มาใส่ใน WP ทุกตัวของคุณ (WP ทุกตัวที่ต้องการแชร์ user ให้ใส่เหมือนกันทั้งหมด)
พอเสร็จขั้นตอนนี้แล้ว เซฟไฟล์ wp-config.php ได้เลย
ขั้นตอนที่ 2 เปิดไฟล์ wp-includes/capabilities.php ของทุกไซต์ที่คุณต้องการแชร์ user
ประมาณบรรทัดที่ 515 มันจะมีฟังก์ชัน
สำหรับ wp 3.0.x +
<?php
function _init_caps( $cap_key = '' ) {
global $wpdb;
if ( empty($cap_key) )
$this->cap_key = $wpdb->prefix . 'capabilities';
else
$this->cap_key = $cap_key;
$this->caps = &$this->{$this->cap_key};
if ( ! is_array( $this->caps ) )
$this->caps = array();
$this->get_role_caps();
}
?>
สำหรับ WP 2.9 <
<?php
function _init_caps() {
global $wpdb;
$this->cap_key = $wpdb->prefix . 'capabilities';
$this->caps = &$this->{$this->cap_key};
if ( ! is_array( $this->caps ) )
$this->caps = array();
$this->get_role_caps();
}
?>
copy โค้ดเดิมเก็บเอาไว้ด้วย
บรรทัด
แก้ไขบรรทัด
<?php $this->cap_key = $wpdb->prefix . 'capabilities'; ?>
ด้วย
<?php
if (defined ('CUSTOM_CAPABILITIES_PREFIX')) {
$this->cap_key = CUSTOM_CAPABILITIES_PREFIX . 'capabilities';
}
else { $this->cap_key = $wpdb->prefix . 'capabilities';
}
?>
โค้ดมันจะออกมาเป็น
2.9.x<
<?php
function _init_caps() {
global $wpdb;
/*$this->cap_key = $wpdb->prefix . 'capabilities';*/
if (defined ('CUSTOM_CAPABILITIES_PREFIX')) {
$this->cap_key = CUSTOM_CAPABILITIES_PREFIX . 'capabilities';
}
else { $this->cap_key = $wpdb->prefix . 'capabilities';
}
$this->caps = &$this->{$this->cap_key};
if ( ! is_array( $this->caps ) )
$this->caps = array();
$this->get_role_caps();
}
?>
3.0.x+
<?php
function _init_caps( $cap_key = '' ) {
global $wpdb;
if ( empty($cap_key) )
if (defined ('CUSTOM_CAPABILITIES_PREFIX')) {
$this->cap_key = CUSTOM_CAPABILITIES_PREFIX . 'capabilities';
}
else {
$this->cap_key = $wpdb->prefix . 'capabilities';
}
else
$this->cap_key = $cap_key;
$this->caps = &$this->{$this->cap_key};
if ( ! is_array( $this->caps ) )
$this->caps = array();
$this->get_role_caps();
}
?>
หลังจาก นั้นเซฟ และ ปิดไฟล์ไป
จากนั้นก็ อัพขึ้นโฮสไปทับไฟล์เดิม แล้วลอง เข้าเว็บที่เราอัพไฟล์ไปทับนั้น ล็อกอินเข้าไปด้วย user ของ บล็อกหลักดู ถ้าไม่มีอะไรผิดพลาด มันจะล็อกอินได้นะ
ยาวมาก - -
ในส่วนของ single sign on มันยังมี บั๊กอยู่ กำลังทดลองหลายๆ วิธี ที่จะทำให้ มันเป็น single sign on อยู่นะ
*** มันอาจจะมีบั๊กสำหรับ WP 3.0.x อยู่ ไว้จี้จะทดสอบแล้วก็ค่อยๆ แก้มันดูนะ***ทดสอบกับ 3.0.x เต็มรูปแบบแล้ว Share user DB ไม่มีปัญหาร้ายแรง แค่ตัวที่แชร์ไปมันไม่โชว์ว่า ใครเป็น admin แค่นั้น