Repository navigation
Conversation
Fetch post content directly via get_post_field() with the block context postId rather than relying on get_the_content(). This prevents reading time from being incorrectly truncated when rendered inside a Query Loop on a Page or when <!--more--> tags are present.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @Labyala. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
🤖 PR meta 🤖👋 Welcome👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Malayt04! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
db1c560 to
3f2e221
Compare
| } | ||
|
|
||
| $content = get_the_content(); | ||
| $content = get_post_field( 'post_content', $block->context['postId'] ); |
There was a problem hiding this comment.
This may have security implications, for example on a password protected post we would provide information about its lenght.
I'm not sure if the fix is right on packages/block-library/src/post-template/index.php we call $query->the_post() , so in teory get_the_content should work. Maybe we have an issue elsewhere. @Malayt04 would you be able to provide the markup you used for testing on your before video? Thank you in advance.
There was a problem hiding this comment.
@jorgefilipecosta , thanks for the review, I should have shared the markup as well initially. The issue is not present normally, but when you put a More block (<!--more-->), this issue arises.
While $query->the_post() sets up the post in the loop, in a Query Loop (listing context) WordPress sets $more = 0 (teaser mode):
- When a post contains a
<!--more-->block,get_the_content()intentionally cuts off all content after that tag and returns only the teaser - Using
get_post_field( 'post_content', $block->context['postId'] )ensures we retrieve the complete article content
You are right about the password protected posts, we can add a post_password_required( $block->context['postId'] ) check so the block returns '' when a post is locked (something like this). Do let me know if this sounds reasonable, I will update the PR
Test Markup:
1. Post
<!-- wp:paragraph -->
<p>Short intro before the more tag...</p>
<!-- /wp:paragraph -->
<!-- wp:more -->
<!--more-->
<!-- /wp:more -->
<!-- wp:paragraph -->
<p>1,500+ words of article body text here so that you can differentiate between time to read</p>
<!-- /wp:paragraph -->
Fixes: #82971
What?
Updates
core/post-time-to-readserver-side rendering to fetch post content directly viaget_post_field( 'post_content', $block->context['postId'] )rather than relying onget_the_content().Why?
When
core/post-time-to-readis used inside a Query Loop on a Page, the server-side callback usedget_the_content()which relies on the global$postand query state rather than the post provided by the block's context.This caused the frontend to display an incorrect reading time (e.g. 1 minute instead of 8–10 minutes) and differed from the Block Editor (
edit.js), which correctly reads the full post content directly from the post entity in context.How?
packages/block-library/src/post-time-to-read/index.php, replacedget_the_content()withget_post_field( 'post_content', $block->context['postId'] ). This retrieves the full content directly for the specific post in context.test_uses_block_context_post_id_not_global_post) inphpunit/blocks/render-post-time-to-read-test.phpto verify that the block reads from contextpostIdeven when$GLOBALS['post']is set to a different post.packages/block-library/CHANGELOG.mdunder## Unreleased.Testing Instructions
/blog-page/) and insert a Query Loop block.core/post-time-to-read).Testing Instructions for Keyboard
N/A (PHP server-side render fix only).
Screenshots or screencast
Before (Issue)
Before.mov
After (Fix)
After.mov
Use of AI Tools
AI assistance was used to draft the PHPUnit regression test using opencode.