I create post using xml-rpc, in post_content I post HTML, in HTML I have js code. But after post is created in editor I don't see <script type="text/javascript"> tag. How I can fix it?

Example I post body

<script type="text/javascript">
    var ua = navigator.userAgent.toLowerCase();
    var flashInstalled = false;

And then in editor I see


    var ua = navigator.userAgent.toLowerCase();
    var flashInstalled = false;
  • 3
    You should add JavaScript properly. Post content is not the right place. – cybmeta May 15 '16 at 12:54
  • 1
    You should never put script tags inside post content, it's a huge security risk. If you need to embed something use a shortcode, that script looks like it would be better off in your theme template – Tom J Nowell May 15 '16 at 13:05

When you add content to the post_content, Wordpress wraps its own tags around everything and removes certain tags (such as script tags).

Plugins, such as Visual composer let you add raw JS, ie. your script tags.

However, if you don't want to use a plugin, you can add it directly into your page.php or footer.php (if you want the script for all pages, above closing tag).

For example, if you want this for only your home page, you could create a home.php by duplicating your page.php and renaming it home.php (https://developer.wordpress.org/themes/basics/template-hierarchy/)

Or, if you have a page other than home, you could always duplicate page.php and rename it page-yourpagename.php and insert it there.

| improve this answer | |
  • You should never include JavaScript in PHP templates as well. Use wp_enqueue_script() / wp_add_inline_script() combined with coditional tags to load the script in the pages you want. – cybmeta May 15 '16 at 15:23
  • Although its not recommended to use js directly into php templates, (use wp_enququq_script() for files instead), if you aren't loading a .js file or the wp_enqueue isn't working, you'll need to use the script tags. Quoted from Wordpress Documentation: "To include Javascript inside a post, you need to combine the call to the script file with the call to the JavaScript itself. <script type="text/javascript" src="/scripts/updatepage.js"></script> <script type="text/javascript"> your_function();</script>" Documentation: codex.wordpress.org/Using_Javascript. – jake May 15 '16 at 15:34
  • As I said in the comment, wp_add_inline_script() is also available to print inline scripts with dependencies. And if you need to print scripts with no dependencies, you should use wp_head, wp_footer or any other action appropiate for you, or a shortcode, but never include the script directly in template files. Of coruse, you can do what you wish, it is just what I consider the best option. And that reference from the Codex is very bad approach and it should be removed or explained in a more appropiate way. – cybmeta May 15 '16 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.