多站点的隔离 X-profile 字段

多站点的隔离 X-profile 字段

Codex Home → BuddyPress Theme Development → User Submitted Guides → Segregated X-Profile Fields for Multisite
Segregated X-Profile Fields for Multisite

Out of the box, all sites in a multisite install will share BuddyPress tables. That means that with a component like Extended Profiles, all sites will share the same profile fields, and changing them in one site will affect all others. However, with a few modifications you can create separate tables for each site, so Extended Profile Fields work independently on the network.
To do this, you need to modify the DB create queries for new sites, so when creating a new site, the tables for Extended Profiles will be automatically created. Once the tables are created, the $bp global entries for these table names will need to be modified, so at runtime the specific site』s newly created Extended Profile tables are accessed, not the shared ones.
This code will not create dummy content, so for every new site you will need to create a Profile Fields group and add profile fields to it manually. Add the following code to a functionality plugin network activated on your multisite install:

add_action('dbdelta_create_queries', 'add_custom_bp_xprofile_tables');

function add_custom_bp_xprofile_tables($sql) {
global $wpdb;
if ( ! empty($wpdb->charset) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty($wpdb->collate) )
$charset_collate .= " COLLATE $wpdb->collate";

$sql[] = "CREATE TABLE {$wpdb->prefix}bp_xprofile_groups (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(150) NOT NULL,
description mediumtext NOT NULL,
group_order bigint(20) NOT NULL DEFAULT '0',
can_delete tinyint(1) NOT NULL,
KEY can_delete (can_delete)
) {$charset_collate};";

$sql[] = "CREATE TABLE {$wpdb->prefix}bp_xprofile_fields (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
group_id bigint(20) unsigned NOT NULL,
parent_id bigint(20) unsigned NOT NULL,
type varchar(150) NOT NULL,
name varchar(150) NOT NULL,
description longtext NOT NULL,
is_required tinyint(1) NOT NULL DEFAULT '0',
is_default_option tinyint(1) NOT NULL DEFAULT '0',
field_order bigint(20) NOT NULL DEFAULT '0',
option_order bigint(20) NOT NULL DEFAULT '0',
order_by varchar(15) NOT NULL DEFAULT '',
can_delete tinyint(1) NOT NULL DEFAULT '1',
KEY group_id (group_id),
KEY parent_id (parent_id),
KEY field_order (field_order),
KEY can_delete (can_delete),
KEY is_required (is_required)
) {$charset_collate};";

$sql[] = "CREATE TABLE {$wpdb->prefix}bp_xprofile_data (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
field_id bigint(20) unsigned NOT NULL,
user_id bigint(20) unsigned NOT NULL,
value longtext NOT NULL,
last_updated datetime NOT NULL,
KEY field_id (field_id),
KEY user_id (user_id)
) {$charset_collate};";

$sql[] = "CREATE TABLE {$wpdb->prefix}bp_xprofile_meta (
id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
object_id bigint(20) NOT NULL,
object_type varchar(150) NOT NULL,
meta_key varchar(255) DEFAULT NULL,
meta_value longtext DEFAULT NULL,
KEY object_id (object_id),
KEY meta_key (meta_key)
) {$charset_collate};";
return $sql;
}

add_action( 'bp_init', 'modify_bp_global_xprofiles_tablenames' );
add_action( 'switch_blog', 'modify_bp_global_xprofiles_tablenames' );

function modify_bp_global_xprofiles_tablenames() {
global $bp, $wpdb;
$bp->profile->table_name_data = $wpdb->prefix . 'bp_xprofile_data';
$bp->profile->table_name_groups = $wpdb->prefix . 'bp_xprofile_groups';
$bp->profile->table_name_fields = $wpdb->prefix . 'bp_xprofile_fields';
$bp->profile->table_name_meta = $wpdb->prefix . 'bp_xprofile_meta';
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注