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?

5 Answers 5

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.

5
  • 1
    How do you find the ID of your reusable block? Commented Feb 11, 2020 at 19:18
  • 2
    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. Commented Feb 13, 2020 at 15:10
  • @protohominid 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...
    – Capiedge
    Commented Mar 9, 2020 at 10:58
  • site.com/wp-admin/edit.php?post_type=wp_block Commented Feb 9, 2021 at 4:03
  • 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.
    – red5
    Commented Sep 24, 2021 at 20:34
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 );
}

1
  • Thank you so much! I don't know why Wordpress doesn't have this enabled by default.
    – CodeMonkey
    Commented Dec 20, 2020 at 3:35
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;
0
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 '';
}

}

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();

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