.$class_name a{" . $link_color_declaration . " !important;}\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; } add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );