• Resolved Kathyiscool

    (@kathyiscool)


    Hi,

    This is my first time adding jQuery to wordpress. I’ve been working at this for hours and made some progress but I finally decided to reach out for help.

    I’m trying to include this jQuery plugin into my child theme. I’ve already registered and enqueued the script file on the functions.php page.

    I’m having a problem with step 2 of the code.

    How do I add this to my header?

    $(document).ready(function() {
    $('img').hover_caption();
    });

    Would I have to make another script file and register and enqueue that like I did before?

    Thank you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Yes, you can use another script file, or you can just put it in SCRIPT tags directly in the HEAD (normally using the wp_head action).

    Thread Starter Kathyiscool

    (@kathyiscool)

    Thank you for replying!

    Can you show me an example of the wp_head action?

    I tried and it broke my pages.

    add_action('wp_head','call_caption_hover');
    function call_caption_hover(){
    	echo '<script type="text/javascript">jQuery(document).ready(function() {jQuery('img.showDescription').hover_caption();});</script>';
    }

    You do not add scripts to wp_head() and you should not add them to the <head></head> section of a theme. They need to be correctly enqueued using wp_enqueue_script().

    Thread Starter Kathyiscool

    (@kathyiscool)

    Thanks, my script files show up just fine. I think there’s something wrong with the plugin itself. I’m going to code from scratch.

    You do not add scripts to wp_head()… They need to be correctly enqueued…

    For static script and style files where you may need version and dependency control, that’s useful advice. For scripts and styles generated by other code, it’s not. For example, TwentyThirteen adds its custom header style directly in the head because the style is generated by code, not a static file. This does not cause any problems.

    May I suggest that you review Using_Javascript?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Calling JQuery Plugin On Child Theme’ is closed to new replies.