5

I have registered a custom block with ACFs acf_register_block. I have set a render_callback to load a php template but would like to access any extra CSS classes which the user may have set in the admin.

enter image description here

Does WordPress provide a global like $block (similar to $post) or would I need to raise a question on the ACF support forums?

6

Note that I don't have ACF PRO 5.8 which comes with the Block features, but I hope this answer helps.

If you look at the example here, you can retrieve the additional/custom CSS classes using props.attributes.className; i.e. the classes are saved as an attribute named className.

So with that in mind and based on acf_register_block() (which as of writing, it redirects to acf_register_block_type()), your render_callback function would receive a $block array (the block settings and attributes) as the first argument, and therefore you should be able to retrieve the additional CSS class(es) via $block['className'] as shown here — see the example under "Registering a block with callback".

| improve this answer | |
  • Perfect, thank you. I don't know why I didn't just try a var_dump($block) as I said in my own question! – Alexander Holsgrove May 9 '19 at 8:18
  • I don't know, either.. :) But anyway, I'm glad I could help. – Sally CJ May 9 '19 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.