0

I'm working on a wordpress blog and I want to add simple JS script to one of my pages. But when you go to edit->text and I add my script in <scirpt> tags it's not working. So I want to know how to insert the script or a .js file with the script in it? I have access only to the wp-admin and I don't have access to the files. Can someone suggest some ways to solve this?

Thank you in advance

1 Answer 1

1

I'm not too sure I understand your question 100%, but I'm gonna try to answer the best I can.

If you're adding the JavaScript via Wordpress's Page/Post text editor, you have to make sure you're using the HTML editor (not visual).

That would be step 1.

Step 2 would be adding the script itself. Make sure you don't have any line breaks in between your code, or it will break (learned that from experience)

For example, this will work:

<script type="text/javascript">
  var mySpecialFunction = function() {
    return 'oh yeaaa';
  };
  mySpecialFunction();
</script>

This will not:

<script type="text/javascript">

  var mySpecialFunction = function() {
    return 'oh yeaaa';
  };

  mySpecialFunction();

</script>

At least, that's what I found with the versions of Wordpress that I'm using (dunno about their latest release)

Hope this helps!

Good luck.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes you understood me right! I'm using the HTML editor but when I type the code and save the post/page and it's putting some <p> tags around the <script tag and stuff like that and I'm not sure if this is the problem because when I type my script in Firefox console it's working perfectly :(
Hmmm.. I had a feeling that was the case, but I was really hoping it wasn't the case. WP's text/HTML editor is great, except when it tries to auto do stuff for you like creating <div> elements or injecting <p> tags. Have you tried adding the <script></script> code without any linebreaks at all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.