admin管理员组文章数量:1391918
I'm trying to add a "lazy" field to the "content" endpoint using "rest_prepare_post".
I do this by adding "rest_prepare_post" filter. Then I check if "$post->post_content" has gutenberg blocks. After that I search in the loop all those that have blockName set as "core/image". Then I edit the contents of this block.
My problem is to put a changed HTML into "content" field, which will be a copy of "content[rendered]". Below I insert the code.
<?php
class EndpointLazyLoading
{
public function __construct()
{
add_filter( 'rest_prepare_post', [ $this, 'add_content_lazy_field' ], 1, 3);
}
public function add_content_lazy_field($response, $post, $request)
{
if (has_blocks($post->post_content)) {
$blocks = parse_blocks($post->post_content);
foreach ($blocks as &$block) {
if (isset($block['blockName']) && $block['blockName'] === 'core/image') {
// some magic with $block['innerContent'][0] and $block['innerHTML']
}
}
unset($block);
$response->data['content']['lazy'] = [
'markup' => post_password_required($post)
? ''
: '^^^^^^^^content that i try to add^^^^^^^^',
'protected' => (bool) $post->post_password
];
}
}
}
I want to see something like this:
GET domain/blog/wp-json/wp/v2/posts/6222?_lazy
{
"id": 6222,
(...),
"content": {
"rendered": "original content",
"protected": false,
"lazy": {
"markup": "original content with changed image block (wp:image block)"
}
},
(...)
}
I'm trying to add a "lazy" field to the "content" endpoint using "rest_prepare_post".
I do this by adding "rest_prepare_post" filter. Then I check if "$post->post_content" has gutenberg blocks. After that I search in the loop all those that have blockName set as "core/image". Then I edit the contents of this block.
My problem is to put a changed HTML into "content" field, which will be a copy of "content[rendered]". Below I insert the code.
<?php
class EndpointLazyLoading
{
public function __construct()
{
add_filter( 'rest_prepare_post', [ $this, 'add_content_lazy_field' ], 1, 3);
}
public function add_content_lazy_field($response, $post, $request)
{
if (has_blocks($post->post_content)) {
$blocks = parse_blocks($post->post_content);
foreach ($blocks as &$block) {
if (isset($block['blockName']) && $block['blockName'] === 'core/image') {
// some magic with $block['innerContent'][0] and $block['innerHTML']
}
}
unset($block);
$response->data['content']['lazy'] = [
'markup' => post_password_required($post)
? ''
: '^^^^^^^^content that i try to add^^^^^^^^',
'protected' => (bool) $post->post_password
];
}
}
}
I want to see something like this:
GET domain/blog/wp-json/wp/v2/posts/6222?_lazy
{
"id": 6222,
(...),
"content": {
"rendered": "original content",
"protected": false,
"lazy": {
"markup": "original content with changed image block (wp:image block)"
}
},
(...)
}
Share
Improve this question
edited Feb 5, 2020 at 12:52
Marek
asked Feb 5, 2020 at 12:21
MarekMarek
32 bronze badges
1 Answer
Reset to default 0Add this before your $response->data[...
to render changed blocks
$markup = '';
foreach ( $blocks as $block ) {
$markup .= render_block( $block );
}
本文标签: filtersHow to add lazy field in content endpoint using Gutenberg blocks
版权声明:本文标题:filters - How to add lazy field in content endpoint using Gutenberg blocks 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744775365a2624589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论