You’re probably running into a function collision in terms of the function names, as pointed out in the comments on trueslant…..
Unfortunately not (I’m not quite that much of a beginner!!) – I’d already modified the function names.
Edit to expand:
Is there something I’m not understanding about how to call jQuery in functions.php? After a bit of fiddling, I’m getting a slightly different error message, but it still seems to be related to the jQuery – it’s not happy with this line:
jQuery(document).ready(function() {
which is now throwing up the error:
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in (my-site-path)/functions.php on line 78
Do I need to have explicitly enqueued jQuery somewhere else before I can use it like this?
Sorry, further addition.
I’m now enqueueing jQuery in functions.php, using the following method:
function myfunc_jquery_init() {
if (!is_admin()) {
wp_enqueue_script('jquery');
}
}
add_action('init', 'myfunc_jquery_init');
and still get the same error message on the same line when I paste in the jQuery stuff. So I guess it’s not the explicit enqueueing that’s the problem (unless I’m doing that wrong too, of course).
Any other ideas? Or is it my enqueueing that’s the problem?
jQuery is already included via wp_head. You don’t need to include it again. If you’re trying to just include your own function, have you tried adding it to header.php rather than functions?
jQuery is javascript, you can’t declare jQuery functions as PHP..
Put the jQuery stuff jQuery(document).ready(function() { etc..
into a JS file, then enqueue that file with jQuery as a dependancy(to make sure jquery is loaded before your script)…
Assuming you want to call this file from the theme’s folder..
<?php
function my_init_method() {
wp_enqueue_script( 'myjquery' , get_bloginfo('template_directory').'/yourfilename.js' , array('jquery') );
}
add_action('init', 'my_init_method');
?>
I’ll admit I’ve found myself in over my head here….
@t31os_ nice catch and code snippet. I probably should have mentioned that in the post.
@t31os_ I thought that was probably it…. I’ll try your approach – the various options I’d tried were dumping the jQuery portion in the page before any of the rest of it (i.e. even before the <head>
declaration!)
Thanks!
@songdogtech
Hey no worries, you’re welcome to offer input, even if it’s a little over your head, i welcome your opinion…. 😉
—-
@rogertheriault
We can’t think of everything, all of the time.. 🙂
Good article by the way, well presented and clear… i don’t always read an article to the end, and you managed to keep me interested all the way(even though i’d have no use for it myself).
—-
@flossiet
I’m not saying you can’t use JS in a PHP file full stop, but if you’re printing JS it needs to be part of a function that inserts into the correct part of the webpage, in the head of the page, or in the case of document.ready functions i believe you can actually load it later on in the page… since it’s waiting for the page to load before it’s executed anyway.. ie. when the document is ready/finished(i’m not 100% sure on that though, i could be wrong – it wouldn’t be a first)..
If you enqueue your script and make use of the dependancy parameter (third parameter of the function), it ensures the required script(s) ..(in this case jquery), is loaded before your own…. and only loads jQuery if it’s not loaded already..