WordPress.org

Ready to get started?Download WordPress

Forums

jQuery and functions.php problems: PHP errors OR script called before head (10 posts)

  1. FlossieT
    Member
    Posted 4 years ago #

    I've been trying to implement a solution for moderating comment karma from TrueSlant.

    However, when I get to the section on triggering the AJAX using jQuery, everything goes wrong.

    I'm including the various functions in functions.php, and when I paste the jQuery code in as shown on the True/Slant page, I get the PHP error message:

    Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in (my-site-path)/functions.php on line 79.

    If I enclose it in <script> tags (in functions.php), the block of Javascript is dumped in the page before even the <head>, so unsurprisingly doesn't work.

    I'm on a bit of a steep learning curve here with both PHP and Javascript so would really appreciate pointers as to where I'm going wrong!

    Many thanks,

    Rachael

  2. You're probably running into a function collision in terms of the function names, as pointed out in the comments on trueslant.....

  3. FlossieT
    Member
    Posted 4 years ago #

    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?

  4. FlossieT
    Member
    Posted 4 years ago #

    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?

  5. 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?

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    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');
    ?>
  7. I'll admit I've found myself in over my head here....

  8. Roger Theriault
    Member
    Posted 4 years ago #

    @t31os_ nice catch and code snippet. I probably should have mentioned that in the post.

  9. FlossieT
    Member
    Posted 4 years ago #

    @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!

  10. Mark / t31os
    Moderator
    Posted 4 years ago #

    @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..

Topic Closed

This topic has been closed to new replies.

About this Topic