wp_render_elements_support

The timeline below displays how wordpress function wp_render_elements_support has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Updates the block content with elements class names.
 *
 * @since 5.8.0
 * @since 6.4.0 Added support for button and heading element styling.
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content || !isset($block['attrs']['style']['elements'])) {
        return $block_content;
    }
    $block_type = WP_Block_Type_Registry::get_instance()->get_registered($block['blockName']);
    if (!$block_type) {
        return $block_content;
    }
    $element_color_properties = array('button' => array('skip' => wp_should_skip_block_supports_serialization($block_type, 'color', 'button'), 'paths' => array(array('button', 'color', 'text'), array('button', 'color', 'background'), array('button', 'color', 'gradient'))), 'link' => array('skip' => wp_should_skip_block_supports_serialization($block_type, 'color', 'link'), 'paths' => array(array('link', 'color', 'text'), array('link', ':hover', 'color', 'text'))), 'heading' => array('skip' => wp_should_skip_block_supports_serialization($block_type, 'color', 'heading'), 'paths' => array(array('heading', 'color', 'text'), array('heading', 'color', 'background'), array('heading', 'color', 'gradient'), array('h1', 'color', 'text'), array('h1', 'color', 'background'), array('h1', 'color', 'gradient'), array('h2', 'color', 'text'), array('h2', 'color', 'background'), array('h2', 'color', 'gradient'), array('h3', 'color', 'text'), array('h3', 'color', 'background'), array('h3', 'color', 'gradient'), array('h4', 'color', 'text'), array('h4', 'color', 'background'), array('h4', 'color', 'gradient'), array('h5', 'color', 'text'), array('h5', 'color', 'background'), array('h5', 'color', 'gradient'), array('h6', 'color', 'text'), array('h6', 'color', 'background'), array('h6', 'color', 'gradient'))));
    $skip_all_element_color_serialization = $element_color_properties['button']['skip'] && $element_color_properties['link']['skip'] && $element_color_properties['heading']['skip'];
    if ($skip_all_element_color_serialization) {
        return $block_content;
    }
    $elements_style_attributes = $block['attrs']['style']['elements'];
    foreach ($element_color_properties as $element_config) {
        if ($element_config['skip']) {
            continue;
        }
        foreach ($element_config['paths'] as $path) {
            if (null !== _wp_array_get($elements_style_attributes, $path, null)) {
                /*
                 * It only takes a single custom attribute to require that the custom
                 * class name be added to the block, so once one is found there's no
                 * need to continue looking for others.
                 *
                 * As is done with the layout hook, this code assumes that the block
                 * contains a single wrapper and that it's the first element in the
                 * rendered output. That first element, if it exists, gets the class.
                 */
                $tags = new WP_HTML_Tag_Processor($block_content);
                if ($tags->next_tag()) {
                    $tags->add_class(wp_get_elements_class_name($block));
                }
                return $tags->get_updated_html();
            }
        }
    }
    // If no custom attributes were found then there's nothing to modify.
    return $block_content;
}

WordPress Version: 6.3

