2

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.

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
    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
    Mar 21 at 13:06

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.