WP_HTML_Tag_Processor::get_modifiable_text(): string

Returns the modifiable text for a matched token, or an empty string.

Description

Modifiable text is text content that may be read and changed without changing the HTML structure of the document around it. This includes the contents of #text nodes in the HTML as well as the inner contents of HTML comments, Processing Instructions, and others, even though these nodes aren’t part of a parsed DOM tree. They also contain the contents of SCRIPT and STYLE tags, of TEXTAREA tags, and of any other section in an HTML document which cannot contain HTML markup (DATA).

If a token has no modifiable text then an empty string is returned to avoid needless crashing or type errors. An empty string does not mean that a token has modifiable text, and a token with modifiable text may have an empty string (e.g. a comment with no contents).

Return

string

Source

if ( null === $tag_name ) {
	return null;
}

if ( 'html' === $this->get_namespace() ) {
	return $tag_name;
}

$lower_tag_name = strtolower( $tag_name );
if ( 'math' === $this->get_namespace() ) {
	return $lower_tag_name;
}

if ( 'svg' === $this->get_namespace() ) {
	switch ( $lower_tag_name ) {
		case 'altglyph':
			return 'altGlyph';

		case 'altglyphdef':
			return 'altGlyphDef';

		case 'altglyphitem':
			return 'altGlyphItem';

		case 'animatecolor':
			return 'animateColor';

		case 'animatemotion':
			return 'animateMotion';

		case 'animatetransform':
			return 'animateTransform';

		case 'clippath':
			return 'clipPath';

		case 'feblend':
			return 'feBlend';

		case 'fecolormatrix':
			return 'feColorMatrix';

		case 'fecomponenttransfer':
			return 'feComponentTransfer';

		case 'fecomposite':
			return 'feComposite';

		case 'feconvolvematrix':
			return 'feConvolveMatrix';

		case 'fediffuselighting':
			return 'feDiffuseLighting';

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Since a #text node is not part of the tag itself, get_modifiable_text() can’t be used directly.
    After selecting the desired tag you must first pass to the next_token().

    function wpdocs_get_text_from_block( $block_content, $block ) {
    
    	// $block_content = "<div>Lorem Ipsum</div>"
    	$processor = new WP_HTML_Tag_Processor( $block_content );
    
    	if ( $processor->next_tag( 'div' ) ) {
    		$processor->next_token();
    		$node_text = $processor->get_modifiable_text();
    		error_log( $node_text ); // output: "Lorem Ipsum"
      	}
    
    	return $processor->get_updated_html();
    }
    add_filter( 'render_block_custom/div', 'wpdocs_get_text_from_block', 10, 2 )

You must log in before being able to contribute a note or feedback.