• Resolved jayenashar

    (@jayenashar)


    I’m trying to put javascript in post content in the text (not visual) editor. It mostly works fine, but I noticed a few issues:

    • blank lines and script tags are wrapped with p tags – not a big deal as i can just remove blank lines within the script
    • sometimes html within the script is wrapped with p tags – it doesn’t seem to be causing issues in my case but it’s a little weird
    • & is sometimes converted to an html entity of & – i can’t find a workaround and i need & to be & because it’s in a url

    is there any shortcode or something i can use to tell wordpress not to change my post content and to render it on the page verbatim?

    • This topic was modified 2 years, 3 months ago by jayenashar.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Yes – but it is complex – I wrote one for my own use once years ago

    However in 2022 we have the block editor. I assume that you have a classic editor plugin turning the block editor off.

    If you can, enable the block editor for the post in question. You than can use an HTML block – the HTML block doesn’t mangle scripts like the Classic editor does.

    Thread Starter jayenashar

    (@jayenashar)

    Thanks so much for that! I used the block editor and now it’s not inserting p tags, but that one line of the script is still replacing & with & . The script has a few fetch() calls, all with &, but for one fetch() line, all & are replaced with &

    A quick alternative if you are struggling is write a shortcode. A shortcode is very simple to write and you can add it to your functions.php or a code snippet plugin or write it as a mini plugin.

    The basics of a shortcode in a plugin is as simple as a php file with

    <?php
    /*
     * Plugin Name:   My Custom Code
     */
    function mcc_shortcode() {
    	$script = <<<EOT
        <script>
        // your script
        </script>
    EOT;
    	return $script;
    }
    add_shortcode('my_custom_shortcode','mcc_shortcode');

    Then you can easily add the shortcode on the page

    • This reply was modified 2 years, 3 months ago by Alan Fuller.
    Thread Starter jayenashar

    (@jayenashar)

    Thanks! I went with a code snippet plugin and that worked great.

    Glad you got it sorted

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘html in script in post content’ is closed to new replies.