This is probably super simple answer, but I just can’t figure this out.
Basically, I’ve got a php page only starting with opening <?php
tag.
I’ve got a jquery script that I need to call on this page.
<script type="text/javascript">
$('input[name="_manage_item"]').click(function() {
$('input[name="_item"]')[this.checked ? "show" : "hide"]();
});
</script>
From what I researched, I need to load the script by placing
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
into the <head>
tags of the page.
But my page doesn’t have the html <head>
or <body>
tags. Where do I call the script then ?
I tried to place it in front of the opening <?php
tag, but that broke the page.
What is the correct way of doing this ?
You can place your javascript outside the
php
tags at the bottom of you page. But when you use the php in a web page, you should add thehtml
andhead
andbody
tags. A simple php page could look like this:Relying on your
wordpress
tag, I suggest you to read this article. There are described all details concerningjavascript
usage withinwordpress
. Probably the most common way is to usewp_register_script()
function.I would also advise you to merge your code to a distinct file, and load it straight after
jquery
library. It is a good practice, because in such a way browser can cache your script and would not download it on every page request. You should also wrap it into a$( document ).ready()
function, to be sure that it will be executed only only when document is in ready state.