問題描述

設置 WP 多站點實例 – 客户端有一個現有的本體/一組類別,他們希望將所有內容分類到整個博客中。此外,願望是在’network blog’ 級別添加任何新類別並同步到其他博客。

這樣做最好的方法是什麼?

最佳解決方案

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' );

每當在主站點上添加類別時,這將運行。值得一提的幾點注意事項

  • 如果你有很多博客,這個功能可能會變得非常密集。

  • 平均來説,我們在每個博客之間的任何地方可以在 5 到 8 個查詢 (可能更多) 之間運行 – 取決於數據庫的速度,此功能可能需要分塊。

  • 只有新增的類別是’synced’ 。更新和刪除類​​別不是 (代碼將需要修改) 。

  • 如果新添加的類別有父級,並且在有問題的多站點博客中找不到父級,則該類別將在沒有父級的情況下創建 (只有在安裝此功能之前創建父類別時才應用) 。

次佳解決方案

哦,星期日拖延…

https://github.com/maugly/Network-Terminator

  • 在網絡上批量添加條款

  • 您可以選擇哪些網站將受到影響

  • 適用於自定義分類法

  • 不刪除

  • 不同步

這是我在過去幾個小時內完成的工作,現在我沒有時間進行更多的測試。無論如何 – 它適合我! 。)

試一試。還實現了一個’test run’ 功能,因此您可以在實際執行某些操作之前檢查結果。

更新 – > 截圖:

行動前:

試運行後:

上面插入的插件添加了用户界面,但幾乎所有重要事情發生在此功能中:

        <?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;
    } ?>

我會回來再編輯這個更多的信息 (如果需要) 。

它遠不完美 (在插件頭中看到已知的問題) 。任何反饋讚賞!

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。