设置WP multisite实例-客户端有一个现有的本体/类别集,他们希望对博客集上的所有内容进行分类。我们还希望在“网络博客”级别添加任何新类别,并与其他博客同步。
最好的方法是什么?
设置WP multisite实例-客户端有一个现有的本体/类别集,他们希望对博客集上的所有内容进行分类。我们还希望在“网络博客”级别添加任何新类别,并与其他博客同步。
最好的方法是什么?
function __add_global_categories( $term_id )
{
if ( get_current_blog_id() !== BLOG_ID_CURRENT_SITE || ( !$term = get_term( $term_id, \'category\' ) ) )
return $term_id; // bail
if ( !$term->parent || ( !$parent = get_term( $term->parent, \'category\' ) ) )
$parent = null;
global $wpdb;
$blogs = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = \'{$wpdb->siteid}\'" );
foreach ( $blogs as $blog ) {
$wpdb->set_blog_id( $blog );
if ( $parent && ( $_parent = get_term_by( \'slug\', $parent->slug, \'category\' ) ) )
$_parent_ID = $_parent->term_id;
else
$_parent_ID = 0;
wp_insert_term( $term->name, \'category\', array(
\'slug\' => $term->slug,
\'parent\' => $_parent_ID,
\'description\' => $term->description
));
}
$wpdb->set_blog_id( BLOG_ID_CURRENT_SITE );
}
add_action( \'created_category\', \'__add_global_categories\' );
只要在主站点上添加了类别,就会运行此操作。一些值得一提的注意事项/要点;如果你有很多博客,这个功能可能会非常密集
哦,周日的拖延
https://github.com/maugly/Network-Terminator
<要在整个网络中批量添加术语,您可以选择哪些站点将受到影响,这些站点可以使用自定义分类法,但不会删除,也不会同步。这是我在过去几个小时内完成的工作,现在没有时间进行更多测试。无论如何,这对我来说很有用!)试试看。还实现了“测试运行”功能,因此您可以在实际执行操作之前检查结果。
Update -> Screenshots:
行动前:
试运行后:
上面链接的插件添加了用户界面,但该功能中几乎发生了所有重要的事情:
<?php function mau_add_network_terms($terms_to_add, $siteids, $testrun = false) {
// check if this is multisite install
if ( !is_multisite() )
return \'This is not a multisite WordPress installation.\';
// very basic input check
if ( empty($terms_to_add) || empty($siteids) || !is_array($terms_to_add) || !is_array($siteids) )
return \'Nah, I eat only arrays!\';
if ($testrun) $log = \'<p><em>No need to get excited. This is just a test run.</em></p>\';
else $log = \'\';
// loop thru blogs
foreach ($siteids as $blog_id) :
switch_to_blog( absint($blog_id) );
$log .= \'<h4>\'.get_blog_details( $blog_id )->blogname.\':</h4>\';
$log .= \'<ul id="ntlog">\';
// loop thru taxonomies
foreach ( $terms_to_add as $taxonomy => $terms ) {
// check if taxonomy exists
if ( taxonomy_exists($taxonomy) ) {
// get taxonomy name
$tax_name = get_taxonomy($taxonomy);
$tax_name = $tax_name->labels->name;
//loop thru terms
foreach ( $terms as $term ) {
// check if term exists
if ( term_exists($term, $taxonomy) ) {
$log .= "<li class=\'notice\' ><em>$term already exists in the $tax_name taxonomy - not added!</em></li>";
} else {
// if it doesn\'t exist insert the $term to $taxonomy
$term = strip_tags($term);
$taxonomy = strip_tags($taxonomy);
if (!$testrun)
wp_insert_term( $term, $taxonomy );
$log .= "<li><b>$term</b> successfully added to the <b>$tax_name</b> taxonomy</li>";
}
}
} else {
// tell our log that taxonomy doesn\'t exists
$log .= "<li class=\'notice\'><em>The $tax_name taxonomy doesn\'t exist! Skipping...</em></li>";
}
}
$log .= \'</ul>\';
// we\'re done here
restore_current_blog();
endforeach;
if ($testrun) $log .= \'<p><em>No need to get excited. This was just the test run.</em></p>\';
return $log;
} ?>
我会回来编辑更多信息稍后(如果需要)。这远远不够完美(请阅读插件头部的已知问题)<欢迎您的反馈!
死亡医生的答案看起来不错,但我最终采取了不同的方法来解决这个问题。我没有在许多网站上重复相同的术语,而是让其他网站使用主页的术语表。
add_action(\'init\', \'central_taxonomies\');
function central_taxonomies () {
global $wpdb;
$wpdb->terms = "wp_terms";
$wpdb->term_taxonomy = "wp_term_taxonomy";
}
这将替换表名wp_2_terms
具有wp_terms
, 当然,您应该检查数据库以确保表的确切名称,如果更改前缀,名称可能会有所不同。您可以从插件或主题运行它(尽管我建议使用插件)。我可能会在某个时候发布一个插件来实现这一点。这种方法有两个缺点:
它只在插件已激活的子网站上有效。无法从父站点强制执行此操作all 分类法,而不仅仅是选定的分类法
<小时>Update: 我已经将其制作成一个插件,如果需要,可以在整个站点范围内激活它:MU Central Taxonomies
是的,这是可能的。很久以前,我为WPMU构建了一个这样的插件(http://natureofmind.org/30/default-categories-for-new-blogs/但不再支持)以下两个插件将是最新的:http://wordpress.org/extend/plugins/wpmu-new-blog-defaults/ 和http://premium.wpmudev.org/project/new-blog-template
作为对@Marcus Downing answer的补充,我添加了一个保护,以防止在删除MU站点时删除表。
这旨在用作mu插件。如果要在其他地方使用,请更改;muplugins\\u已加载“;挂钩到“;已加载插件;。
<?php
/**
* Plugin name: Shared Taxonomies for Multi-site
* Version: 0.1
* Plugin Author: Marcus Downing, Lucas Bustamante
* Author URI: https://wordpress.stackexchange.com/a/386318/27278
* Description: All instances of multi-site share the same taxonomies as the main site of the network.
*/
$sharedTaxonomies = new SharedTaxonomies;
add_action(\'muplugins_loaded\', [$sharedTaxonomies, \'shareTaxonomies\'], 1, 0);
add_action(\'switch_blog\', [$sharedTaxonomies, \'shareTaxonomies\'], 1, 0);
add_filter(\'wpmu_drop_tables\', [$sharedTaxonomies, \'protectGlobalTables\'], 1, 2);
class SharedTaxonomies
{
/**
* Multi-sites uses term tables from the main site.
*/
public function shareTaxonomies()
{
/**
* @var wpdb $wpdb
* @var WP_Object_Cache $wp_object_cache
*/
global $wpdb, $wp_object_cache;
$wpdb->terms = $wpdb->base_prefix . \'terms\';
$wpdb->term_taxonomy = $wpdb->base_prefix . \'term_taxonomy\';
$wpdb->flush();
$wp_object_cache->flush();
}
/**
* Prevent global tables from being deleted when deleting a mu-site
*
* @param array $tables The tables to drop when deleting a mu-site.
* @param int $siteId The ID of the mu-site being deleted.
*
* @return array The tables to drop when deleting a mu-site, excluding the protected ones.
*/
public function protectGlobalTables($tables, $siteId)
{
$protectedTables = [\'terms\', \'term_taxonomy\'];
return array_filter($tables, function ($tableName) use ($protectedTables) {
return !in_array($tableName, $protectedTables);
}, ARRAY_FILTER_USE_KEY);
}
}
我有初级域名。com和一个附加域。com公司附加域。com指向primarydomain。com/附加域我已经在primarydomain上分别安装了2次wordpress。com和primarydomain。com/附加域我可以激活primarydomain上的多站点吗。com/附加域作为附加域。com?我这样做的原因是这是两个独立的站点,有两个独立的功能,所以我想使用独立的数据库。这可能吗?