Add span to heading block

<?php

/**
 * Heading block, line style
 *
 */
function be_heading_line_style( $content, $block ) {

	if( 'core/heading' === $block['blockName' ] && !empty( $block['attrs'] ) && !empty( $block['attrs']['className'] ) ) {
		$classes = explode( ' ', $block['attrs']['className'] );
		if( in_array( 'is-style-line', $classes ) ) {
			$content = be_inner_span( $content );
		}
	}

	return $content;
}
add_filter( 'render_block', 'be_heading_line_style', 10, 2 );

/**
 * Inner Span
 *
 */
function be_inner_span( $content = '' ) {
	$start = '">';
	$start_pos = strpos( $content, $start );
	$end = '</';
	$end_pos = strrpos( $content, $end );

	if( false !== $start_pos ) {
		$content = substr_replace( $content, '</span>' . $end, $end_pos, strlen( $end ) );
		$content = substr_replace( $content, $start . '<span>', $start_pos, strlen( $start ) );
	}

	return $content;
}
Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk