Get reusable block in php
Asked Answered
D

5

7

For the life of me, I can't find anything on how to do this: simply output a reusable gutenberg block via php in a theme template. Seems like it should be doable. Anyone?

Daigle answered 27/3, 2019 at 16:33 Comment(0)
D
12

Possibly answering my own question. Please tell me if there's a better/easier way to do this.

<?php
    // get reusable gutenberg block:
    $gblock = get_post( 7418 );
    echo apply_filters( 'the_content', $gblock->post_content );
?>

The first downside I can see to this is that it's inconvenient to have to hunt down the post ID of the block.

Daigle answered 27/3, 2019 at 17:8 Comment(5)
How do you find the ID of your reusable block?Anchorage
Only way I know of is to go here: /wp-admin/edit.php?post_type=wp_block Then hover over the edit link for your block and look at the ID in the URL.Daigle
@Daigle It should be easier to get the gutenberg block (not the ID, the content itself), I also spend a lot of time searching for a straight solution and no way...Bouffant
site.com/wp-admin/edit.php?post_type=wp_blockTownsley
I agree. I'd like a solution that uses something like get_page_by_title() because the ID will change when moving the theme to a new site.Hairbreadth
C
4

I just found this handy little snippet. It adds the Reusable blocks as an admin link. Once there you can easily determine the ID of the reusable block that you need. https://github.com/WordPress/gutenberg/issues/15549

add_menu_page( 'linked_url', 'Reusable Blocks', 'read', 'edit.php?post_type=wp_block', '', 'dashicons-editor-table', 22 );
}

Chromatism answered 17/10, 2019 at 22:4 Comment(1)
Thank you so much! I don't know why Wordpress doesn't have this enabled by default.Carrion
H
2

As pointed out by gtamborero here, you can use get_page_by_title(), but you need to specify that this is a 'wp_block'. His example works for me (using WP 5.8.1):

get_page_by_title( 'Your Title', OBJECT, 'wp_block' );

I'm using it like this:

$myPost = get_page_by_title( 'Your Title', OBJECT, 'wp_block' );
$myContent = apply_filters('the_content', $myPost->post_content);
echo $myContent;
Hairbreadth answered 24/9, 2021 at 20:42 Comment(0)
S
2

Combining several answers into an approach with WP_Query, this is a function that can be reused all over the place. Place it in functions.php and call it from templates with get_reusable_block('block-title');.

function get_reusable_block($block) {
// important to have post_type as 'wp_block' since it is a reusable block
$query = new WP_Query(
    array(
        'post_type'              => 'wp_block',
        'title'                  => $block,
        'post_status'            => 'published',
        'posts_per_page'         => 1,
    )
);

if (!empty($query->post)) {
    $reusable_block = $query->post;
    $reusable_block_content = apply_filters('the_content', $reusable_block->post_content);
    return $reusable_block_content;
} else {
    return '';
}

}

Sarmentose answered 29/5, 2023 at 9:26 Comment(0)
O
1

There is a way to query by title without using get_page_by_title (depreciated in 6.2). We can use wp_query() with the wp_blocks post type where reusable blocks are stored.

In the example here, it asks for a wp_block that is titled "Global Call To Action" and published.

$query = new WP_Query(
    array(
        'post_type'              => 'wp_block',
        'title'                  => 'Global Call To Action',
        'post_status'            => 'publish',
        'posts_per_page'         => 1
    )
);
 
if ( $query->have_posts() ) {
    $object = $query->post;
    echo apply_filters('the_content', $object->post_content);
}

wp_reset_postdata();
Overdue answered 30/3, 2023 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.