Skip to content

Commit b717e4b

Browse files
krokodokclaudeocean90
authored
fix: grant read_post cap for preview post to enable Block Bindings (#225)
* fix: grant read_post cap for preview post to enable Block Bindings Since WordPress 6.5, Block Bindings sources (core/post-meta, core/post-data) check is_post_publicly_viewable() OR current_user_can('read_post') before returning a value. For public previews, the post is still a draft, so anonymous users fail both checks and all Block Bindings render empty. Hook into map_meta_cap to grant read_post for the specific preview post ID during the preview request. This requires no object cache manipulation and does not affect any other posts or requests. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * refactor: replace static property + method with closure for map_meta_cap The static $preview_post_id property and grant_read_post_for_preview() method are unnecessary — a closure with use ($post_id) keeps the post ID scoped directly, which is simpler and equally safe. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Use `exist` to grant preview user capabilites to read metadata Co-authored-by: Dominik Schilling <dominikschilling+git@gmail.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: Dominik Schilling <dominikschilling+git@gmail.com>
1 parent 62726ce commit b717e4b

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

public-post-preview.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,23 @@ public static function set_post_to_publish( $posts ) {
731731
// Set post status to publish so that it's visible.
732732
$posts[0]->post_status = 'publish';
733733

734+
// Allow Block Bindings sources to resolve for this post.
735+
add_filter(
736+
'map_meta_cap',
737+
static function( $caps, $cap, $user_id, $args ) use ( $post_id ) {
738+
if (
739+
'read_post' === $cap &&
740+
isset( $args[0] ) &&
741+
(int) $args[0] === $post_id
742+
) {
743+
return [ 'exist' ];
744+
}
745+
return $caps;
746+
},
747+
10,
748+
4
749+
);
750+
734751
// Disable comments and pings for this post.
735752
add_filter( 'comments_open', '__return_false' );
736753
add_filter( 'pings_open', '__return_false' );

0 commit comments

Comments
 (0)