Jump to content
xisto Community
Nelson Blogs

Embedding Javascipt Into Wordpress Posts

Recommended Posts

I was wondering if there was any way to embed some javascript code into a WordPress post? ?I try and put the code into my post but all that it does is display the code text. ?I tried to look for a plugin but I had no luck finding one.

Share this post


Link to post
Share on other sites

Are you trying to insert javascript on each post as it appears? Or, are you trying to insert javascript as a footer, header?You can insert your javascript in item.php of your THEME, insert between anywhere you wish to attach.You can insert your javascript header, head or footer files. In your admin panel, you can add a custom box for any HTML codes (not tested with javascript from my end). Just go to Appearance > Widgets > and drag TEXT to the sidebar. Then, you can insert any HTML code which will be used on every sidebar.

Share this post


Link to post
Share on other sites

I think this is what you need to read : https://codex.wordpress.org/Using_Javascript

 

I've seen that java-script files are stored separately in js folder. That allows it is easy to organize if you've lot of code to put into. In documentation there is way mentioned how to add into template files or link external.

 

I hope the documentation link that is given in this post will answer most of your questions. If not, let me know what exactly you're trying for. I'll see with what i can help.

Share this post


Link to post
Share on other sites

You weren't very clear about this, so I'm going to assume you want to add a small piece of javascript to every post on your blog. In case that was what you meant (this can be used for loading comments with ajax for example or some other interactive function).This depends on the theme you have. Try to find the php file(s) in your theme that load the posts(some themes have separate archive, single etc. files for loading). Now, what you do is you search for the loop that's used to get your posts. There should be php functions such as wp_posts(), x_storytitle(). Insert your javascript where you want it to be in a post and you can also modify the post content's displaying order a bit. This is for embedded javascript. A much easier way to do this is view source in browser and search for the IDs of post elements in the post, then apply external javascript to those by getElementbyId in the .js file(s).

Share this post


Link to post
Share on other sites

Sorry for not being clear in my topic above. I need javascript in a single post, not all of them. I cannot put it in the footer or the header because then it would become site-wide. Thank you so much for your help

Share this post


Link to post
Share on other sites

I'm not that familiar with WordPress but I did use it before.. If you want to embed a JS on a single post, why not create a copy of theme..say you have a Page.ph, copy it and create a Page2.ph and use it as a template of that single page where you want the JS to appear...and put the Javascript on the Page2.ph file.. This is the only answer I can think of so far...Will post for new idea..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.