carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Is it possible to get the current post ID inside set_render_callback()?

Open oobi opened this issue 4 years ago • 4 comments

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!

oobi avatar Nov 25 '21 09:11 oobi

Hi @oobi

Try using $_GET['post']

jorostoyanov avatar Nov 25 '21 10:11 jorostoyanov

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

oobi avatar Nov 25 '21 10:11 oobi

I see, I will check that and will let you know

jorostoyanov avatar Nov 25 '21 13:11 jorostoyanov

Has anyone managed to solve this problem?

SAYri-ua avatar Mar 29 '22 09:03 SAYri-ua

Sadly I found no way to do this without hacking carbon fields itself. An extra param in the set_render_callback would be amazing.

oobi avatar Nov 24 '22 00:11 oobi

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.

HTMLBurger-NG avatar Feb 21 '23 09:02 HTMLBurger-NG

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: @.***>

oobi avatar Feb 21 '23 10:02 oobi

Hi @oobi ,

This new parameter was added in the latest release v3.5.1 so if you'd like you could check it out.

HTMLBurger-NG avatar Feb 27 '23 14:02 HTMLBurger-NG

That works a treat - thanks a bunch!

oobi avatar Mar 02 '23 05:03 oobi

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):

image

On the front end:

image

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

oobi avatar Mar 03 '23 02:03 oobi

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;
	}

oobi avatar Mar 03 '23 02:03 oobi

Hi @oobi,

Thank you for your patch, we've applied it on our next release, currently visible in the development branch.

HTMLBurger-NG avatar Mar 07 '23 13:03 HTMLBurger-NG

No problem -- another quick change:

$admin_url = $_SERVER['HTTP_REFERER'] ?? '';

Referrer isn't guaranteed to be set. I bumped into this using webpack.

oobi avatar Mar 07 '23 21:03 oobi

Thank you again, we've already fixed this issue in the development branch.

HTMLBurger-NG avatar Mar 08 '23 08:03 HTMLBurger-NG