Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to overwrite prefix or namespace for ACF blocks #284

Open
imre08 opened this issue Sep 26, 2024 · 2 comments
Open

Unable to overwrite prefix or namespace for ACF blocks #284

imre08 opened this issue Sep 26, 2024 · 2 comments

Comments

@imre08
Copy link

imre08 commented Sep 26, 2024

When I generated a new block called Hero and I tried to overwrite the prefix from acf/ to custom/ for example. Problem was that it still rendered with acf/ prefix in the Gutenberg editor. So all my fields were missing from the block. But, when I cached the block.json using wp acorn acf:cache, then it had the correct prefix. But problem with caching was that I had to add re-insert the block for it to work again. Since it showed that acf/hero block was missing. Same problem goes overwriting the namespace.

public $prefix = 'custom/';
@Log1x
Copy link
Owner

Log1x commented Sep 27, 2024

It's possible using acf/ for the prefix is required in the latest versions of ACF Pro. I can look into it more/confirm sometime next week.

FWIW, you would always have to repair/re-insert your existing blocks when changing the prefix.

@thunderdw
Copy link
Contributor

thunderdw commented Nov 20, 2024

I was looking into this. ACF Pro hardcodes the namespace to acf/ when blocks are registered via PHP instead of block.json:

From the acf_validate_block_type() function in advanced-custom-fields-pro/pro/blocks.php:

if ( $block['name'] ) {
    $block['name'] = 'acf/' . acf_slugify( $block['name'] );
}

After the block array is passed through that function, it's passed through the acf/register_block_type_args filter. So to overwrite the prefix for both cached and uncached blocks you would need to both set the $prefix property as done in the original post and hook into the acf/register_block_type_args with something like this:

add_filter('acf/register_block_type_args', function (array $block): array {
    $block['name'] = str_replace('acf/', 'custom/', $block['name']);
    return $block;
});

Edit: This is true as of the latest ACF Pro version, 6.3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants