skip to Main Content

I am not able to add the following javascript into my wordpress post.

The javascript is provided by Google to display a map of the web search results.

<script type="text/javascript" src="https://ssl.gstatic.com/trends_nrtr/2578_RC01/embed_loader.js"></script> <script type="text/javascript"> trends.embed.renderExploreWidget("GEO_MAP", {"comparisonItem":[{"keyword":"covid","geo":"","time":"today 12-m"}],"category":0,"property":""}, {"exploreQuery":"q=covid&date=today 12-m","guestPath":"https://trends.google.com:443/trends/embed/"}); </script> 

I have tried to add the javascript using the text editor (not visual) and also tried to add the javascript above within the "< code > </ code >", but it is still unsuccessful. Referring to this feedback adding javascript in wordpress posts (which might be stale)

What should I do if I wanted to add the above javascript into the wordpress post body?


Update:
@bhanu below is how I added the Javascript which results in an empty body post.

enter image description here

2

Answers


  1. Well it is very simple to do that. I don’t fully understand if you are on Gutenberg or classic Editor so I am going to show it for both of them.

    Execute JS inside post with Gutenberg

    • Use Custom HTML block to and paste your code inside the block. If you are adding JS you must wrap it around <script></script> tag.

    JS in gutenberg

    • This would give the following result.

    output : JS in gutenberg


    Execute JS inside post with Classic Editor

    • If you have some content like this.

    js in Classic

    • Switch to text mode and paste the code where you want to add it.

    js in Classic - code added

    • This would be the output.

    js in Classic - output

    Login or Signup to reply.
  2. There’s 2 ways to add Javascript to your WordPress site. Obviously in this case you’re wanting to add it to an individual post/page, rather than site-wide. So a good option is to use the CodeEmbed plugin, which has 30,000 active users, is free & open-source on SVN, and receives frequent updates.

    After activating this plugin, go back to the edit post/page webpage in your admin panel, and enable "Custom Fields" in "Screen Options". Scrolling down below the content body field, you’ll see a new field where you can insert js code:

    After adding this field, simply add the shortcode {{CODEmyjscode}} anywhere in THAT SPECIFIC page/post’s body field.

    If you don’t want to use a plugin, simply use the "Custom HTML" button in the WYSIWYG WordPress editor and paste your jscode inside the Custom HTML area, making sure to add tags

    -L

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search