/**
 * Updates the block content with elements class names.
 *
 * @since 5.8.0
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content) {
        return $block_content;
    }
    $block_type = WP_Block_Type_Registry::get_instance()->get_registered($block['blockName']);
    $skip_link_color_serialization = wp_should_skip_block_supports_serialization($block_type, 'color', 'link');
    if ($skip_link_color_serialization) {
        return $block_content;
    }
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    $hover_link_color = null;
    if (!empty($block['attrs'])) {
        $hover_link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', ':hover', 'color', 'text'), null);
    }
    /*
     * For now we only care about link colors.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color && null === $hover_link_color) {
        return $block_content;
    }
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Add the class name to the first element, presuming it's the wrapper, if it exists.
    $tags = new WP_HTML_Tag_Processor($block_content);
    if ($tags->next_tag()) {
        $tags->add_class(wp_get_elements_class_name($block));
    }
    return $tags->get_updated_html();
}

WordPress Version: 6.2

/**
 * Updates the block content with elements class names.
 *
 * @since 5.8.0
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content) {
        return $block_content;
    }
    $block_type = WP_Block_Type_Registry::get_instance()->get_registered($block['blockName']);
    $skip_link_color_serialization = wp_should_skip_block_supports_serialization($block_type, 'color', 'link');
    if ($skip_link_color_serialization) {
        return $block_content;
    }
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    /*
     * For now we only care about link color.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color) {
        return $block_content;
    }
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Add the class name to the first element, presuming it's the wrapper, if it exists.
    $tags = new WP_HTML_Tag_Processor($block_content);
    if ($tags->next_tag()) {
        $tags->add_class(wp_get_elements_class_name($block));
    }
    return $tags->get_updated_html();
}

WordPress Version: 6.1

/**
 * Update the block content with elements class names.
 *
 * @since 5.8.0
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content) {
        return $block_content;
    }
    $block_type = WP_Block_Type_Registry::get_instance()->get_registered($block['blockName']);
    $skip_link_color_serialization = wp_should_skip_block_supports_serialization($block_type, 'color', 'link');
    if ($skip_link_color_serialization) {
        return $block_content;
    }
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    /*
     * For now we only care about link color.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color) {
        return $block_content;
    }
    $class_name = wp_get_elements_class_name($block);
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Retrieve the opening tag of the first HTML element.
    $html_element_matches = array();
    preg_match('/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE);
    $first_element = $html_element_matches[0][0];
    // If the first HTML element has a class attribute just add the new class
    // as we do on layout and duotone.
    if (strpos($first_element, 'class="') !== false) {
        $content = preg_replace('/' . preg_quote('class="', '/') . '/', 'class="' . $class_name . ' ', $block_content, 1);
    } else {
        // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
        $first_element_offset = $html_element_matches[0][1];
        $content = substr_replace($block_content, ' class="' . $class_name . '"', $first_element_offset + strlen($first_element) - 1, 0);
    }
    return $content;
}

WordPress Version: 9.3

/**
 * Elements styles block support.
 *
 * @package WordPress
 * @since 5.8.0
 */
/**
 * Render the elements stylesheet.
 *
 * @since 5.8.0
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content) {
        return $block_content;
    }
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    /*
     * For now we only care about link color.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color) {
        return $block_content;
    }
    $class_name = wp_unique_id('wp-elements-');
    if (strpos($link_color, 'var:preset|color|') !== false) {
        // Get the name from the string and add proper styles.
        $index_to_splice = strrpos($link_color, '|') + 1;
        $link_color_name = substr($link_color, $index_to_splice);
        $link_color = "var(--wp--preset--color--{$link_color_name})";
    }
    $link_color_declaration = esc_html(safecss_filter_attr("color: {$link_color}"));
    $style = ".{$class_name} a{" . $link_color_declaration . ';}';
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Retrieve the opening tag of the first HTML element.
    $html_element_matches = array();
    preg_match('/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE);
    $first_element = $html_element_matches[0][0];
    // If the first HTML element has a class attribute just add the new class
    // as we do on layout and duotone.
    if (strpos($first_element, 'class="') !== false) {
        $content = preg_replace('/' . preg_quote('class="', '/') . '/', 'class="' . $class_name . ' ', $block_content, 1);
    } else {
        // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
        $first_element_offset = $html_element_matches[0][1];
        $content = substr_replace($block_content, ' class="' . $class_name . '"', $first_element_offset + strlen($first_element) - 1, 0);
    }
    wp_enqueue_block_support_styles($style);
    return $content;
}

WordPress Version: 9.1

/**
 * Elements styles block support.
 *
 * @package WordPress
 * @since 5.8.0
 */
/**
 * Render the elements stylesheet.
 *
 * @since 5.8.0
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content) {
        return $block_content;
    }
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    /*
     * For now we only care about link color.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color) {
        return $block_content;
    }
    $class_name = 'wp-elements-' . uniqid();
    if (strpos($link_color, 'var:preset|color|') !== false) {
        // Get the name from the string and add proper styles.
        $index_to_splice = strrpos($link_color, '|') + 1;
        $link_color_name = substr($link_color, $index_to_splice);
        $link_color = "var(--wp--preset--color--{$link_color_name})";
    }
    $link_color_declaration = esc_html(safecss_filter_attr("color: {$link_color}"));
    $style = ".{$class_name} a{" . $link_color_declaration . ';}';
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Retrieve the opening tag of the first HTML element.
    $html_element_matches = array();
    preg_match('/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE);
    $first_element = $html_element_matches[0][0];
    // If the first HTML element has a class attribute just add the new class
    // as we do on layout and duotone.
    if (strpos($first_element, 'class="') !== false) {
        $content = preg_replace('/' . preg_quote('class="', '/') . '/', 'class="' . $class_name . ' ', $block_content, 1);
    } else {
        // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
        $first_element_offset = $html_element_matches[0][1];
        $content = substr_replace($block_content, ' class="' . $class_name . '"', $first_element_offset + strlen($first_element) - 1, 0);
    }
    wp_enqueue_block_support_styles($style);
    return $content;
}

WordPress Version: 5.9

/**
 * Elements styles block support.
 *
 * @package WordPress
 * @since 5.8.0
 */
