Make WordPress Core

Opened 7 years ago

Last modified 3 weeks ago

#40958 new defect (bug)

force_balance_tags breaks Ninjaforms and probably other plugins that output html within js.

Reported by: programmin's profile programmin Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.4
Component: Shortcodes Keywords:
Focuses: ui, administration Cc:


If you have a shortcode for Ninjaforms and use the post in a context that runs force_balance_tags() it seriously breaks the script. For example if you have a form id=14, add this :


echo 'NO BALANCE:--'. apply_filters('the_content', '[ninja_form id=14]' ) .' --END NO BALANCE. ';
echo 'BALANCED:--'. force_balance_tags(apply_filters('the_content', '[ninja_form id=14]' )) .' --END BALANCED. ';

Notice that some ending /spans, /is, and or /as are added before the end script, breaking it. Seems to not be wp version dependent.

Change History (3)

#1 @programmin
7 years ago

It would be nice if there were a filter always applied at the end of the force balance function, for the possibility of cleaning up bugs like this.

#2 @boceksumuklu
2 months ago

Hello Programmin,

Please don't forget to replace 14 with your form id.

remove_filter( 'the_content', 'force_balance_tags' );
$content = apply_filters( 'the_content', $content_with_shortcodes );
add_filter( 'the_content', 'force_balance_tags' );

function my_ninja_form_shortcode_handler( $atts ) {
    return ninja_forms_display_form( array( 'id' => 14 ) ); // Replace 14 with your form ID
add_shortcode( 'my_ninja_form', 'my_ninja_form_shortcode_handler' );

Last edited 6 weeks ago by boceksumuklu (previous) (diff)

#3 @semenov10
3 weeks ago

I have been following the discussion regarding the issue with force_balance_tags affecting NinjaForms and potentially other plugins that output HTML within JavaScript.

Issue Description

As described, the force_balance_tags function is causing additional closing tags to be inserted into the content processed by the the_content filter. This is particularly problematic for shortcodes like [ninja_form id=14], where the inserted tags break the intended script.

Example Demonstration

To illustrate the issue:

echo 'NO BALANCE:--' . apply_filters('the_content', '[ninja_form id=14]') . ' --END NO BALANCE. ';
echo 'BALANCED:--' . force_balance_tags(apply_filters('the_content', '[ninja_form id=14]')) . ' --END BALANCED. ';

In the above example, the output shows that the `force_balance_tags` function adds unwanted closing tags, which disrupts the form's functionality.

 Suggested Workaround

To temporarily address this issue, we can use a custom shortcode handler that bypasses `force_balance_tags`. Here’s a potential workaround:

remove_filter('the_content', 'force_balance_tags');

function my_ninja_form_shortcode_handler($atts) {
    $form_id = isset($atts['id']) ? intval($atts['id']) : 0;
    return ninja_forms_display_form(array('id' => $form_id));
add_shortcode('my_ninja_form', 'my_ninja_form_shortcode_handler');

$content_with_shortcodes = apply_filters('the_content', '[my_ninja_form id=14]');
add_filter('the_content', 'force_balance_tags');

echo $content_with_shortcodes;

 Proposed Long-Term Solution

To prevent such issues in the future, it would be beneficial if there were a filter applied at the end of the `force_balance_tags` function. This filter would allow developers to clean up any bugs resulting from additional tags.

For example, implementing a filter like this:

function custom_force_balance_tags($content) {
    $content = force_balance_tags($content);
    return apply_filters('after_force_balance_tags', $content);
add_filter('the_content', 'custom_force_balance_tags', 20);

This approach provides a hook (after_force_balance_tags) that developers can use to further process the content if needed, addressing specific issues like the one with NinjaForms.


Implementing a filter at the end of the force_balance_tags function can provide a flexible solution for developers to handle edge cases without disrupting the default behavior. I hope this suggestion can be considered to enhance the robustness of content handling in WordPress.

Thank you for your attention to this matter, and I look forward to further discussions on this topic.

Last edited 3 weeks ago by semenov10 (previous) (diff)
Note: See TracTickets for help on using tickets.