Search code examples
javascriptphpwordpressbuddypress

Wordpress 5.4 inserting <p> tags into Javascript code on Buddypress activity feed


After upgrading to Wordpress 5.4, we've found that it's inserting rogue <p> tags into Javascript code that was previously working on our Buddypress activity feed.

The code is included with a call to bp_get_template_part:

bp_get_template_part( 'activity/submit-tabs' );

Here's an example of the code from the Web inspector, showing these roque <p> tags:

<p><script>
    jQuery(function($) {
        var input = $('#our-activity-action-input');
        var privacy = $('#our-activity-action-privacy');
        var type = $('#our-activity-action-type');
        var button = $('#our-activity-action-button');</p>
<p>        type.change(function() {
            var typeValue = type.val();
            var placeholder = '';</p>
<p>            if (typeValue == 'news') {
                placeholder = 'Enter the URL of a news article you’d like to rate or fact-check.';
            }</p>
<p>            input.attr('placeholder', placeholder);
        });</p>
<p>        button.click(function() {
            var inputValue = input.val().trim();</p>
<p>            if (!inputValue) {
                input.val('').focus();
                return false;
            }</p>
<p>            var submitAction = function() {
                var typeValue = type.val();</p>
<p>                var action;
                var actionInput;
                var actionSubmit;</p>
<p>                if (typeValue == 'news') {
                    action = $('.our-activity-actions .action-news');
                    actionInput = action.find('#af_ournews_url_input');
                    actionSubmit = action.find('#af_ournews_url_submit');
                } else if (typeValue == 'claim') {
                    action = $('.our-activity-actions .action-claim');
                    actionInput = action.find('#af_ournews_url_input_quote');
                    actionSubmit = action.find('#af_ournews_url_submit_quote');
                } else if (typeValue == 'status') {
                    action = $('.our-activity-actions .action-status');
                    actionInput = action.find('#whats-new');
                    actionSubmit = action.find('#aw-whats-new-submit');
                }</p>
<p>                actionInput.val(inputValue);</p>
<p>                actionSubmit.trigger('click');</p>
<p>                if (typeValue == 'status') {
                    input.val('');</p>
<p>                                    }
            };</p>
<p>            var privacyValue = privacy.val();</p>
<p>            var ajaxUrl = 'https://our.news/wp-admin/admin-ajax.php';</p>
<p>            var data = {
                action: 'ours_set_temp_privacy',
                privacy: privacyValue
            };</p>
<p>            button.attr('disabled', true);</p>
<p>            $.post( ajaxUrl, data, function(response) {
                submitAction();</p>
<p>                button.attr('disabled', false);
            });</p>
<p>        });
    });

We tried the usual solutions like adding this to our child theme's functions.php:

remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

We went even further and tried guessing at a few Buddypress filters, but no success:

remove_filter( 'bp_get_template_part', 'wpautop');
remove_filter( 'bp_before_has_activities_parse_args', 'wpautop');
remove_filter( 'bp_after_has_activities_parse_args', 'wpautop');

Any advice or suggestions how to stop this from happening?


Solution

  • Alright we found a hacky workaround that works but there must be a better way:

    add_filter ('the_content','our_remove_autop',0);
    function our_remove_autop($content)
    {
        $request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH );
        $is_activity = strpos($request_uri, 'activity/',0);
        if ($is_activity)
        {
                remove_filter('the_content','wpautop');
        }
        return $content;
    }