/**
 * Render the elements stylesheet.
 *
 * @since 5.8.0
 * @access private
 *
 * @param string $block_content Rendered block content.
 * @param array  $block         Block object.
 * @return string Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    if (!$block_content) {
        return $block_content;
    }
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    /*
     * For now we only care about link color.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color) {
        return $block_content;
    }
    $class_name = 'wp-elements-' . uniqid();
    if (strpos($link_color, 'var:preset|color|') !== false) {
        // Get the name from the string and add proper styles.
        $index_to_splice = strrpos($link_color, '|') + 1;
        $link_color_name = substr($link_color, $index_to_splice);
        $link_color = "var(--wp--preset--color--{$link_color_name})";
    }
    $link_color_declaration = esc_html(safecss_filter_attr("color: {$link_color}"));
    $style = "<style>.{$class_name} a{" . $link_color_declaration . ";}</style>\n";
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Retrieve the opening tag of the first HTML element.
    $html_element_matches = array();
    preg_match('/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE);
    $first_element = $html_element_matches[0][0];
    // If the first HTML element has a class attribute just add the new class
    // as we do on layout and duotone.
    if (strpos($first_element, 'class="') !== false) {
        $content = preg_replace('/' . preg_quote('class="', '/') . '/', 'class="' . $class_name . ' ', $block_content, 1);
    } else {
        // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
        $first_element_offset = $html_element_matches[0][1];
        $content = substr_replace($block_content, ' class="' . $class_name . '"', $first_element_offset + strlen($first_element) - 1, 0);
    }
    /*
     * Ideally styles should be loaded in the head, but blocks may be parsed
     * after that, so loading in the footer for now.
     * See https://core.trac.wordpress.org/ticket/53494.
     */
    add_action('wp_footer', static function () use ($style) {
        echo $style;
    });
    return $content;
}

WordPress Version: 5.8

/**
 * Elements styles block support.
 *
 * @package WordPress
 * @since 5.8.0
 */
/**
 * Render the elements stylesheet.
 *
 * @since 5.8.0
 * @access private
 *
 * @param  string $block_content Rendered block content.
 * @param  array  $block         Block object.
 * @return string                Filtered block content.
 */
function wp_render_elements_support($block_content, $block)
{
    $link_color = null;
    if (!empty($block['attrs'])) {
        $link_color = _wp_array_get($block['attrs'], array('style', 'elements', 'link', 'color', 'text'), null);
    }
    /*
     * For now we only care about link color.
     * This code in the future when we have a public API
     * should take advantage of WP_Theme_JSON::compute_style_properties
     * and work for any element and style.
     */
    if (null === $link_color) {
        return $block_content;
    }
    $class_name = 'wp-elements-' . uniqid();
    if (strpos($link_color, 'var:preset|color|') !== false) {
        // Get the name from the string and add proper styles.
        $index_to_splice = strrpos($link_color, '|') + 1;
        $link_color_name = substr($link_color, $index_to_splice);
        $link_color = "var(--wp--preset--color--{$link_color_name})";
    }
    $link_color_declaration = esc_html(safecss_filter_attr("color: {$link_color}"));
    $style = "<style>.{$class_name} a{" . $link_color_declaration . " !important;}</style>\n";
    // Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
    // Retrieve the opening tag of the first HTML element.
    $html_element_matches = array();
    preg_match('/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE);
    $first_element = $html_element_matches[0][0];
    // If the first HTML element has a class attribute just add the new class
    // as we do on layout and duotone.
    if (strpos($first_element, 'class="') !== false) {
        $content = preg_replace('/' . preg_quote('class="', '/') . '/', 'class="' . $class_name . ' ', $block_content, 1);
    } else {
        // If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
        $first_element_offset = $html_element_matches[0][1];
        $content = substr_replace($block_content, ' class="' . $class_name . '"', $first_element_offset + strlen($first_element) - 1, 0);
    }
    return $content . $style;
}