Is it possible to get the current post ID inside set_render_callback()?
Version
- Carbon Fields: 3.2
- WordPress: 5.8.2
- PHP: 7.4
Container definition
Block::make(__('child-pages', $this->text_domain))
...
->set_render_callback(function ($fields, $attributes, $inner_blocks) {
// Do something with $post->ID
});
}
Comments
I'm trying to find a way to access the current post's ID within the render callback but so far a solution eludes me. I've tried the usual suspects (global $post etc) but short of modifying Carbon so I can pass it in, I can't see a way to get access to the $post object.
Any clues are gratefully received!
Hi @oobi
Try using $_GET['post']
Yeah I tried that and $_POST and $_REQUEST. The trouble I had is it's going through the Wordpress REST process (in the editor) and that stuff doesn't appear to exist in that context.
/wp-json/carbon-fields/v1/block-renderer?_locale=user
I see, I will check that and will let you know
Has anyone managed to solve this problem?
Sadly I found no way to do this without hacking carbon fields itself. An extra param in the set_render_callback would be amazing.
Hi @oobi ,
We're going to address this in a future release.
We will add an additional 4th parameter In the callback function, which would be the post_id.
That would be amazing - thanks for considering it!
On Tue, 21 Feb 2023 at 8:51 pm, HTMLBurger-NG @.***> wrote:
Hi @oobi https://github.com/oobi ,
We're going to address this in a future release. We will add an additional 4th parameter In the callback function, which would be the post_id.
— Reply to this email directly, view it on GitHub https://github.com/htmlburger/carbon-fields/issues/1059#issuecomment-1438174721, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD22LFPGPEGG5XMKMRFLH3WYSFY3ANCNFSM5IYC36PA . You are receiving this because you were mentioned.Message ID: @.***>
Hi @oobi ,
This new parameter was added in the latest release v3.5.1 so if you'd like you could check it out.
That works a treat - thanks a bunch!
I spoke too soon. This works in the editor (preview the block) but not on the front end. What I'm hoping for is access to the post_id for the purpose of running query to access (for example) child pages or post metadata.
Example:
->set_render_callback(function ($fields, $attributes, $inner_blocks, $post_id) {
echo 'Post ID is ' . $post_id;
});
In the editor (preview):
On the front end:
Having a little dig into the code for Block_Container, the get_post_id method seems to rely on the post ID being present in the request or admin URL, which is not the case for the front end.
admin: /wp-admin/post.php?post=5&action=edit
frontend: /path/to/post
Actually here's a little patch for that method that allows it to work in both places:
public function get_post_id()
{
$post_id = isset($_GET['post']) && (int) $_GET['post'] > 0 ? (int) $_GET['post'] : null;
if (!empty($post_id)) {
return $post_id;
}
$admin_url = $_SERVER['HTTP_REFERER'];
if (!empty($admin_url)) {
$parsed_url = parse_url($admin_url);
if (isset($parsed_url['query']) && !empty($parsed_url['query'])) {
$params = explode('&', $parsed_url['query']);
foreach ($params as $param) {
$param = explode('=', $param);
if ($param[0] === 'post') {
$post_id = $param[1];
}
}
}
}
// grab post_id on front end render
if (empty($post_id)) {
$post_id = get_the_ID();
}
return $post_id;
}
Hi @oobi,
Thank you for your patch, we've applied it on our next release, currently visible in the development branch.
No problem -- another quick change:
$admin_url = $_SERVER['HTTP_REFERER'] ?? '';
Referrer isn't guaranteed to be set. I bumped into this using webpack.
Thank you again, we've already fixed this issue in the development branch.