[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 May 31 17:18:11 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 semenov10):
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:
{{{#!php
<?php
```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. ';
```
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:
```php
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:
```php
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.
Conclusion
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.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/40958#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list