WordPress.org

Ready to get started?Download WordPress

Forums

double jquery, complete mess (11 posts)

  1. DocHoloday
    Member
    Posted 3 years ago #

    I've unfortunately inherited a really poorly written theme on a site I'm going to maintain.

    In the header, it's already calling jquery...

    <script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery.js"></script><br />
        <script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/scripts.js"></script><br />
          <script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/prettyPhoto/js/jquery.prettyPhoto.js"></script>  </p>
    <p>    <?php wp_head(); ?>

    As soon as I install any plugin with jquery (nextgen gallery for example) that gets added to the header, everything starts to break.

    I know it's because it's loading jquery twice, but here's the kicker, removing the original call from the header.php breaks everything. Homepage slider, dropdown menus, the whole shebang. I think whoever wrote this put in a bunch of dependencies to it all over the place.

    If I use the function in NGG to stop the scripts from loading...

    define('NGG_SKIP_LOAD_SCRIPTS',TRUE);

    ...then the menus/sliders work again, but it kills off Nextgen's galleries and lightboxes.

    I'm completely confused as to where to start fixing this.

    In a perfect world, if I could get either the theme to stop loading jquery OR nextgen to only load jquery when it's used (or, just NOT on the homepage) then I'd be all set. I can't seem to accomplish either.

    Anyone have any idea what I'm talking about? I've been staring at this for way too long and I think I'm just confusing myself more and more. HELP!

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Have you tried enqueueing your scripts?

  3. DocHoloday
    Member
    Posted 3 years ago #

    I did actually. I tried...

    <?php wp_enqueue_script('jquery');

    ...in the header in place of the script call. It didn't load at all and the navigation and home page slider stopped working.

    This is what the entire header looks like if I enable NextGen...

    [Large code excerpt removed by moderator. Per forum rules, please use the pastebin for all large code excerpts. It works better anyway.]

    Like I said, it's a trainwreck. I inherited this problem from someone else. They "love" their theme and don't want to change it, and just want "this to work".

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    No - you don't enqueue jQuery. You enqueue your custom script and make it dependant on jQuery. Personally, I enqueue scripts in functions.php and use the template_redirect action:

    function init_myscript() {
        wp_enqueue_script('myscript',
        get_bloginfo('template_directory') . '/myscript.js',
        array('jquery'),
        '3.0.2'
        );
    }
    if( !is_admin() ) add_action('template_redirect', 'init_myscript');
  5. DocHoloday
    Member
    Posted 3 years ago #

    Ohhhh, ok, I gotcha. So, would I then remove both jquery and the scripts.js from the header and replace it with "wp_enqueue_script('myscript')" ?

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    Using a function similar to the one I gave above, you'd remove both jquery and the scripts.js from the header - full stop. You don't need to add anything other than the function. The function itself is instructing WP to add the scripts appropriately.

  7. DocHoloday
    Member
    Posted 3 years ago #

    Gotcha. I'll give that a try this afternoon. Thank you so much for the help! I'll let you know if it works.

  8. DocHoloday
    Member
    Posted 3 years ago #

    Unfortunately, it didn't work. I added that to my functions.php and removed the scripts from the header.php. It broke my front page slider. Actually, any time I remove the jquery link from the header, it instantly breaks. I don't understand how the theme can be so dependent on it being hardlinked like that. :(

  9. webcodeslinger
    Member
    Posted 3 years ago #

    I am also having this issue.

    I basically added the above code block within PHP tags to my functions file, updating the URL and name of the script I want to be included, and it doesn't do anything - WP still loads jQuery below my jquery-functions file and screws everything up.

  10. DocHoloday
    Member
    Posted 3 years ago #

    In the end, what I ended up doing was removing just about everything from the header above the wp_head call, then adding whatever script I was calling, like...

    <?php wp_head(); ?>
    
    	<script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/scripts.js"></script>

    Then, in my functions.php, I basically unloaded and reloaded jquery...

    if( !is_admin()){
    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"), false, '1.4.2', false);
    wp_enqueue_script('jquery');
    }

    That seems to be working for me. It's not a perfect solution, but it's worth trying. Good luck!

  11. aparna_ap
    Member
    Posted 3 years ago #

    Thanks DocHoloday, I have same problem and it also work for me.

Topic Closed

This topic has been closed to new replies.

About this Topic