Home › Forums › General Issues › $block > [margin] returns string(21) “var:preset|spacing|80” › Reply To: $block > [margin] returns string(21) “var:preset|spacing|80”
Commenting here just in case there are others who may need this solution. The function that is most helpful here is wp_style_engine_get_styles
. When used in your block rendering script, you can call this function to produce the inline styles needed to render your block correctly.
I made a little utility function for doing so and generally include it in all of the ACF blocks we’re building. Hope it helps. =)
/**
* Given an $block object from an ACF block for gutenberg:
* This will return a string of CSS inline values suitable for
* inclusion in the block output in PHP.
*
* @param mixed $block
* @return $style as string
*/
function acf_blocks_calculate_inline_styles( $block ) {
if ( ! empty( $block['style'] ) ) {
$style_engine = wp_style_engine_get_styles( $block['style'] );
$style = $style_engine['css'];
} else {
$style = '';
}
return $style;
// echo '<div style="' . $style . '">Block content</div>';
}
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.