Adding JavaScript (to a single WordPress post/page)

Here’s one I did earlier – an interactive cube in WebGL (just don’t look at the code 😱).

Full disclosure: this post is for my own reference as much as anything. 😉

A quick search of the web to find how to add JavaScript to WordPress returns many articles that make quite a meal of it (many just being copies of each other 🙄). But for a single post or page it’s actually not all that difficult (when you know how)*, and doesn’t need plug-ins or messing about with PHP files.
* I do try to avoid the phrase, “it’s easy”, it so rarely is. Or else I heavily qualify it.

Allow to me to elucidate…

1. Start a new post/page.

2a. Select a text block, type “/” then select “Custom HTML”.

OR

2b. Select a text block, type a character then click “⋮” then select “Edit as HTML”.

OR

For some reason, when using the following method, WordPress randomly adds <br /> and <p></p> elements. 2a and 2b seem more stable. This following method is included for completeness.

3. Open the Options Panel, by clicking “⋮” (Options).

4. Open the Code Editor, by clicking “Code Editor”. 

 

5. Finally, add your JavaScript code (here I’ve also added a div with an ID so I can write to it).

 

6. Preview to check it’s all working! 🤞

Bingo, bango! All done! ❤

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.