How do I stop WordPress from adding paragraph tags?

Hi guys,

I have some code in a PHP plugin file below:

document.getElementById('game-login-form').addEventListener('submit', function(event) {
            event.preventDefault();
            let username = document.getElementById('game-username').value;
            
            fetch(`resource`)
            .then(response => response.blob())
            .then(blob => {
                let avatarURL = URL.createObjectURL(blob);
                document.getElementById('avatar-container').innerHTML = `<img src="${avatarURL}" alt="Avatar"><p>Hello, ${username}</p>`;

But once it runs on my website, WordPress adds paragraph tags everywhere, messing up the code. I’ve tried installing different plugins like rawHTML and using this code snippet to fix it:

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

But sadly to no avail. Maybe I’m putting it in the wrong place, but I’ve only put it in my theme’s functions.php file as recommended. Any help is greatly appreciated!

Coding problems should better be posted somewhere else as not everyone here is capable of debugging the code for you.

That is to say, if you need JavaScript fetching stuffs from WordPress, you should better rely on REST API instead of parsing the returned HTML directly (and end up blamimg WordPress for adding <p> tags for you.)

6 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.