0

I am developing a custom block for my WordPress plugin. The block is meant to be very similar to the WordPress core search block. On the default search block, there are several supports declared, including for border, background color, text color, text size, etc. This block is able to apply styles and classes from the block supports to different elements in the block. For example, the background color control only changes the background color of the button, and the font size support only applies to the label above the search input. I am looking for a solution for how to accomplish this in my block. My block is a server rendered block using the render.php file, and I am having a hard time finding a convenient and scalable way to use the string returned by get_block_wrapper_attributes() to apply styles only to certain parts of my block. So, what is the best way to extract specific styles from the block wrapper attributes to apply to specific elements in my custom block's render.php?

3
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented Aug 14 at 11:20
  • You should perhaps split it into several blocks : you can define a template when inserting the parent block to include default children. Commented Aug 22 at 17:09
  • Interesting, I may update my block to use this method in the future. Thank you. For now, I just manually parsed the block supports based on the way the core search block does it.
    – ho88it
    Commented Aug 27 at 14:12

1 Answer 1

0

I solved this issue when I realized that all style attributes are held in the $attributes array passed to the render.php file. It is a lot of raw work, but specific styles from block supports can be extracted from this object. For certain supports, I have to check multiple possible locations in the block supports, and sometimes you will need to convert them from variable names to usable classnames or css vars using regexes, but it is done by parsing through the $attributes array manually :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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