WordPress.org

Forums

Problem with including jQuery (23 posts)

  1. tkse
    Member
    Posted 1 year ago #

    Hi.

    How should I go about including jQuery in 3.3.1? Every way I can think of either create problems with the admin-bar (hiding it) or it won't work.

    I have Googled for other ways, but it doesn't do anything good.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    jQuery is already available in WP 3.3.1. Perhaps you need to review wp_enqueue_script?

  3. tkse
    Member
    Posted 1 year ago #

    I must have missed something, because that didn't help.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    I'm sorry but all of the information that you need is on that page.

  5. tkse
    Member
    Posted 1 year ago #

    I guess me being a newbie at this doesnt help, but, more right to the point.

    How to write jQuery in a template-file? What I got from that is to add the script under "Load a default WordPress script from a non-default location". :S

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Is the script you want to load within your theme or located elsewhere?

  7. tkse
    Member
    Posted 1 year ago #

    The jQuery-script I have written(?) is in my sidebar.php theme-file. The jQuery source-file is on the Google API-site.

    I'm sorry if I dont understand everything, being bad at English doesnt help my situation. :)

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Take your script out of your sidebar file, save it as a standalone script and then enqueue it - eg:

    <?php
    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'google-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
        wp_enqueue_script( 'google-jquery' );
        wp_register_script( 'my-jquery', get_template_directory_uri() . '/my-script.js', 'google-jquery');
        wp_enqueue_script( 'my-jquery' );
    }    
    
    add_action('wp_enqueue_scripts', 'my_scripts_method');
    ?>
  9. tkse
    Member
    Posted 1 year ago #

    Okey, so I did what you suggested.

    I took my script out of sidebar.php and into a new file, myjquery.js inside the js-directory.

    Then, I posted this in my functions.php-file:

    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'google-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
        wp_enqueue_script( 'google-jquery' );
        wp_register_script( 'my-jquery', get_template_directory_uri() . 'js/myjquery.js', 'google-jquery');
        wp_enqueue_script( 'my-jquery' );
    }    
    
    add_action('wp_enqueue_scripts', 'my_scripts_method');

    I also tried seperating it into its own PHP-tag. It didn't work unfortunaly.

  10. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    That was just an example! you have to substitute your own script location and the correct Google jQuery call.

  11. tkse
    Member
    Posted 1 year ago #

    I did, didnt I? :)

    I can try with the 1.7.1-link.

  12. fordp
    Member
    Posted 1 year ago #

    I think you need a slash before js/myjquery.js

    ie:

    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'google-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
        wp_enqueue_script( 'google-jquery' );
        wp_register_script( 'my-jquery', get_template_directory_uri() . '/js/myjquery.js', 'google-jquery');
        wp_enqueue_script( 'my-jquery' );
    }    
    
    add_action('wp_enqueue_scripts', 'my_scripts_method');
  13. tkse
    Member
    Posted 1 year ago #

    Tried it, tried full URL, doesn't work. And it still creates a problem with the admin-bar.

  14. fordp
    Member
    Posted 1 year ago #

    Can you post your sites URL so I can take a quick look

  15. tkse
    Member
    Posted 1 year ago #

    Go ahead: http://goo.gl/Cpq2E :)

    But I did some troubleshooting myself, and there is two main problems. It cant find the jquery file and it looks for a text/html-file, not javascript.

  16. tkse
    Member
    Posted 1 year ago #

    I was able to fix it. :)

    In the end it was just sloppy mistakes ruining it for me.

  17. tkse
    Member
    Posted 1 year ago #

    Or not.

    The wp_enqueue_scripts didnt fix the problem with the admin-bar.

  18. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    he wp_enqueue_scripts didnt fix the problem with the admin-bar.

    Yeah. If you're dequeueing the version of jQuery bundled with WordPress - i.e. the version of jQuery against which the Admin Bar and other core code has been tested - and enqueueing your own version of jQuery, you assume some degree of risk of breaking things - things such as the Admin Toolbar.

  19. tkse
    Member
    Posted 1 year ago #

    Back at zero then.

    Any suggestions on how I should add my own script without breaking anything then?

  20. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    Any suggestions on how I should add my own script without breaking anything then?

    Is there any reason that you can't just use the core-bundled version of jQuery?

  21. tkse
    Member
    Posted 1 year ago #

    No, not really. But how would I go about writing jQuery using that? Just throwing a script in there doesnt work so thats kind of where Im lost. :P

    In the last version of WordPress I used, I could just link to the jQuery-file in the header and write my script, which in 3.3.1 causes problems.

  22. Chip Bennett
    Theme Review Admin
    Posted 1 year ago #

    No, not really. But how would I go about writing jQuery using that? Just throwing a script in there doesnt work so thats kind of where Im lost. :P

    Are you trying to enqueue a jQuery library, or are you trying to write an inline script using jQuery? If you're trying to write an inline script (e.g. for a slider or something), that should work just fine. Just be aware that WordPress uses no-conflict, so you can't nakedly use $ as a shorthand for jQuery.

    In the last version of WordPress I used, I could just link to the jQuery-file in the header and write my script, which in 3.3.1 causes problems.

    ...in which case, we're right back where @esmi started: wp_enqueue_script().

  23. tkse
    Member
    Posted 1 year ago #

    I'm trying to write a script, but I thought I needed to get the library-file first, because when I just write a script it doesnt work.

    <script>
    jQuery(document).ready(function(){
       jQuery('.single #recent-posts-2').appendTo('.topgrid');
     });
    </script>

    ...in which case, we're right back where @esmi started: wp_enqueue_script().

    That again causes problems. :S

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags