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:
- Go to the new case study and set a new reusable block up
- Go to every page with the old reusable block and set the new one
- 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
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:
- Go to the new case study and set a new reusable block up
- Go to every page with the old reusable block and set the new one
- 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
Share Improve this question asked Dec 23, 2018 at 15:31 Toby OsborneToby Osborne 1931 silver badge10 bronze badges 1- It is also possible to do that by creating a new block, however, due to few people knowing how to build blocks, this is probably not an option? In the edit function, you shoud use select component and fill it with a name of posts to choose post ID, from that ID get post content by using getEntityRecord('postType', 'post', ID, {_fields: 'ID, content'}) and retreive content from there to build a static block. If dynamic block is needed (more likely, to auto-refresh), you can save ID to attribute and get post content in block render function. – Lovor Commented May 1, 2023 at 22:14
1 Answer
Reset to default 7WordPress 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.