balanceTags( string $text, bool $force = false ): string

Balances tags if forced to, or if the ‘use_balanceTags’ option is set to true.

Parameters

$textstringrequired
Text to be balanced
$forcebooloptional
If true, forces balancing, ignoring the value of the option.

Default:false

Return

string Balanced text

More Information

The option ‘use_balanceTags’ is used for whether the tags will be balanced. Either the $force parameter or ‘use_balanceTags’ option need to be true before the tags will be balanced.

Source

function balanceTags( $text, $force = false ) {  // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	if ( $force || (int) get_option( 'use_balanceTags' ) === 1 ) {
		return force_balance_tags( $text );
	} else {
		return $text;
	}
}

Changelog

VersionDescription
0.71Introduced.

User Contributed Notes

  1. Skip to note 4 content

    The `use_balanceTags` option has default 0. The admin UI for it was removed several years ago, along with the UI for `use_smilies` (default 1), in Dashboard/Settings/Writing. So the core filters calling `balanceTags() `, such as ‘content_save_pre’, do nothing unless the user set an option in the distant past, or a plugin/theme changed the setting.

    The bug ticket back then said the TinyMCE editor handles the balancing in ‘Visual’ mode, and users in ‘Text’ mode should be allowed to do their own balancing, by default. Opinion changed to that as the ticket developed: https://core.trac.wordpress.org/ticket/5161

    Most coders will want to use `force_balance_tags() ` instead of `balanceTags() `.

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