Title: balanceTags
Published: April 25, 2014
Last modified: August 20, 2026

---

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

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#wp--skip-link--target)

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#parameters)󠁿

 `$text`stringrequired

Text to be balanced.

`$force`booloptional

If true, forces balancing, ignoring the value of the option.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#return)󠁿

 string Balanced text.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#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](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/formatting.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/formatting.php#L2564)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/formatting.php#L2564-L2570)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#related)󠁿

| Uses | Description | 
| [force_balance_tags()](https://developer.wordpress.org/reference/functions/force_balance_tags/)`wp-includes/formatting.php` |

Balances tags of string using a modified stack.

  | 
| [get_option()](https://developer.wordpress.org/reference/functions/get_option/)`wp-includes/option.php` |

Retrieves an option value based on an option name.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#changelog)󠁿

| Version | Description | 
| [0.71](https://developer.wordpress.org/reference/since/0.71/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#comment-content-1403)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/balancetags/#comment-1403)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F%23comment-1403)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F%23comment-1403)
 4.  **Unclosed LI tags**
 5.      ```php
         <?php
         $html = '<ul>
           <li>this
           <li>is
           <li>a
           <li>list
         </ul>';
         echo balanceTags($html, true);
         ?>
         ```
     
 6.  Will output this HTML:
 7.      ```php
         <ul>
           <li>this
           </li><li>is
           </li><li>a
           </li><li>list
         </li></ul>
         ```
     
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F%3Freplytocom%3D1403%23feedback-editor-1403)
 9.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/balanceTags/?output_format=md#comment-content-5977)
 10.   [kitchin](https://profiles.wordpress.org/kitchin/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/balancetags/#comment-5977)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F%23comment-5977)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F%23comment-5977)
 12. 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()](https://developer.wordpress.org/reference/functions/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.
 13. 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](https://core.trac.wordpress.org/ticket/5161)
 14. Most coders will want to use `[force_balance_tags()](https://developer.wordpress.org/reference/functions/force_balance_tags/)`
     instead of `[balanceTags()](https://developer.wordpress.org/reference/functions/balancetags/)`.
 15.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F%3Freplytocom%3D5977%23feedback-editor-5977)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fbalancetags%2F)
before being able to contribute a note or feedback.