[wp-trac] [WordPress Trac] #40958: force_balance_tags breaks Ninjaforms and probably other plugins that output html within js.
WordPress Trac
noreply at wordpress.org
Fri Jun 21 12:44:24 UTC 2024
#40958: force_balance_tags breaks Ninjaforms and probably other plugins that output
html within js.
--------------------------+---------------------------------
Reporter: programmin | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Shortcodes | Version: 4.7.4
Severity: normal | Resolution:
Keywords: | Focuses: ui, administration
--------------------------+---------------------------------
Comment (by sparkodev):
The force_balance_tags function in WordPress adds unwanted closing tags to
content processed by the the_content filter, disrupting shortcodes like
[ninja_form id=14].
Workaround
Bypass force_balance_tags using a custom shortcode handler:
{{{
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;
}}}
Long-Term Solution
Implement a filter at the end of force_balance_tags:
{{{
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 provides a hook (after_force_balance_tags) for developers to clean up
any additional tags.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/40958#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list