1

Bit of a vague question and I'm certain that everyone will say "Use reusable blocks", but I'm hoping I'm just missing something.

So a use case is,

I feature a case study on my homepage, and I want to pull the testimonial block from that case study (a custom post type); So that I only have to maintain the text in one place. No problem, that's what the reusable blocks are for.

My issue is, if I wanted to change the featured case study shown on the home page, I would have to:

  1. Go to the new case study and set a new reusable block up
  2. Go to every page with the old reusable block and set the new one
  3. Remove the old reusable block (else eventually I'll end up with hundreds)

Or am I wrong, (Remember that each case study will have a unique testimonial; so it wouldn't be a case of changing the text in the current reusable as that would change it on the initial case study correct?)

Now for me, this isn't too difficult, but moving forward on client sites, the whole process would be far too confusing for them (I mean, most seem to have trouble adding text to a box).

Basically, is there a way to get a specific block from a specified page, without jumping through hoops? Ya'know like select a post, get a list of blocks in the post, then select the block you want?

Thanks

3

WordPress provides a way to get an array of blocks for a page with the parse_blocks() function. If you extract from that the specific block(s) you are interested in, you can then use render_block() to put the block on another page. This function would do that:

function example_extract_block_type_from_page( $post_id, $block_name ) {

    //get post_content for page
    $post_content = get_post( $post_id )->post_content;

    //get all blocks of requested type
    $blocks = array_filter( parse_blocks( $post_content ), function( $block ) use( $block_name ) {
        return $block_name === $block['blockName'];
    });

    $block_content = '';
    foreach( $blocks as $block ) {
        $block_content .= render_block( $block );
    }

    return $block_content;
}

You didn't say where you wanted the testimonial to go on the other pages. If it's outside of the main post content, then you could use a custom template and/or hooks to place it where you want. If it's inside the main post content, then a custom shortcode might be the best way to do it.

function example_extract_block_type_from_page_shortcode( $atts=[] ) {
    $defaults = [
        'post_id' => 0,
        'block_name' => ''
    ];
    $atts = shortcode_atts( $defaults, $atts );

    return example_extract_block_type_from_page( $atts['post_id'], $atts['block_name'] );
}
add_shortcode( 'example_extract_block_type_from_page', 'example_extract_block_type_from_page_shortcode' );

Then something like [example_extract_block_type_from_page post_id="123" block_name="myblock"] would show the relevant block content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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