Created
October 8, 2025 13:25
-
-
Save nicmare/03ff5df7db8e61f352ffa87d7acfa6db to your computer and use it in GitHub Desktop.
Make wordpress synced blocks public in rest api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // fetch block by name like this: | |
| // https://…/wp-json/public/v1/reusable?slug=brunch-termine | |
| add_action('rest_api_init', function () { | |
| register_rest_route('public/v1','/reusable',[ | |
| 'methods' => 'GET', | |
| 'args' => ['slug'=>['required'=>true]], | |
| 'permission_callback' => '__return_true', | |
| 'callback' => function(WP_REST_Request $r){ | |
| $slug = sanitize_title($r['slug']); | |
| $post = get_page_by_path($slug, OBJECT, 'wp_block'); | |
| if(!$post || 'publish'!==$post->post_status){ | |
| return new WP_Error('not_found','Block not found or not public',['status'=>404]); | |
| } | |
| return [ | |
| 'id' => $post->ID, | |
| 'slug' => $post->post_name, | |
| 'title' => get_the_title($post), | |
| 'rendered' => apply_filters('the_content', $post->post_content), | |
| ]; | |
| } | |
| ]); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment