Make WordPress Core

Opened 7 years ago

Last modified 2 days 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:

Description

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 :

<?php

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 @vizvizka
3 weeks ago

remove_filter( 'the_content', 'force_balance_tags' ); Disable force_balance_tags
$content = apply_filters( 'the_content', $content_with_shortcodes );
Process content with shortcodes
add_filter( 'the_content', 'force_balance_tags' ); Re-enable 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 3 weeks ago by vizvizka (previous) (diff)

#3 @wpteccho
2 days ago


This worked for me

$form_content = apply_filters('the_content', '[ninja_form id=14]');
if (strpos($form_content, '[ninja_form') === false) {
    // Only use force_balance_tags if the content doesn't contain the Ninja Form shortcode
    $balanced_content = force_balance_tags($form_content);
} else {
    $balanced_content = $form_content; // Skip force_balance_tags for forms
}

echo 'BALANCED:--' . $balanced_content . ' --END BALANCED. ';

Last edited 2 days ago by wpteccho (previous) (diff)
Note: See TracTickets for help on using tickets.