Forked from joseph-farruggio/acf-block-attributes-wrapper.php
Created
September 27, 2023 17:57
-
-
Save alinademi/f15e171ace714797cadbfea32011e8de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A function that takes an array of HTML attribute | |
* key/value pairs and returns a string of HTML attributes. | |
**/ | |
function unwrap_html_attrs($attrs) | |
{ | |
$attributes = ''; | |
foreach ($attrs as $key => $value) { | |
$attributes .= sprintf(' %s="%s"', $key, esc_attr($value)); | |
} | |
return $attributes; | |
} | |
/** | |
* A wrapper for the get_block_wrapper_attributes() function | |
* that allows us to pass in an array of HTML attribute key/value pairs. | |
* get_block_wrapper_attributes() is only returned on the front-end while | |
* the attributes that we set ourselves are returned in the block editor. | |
**/ | |
function acf_block_attributes_wrapper($block, $is_preview = false, $attrs = []) | |
{ | |
$attrs['id'] = isset($attrs['id']) ?: $block['id']; | |
$attrs['class'] = isset($attrs['class']) ? $attrs['class'] . ' ' . $block['className'] : $block['className']; | |
// Get the text alignment class name (alignwide or alignfull) | |
if (!empty($block['alignText'])) { | |
$attrs['class'] .= ' has-text-align-' . $block['alignText']; | |
} | |
// To do: Check for other block styles that are missed in get_block_wrapper_attributes() | |
if ($is_preview) { | |
return unwrap_html_attrs($attrs); | |
} else { | |
return get_block_wrapper_attributes($attrs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment