admin管理员组

文章数量:1123932

How can I share reusable blocks from the main site to all subfolder sites?

Background: I am in the planning stages of a WordPress multisite where I plan to write a custom theme from scratch and use the block editor for page layouts. The page is for an organization that has multiple locations, one main national site and a subfolder site for each location. This is needed to restrict editors to their own location. The page will rely heavily on reusable blocks and block patterns. Block patterns I will code into the theme.

How can I share reusable blocks from the main site to all subfolder sites?

Background: I am in the planning stages of a WordPress multisite where I plan to write a custom theme from scratch and use the block editor for page layouts. The page is for an organization that has multiple locations, one main national site and a subfolder site for each location. This is needed to restrict editors to their own location. The page will rely heavily on reusable blocks and block patterns. Block patterns I will code into the theme.

Share Improve this question asked Jun 14, 2022 at 8:07 jtveterjtveter 212 bronze badges 2
  • Reusable blocks are just posts. I have decided to save the blocks to multiple sites at the same time, and add a UI to select what sites to save it to. – jtveter Commented Jun 27, 2022 at 11:55
  • How exactly did you do that, is another plugin involved? Would this work with 150 network sites? I want to sync the legal pages so that I case of minor changes I wouldn't have to edit 150 pages. – w-sky Commented Mar 21, 2024 at 13:06
Add a comment  | 

1 Answer 1

Reset to default 1

I found a solution here: https://krautpress.de/2020/wiederverwendbare-bloecke-zwischen-sites-einer-multisite-synchronisieren/

/**
 * Sync the reusable blocks between the sites of the multisite.
 * 
 * @param int $post_id The post id.
 * @param WP_Post $post The post object.
 */
add_action( 'publish_wp_block', 'slug_sync_reusable_blocks', 10, 2  );
function slug_sync_reusable_blocks( $post_id, $post ) {
    // Check if the post already has a sync_hash or create one.
    if ( get_post_meta( $post_id, 'sync_hash', true ) !== '' ) {
        $sync_hash = get_post_meta( $post_id, 'sync_hash', true );
    } else {
        $sync_hash = uniqid( $post->post_title );
        update_post_meta( $post_id, 'sync_hash', $sync_hash );
    }
    $current_site_id = get_current_blog_id();
    
    // Get the site IDs.
    $sites = get_sites(
        [
            'fields' => 'ids',
        ]
    );
    
    remove_action( 'publish_wp_block', 'slug_sync_reusable_blocks', 10 );
    foreach ( $sites as $site_id ) {
        if ( $current_site_id !== $site_id ) {
            switch_to_blog( $site_id );
            // Check if we already synced the block and only need to update it.
            $existing_block_query = new WP_Query(
                [
                    'post_type' => 'wp_block',
                    'no_found_rows' => true,
                    'update_post_meta_cache' => false,
                    'update_post_term_cache' => false,
                    'fields' => 'ids',
                    'meta_key' => 'sync_hash',
                    'meta_value' => $sync_hash,
                    'posts_per_page' => 1,
                ]
            );
            
            if ( $existing_block_query->have_posts() ) {
                // Update the post.
                $existing_post_id = $existing_block_query->posts[0];
                wp_update_post(
                    [
                        'ID' => $existing_block_query->posts[0],
                        'post_content' => $post->post_content,
                        'post_title' => $post->post_title,
                        'post_status' => 'publish',
                    ]
                );
            } else {
                // Create the post if we do not have the block already.
                wp_insert_post(
                    [
                        'post_type' => 'wp_block',
                        'post_content' => $post->post_content,
                        'post_title' => $post->post_title,
                        'post_status' => 'publish',
                        'meta_input' =>
                        [
                            'sync_hash' => $sync_hash,
                        ],
                    ]
                );
            }
            restore_current_blog();
        }
    }
    add_action( 'publish_wp_block', 'slug_sync_reusable_blocks', 10, 2  ); 
}

I added the code to my site with the "Code Snippets" plugin and it's working great. If you have a "master" site like I have and all other multisites should use exactly the same reusable template block, the code only has to be activated on that master site.

本文标签: multisiteShare reusable blocks between network sites