You can not only pass a string as the render_callback
. You could also pass an anonymous function, plus PHP treats an [$object, 'methodName']
array as a callable.
Here’s the Block
class i came up with. Check register()
to see how it specifies an instance method as the render_callback.
namespace Foo;
use Timber\Timber;
abstract class Block
{
abstract public function getName(): string;
abstract protected function getTitle(): string;
abstract protected function getDescription(): string;
abstract protected function getCategory(): string;
abstract protected function getIcon(): string;
abstract protected function getKeywords(): array;
abstract protected function getTemplateFilename(): string;
abstract protected function defineFields(): array;
protected function filterSupports(array $supports): array
{
return $supports;
}
protected function filterContext(array $context): array
{
return $context;
}
public function register()
{
acf_register_block_type([
'name' => $this->getName(),
'title' => $this->getTitle(),
'description' => $this->getDescription(),
'category' => $this->getCategory(),
'icon' => $this->getIcon(),
'keywords' => $this->getKeywords(),
'render_callback' => [$this, 'render'],
'mode' => 'edit',
'supports' => $this->filterSupports([
'align' => false,
'align_text' => false,
'alignt_content' => false,
'mode' => true,
'multiple' => true,
]),
]);
$this->registerFields();
}
private function registerFields()
{
acf_add_local_field_group([
'key' => 'group_block_' . $this->getName(),
'title' => $this->getTitle(),
'fields' => $this->defineFields(),
'active' => true,
'location' => [
[
[
'param' => 'block',
'operator' => '==',
'value' => 'acf/' . $this->getName(),
],
],
],
]);
}
public function compile(array $block): string
{
$context = $this->filterContext($block['data']);
return Timber::compile($this->getTemplateFilename(), $context);
}
public function render(array $block)
{
echo $this->compile($block);
}
public function dynamicRender(string $content, array $block): string
{
return $content;
}
}
You don’t seem to understand what @saltnpixels is trying to achieve. The result of the render_callback specified when registering a block through ACF is stored to database on save of the post.
The desired behaviour is to have the content generated on request. Thus the example of the latest three posts, which might be different each time a user views the post.
The solution to the problem is using the render_block
filter: https://developer.wordpress.org/reference/hooks/render_block
The second argument to this hook, $block
, is an array. You can compare $block['blockName']
against your block name (beware of the acf/
prefix) and return dynamic content when it matches.
$block['attrs']['data']
is an array of all custom fields you defined for the block.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.