0

after reading everywhere about the proper way to use jQuery and Wordpress: http://scribu.net/wordpress/optimal-script-loading.html http://www.ericmmartin.com/5-tips-for-using-jquery-with-wordpress/

I came up with this code:

First the functions.php

add_action('init', 'deregister_wp_jq');
add_action('init', 'register_my_jquery');
add_action('init', 'enqueue_my_jquery');
add_action('init', 'register_my_scripts');
add_action('wp_footer', 'print_my_scripts');

function deregister_wp_jq () {
    wp_deregister_script('jquery');             
}

function register_my_jquery ()
{
    wp_register_script('my-jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js", array(), '1.7.2', true);
    wp_register_script('my-jquery-ui', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js", array('my-jquery'), '1.7.2', true);
}


function enqueue_my_jquery () {
    wp_enqueue_scripts('my-jquery');
    wp_enqueue_scripts('my-jquery-ui');
}


function register_my_scripts() {
    wp_register_script('setNavigation', get_template_directory_uri() . '/js/jquery.setNavigation.js', array('my-jquery'), null, true);
    wp_register_script('spasticNav', get_template_directory_uri() . '/js/jquery.spasticNav.mod.js', array('my-jquery', 'my-jquery-ui', 'setNavigation'), null, true);


}




function print_my_scripts() {
        wp_print_scripts('spasticNav');
        wp_print_scripts('setNavigation');
}

This is spasticNav, the plugin I want to use:

    // JavaScript Document
    (function($) {
        $.fn.spasticNav = function(options) {
            options = $.extend({
                overlap : 20,
                speed : 500,
                reset : 1500,
                color : '#b80606',
                easing : 'easeOutExpo'
            }, options);
            return this.each(function() {

                var nav = $(this), currentPageItem = $('#selected', nav), blob, reset;

                $('<li id="blob"></li>').css({
                    width : currentPageItem.outerWidth(),
                    height : currentPageItem.outerHeight() + options.overlap,
                    left : currentPageItem.position().left,
                    top : currentPageItem.position().top - options.overlap / 2,
                    backgroundColor : options.color
                }).appendTo(this);

                blob = $('#blob', nav);

                $('li:not(#blob)', nav).hover(function() {
                    // mouse over
                    clearTimeout(reset);
                    blob.animate({
                        left : $(this).position().left,
                        width : $(this).width()
                    }, {
                        duration : options.speed,
                        easing : options.easing,
                        queue : false
                    });
                });

            });
            // end each

        };

    })(jQuery); 

jQuery(function() 
        {
        jQuery("#nav").spasticNav();

        });

Then this is the jquery.setNavigation.js

function setNavigation() {
    var path = window.location.pathname;
    path = path.replace(/\/$/, "");
    path = decodeURIComponent(path);

    $(".nav a").each(function () {
        var href = $(this).attr('href');
        if (path.substring(0, href.length) === href) {
            $(this).closest('li').addClass('active');
        }
    });
}

$(function () {
    setNavigation();
});

I got a headache, backache, whatever-ache, spent hours and hours to fix this, but still, it does not work. Please, help me. I tried everything from different tutorials, etc., but nothing.

P.S.: I added the function to set the active state for

  • elements of the (the menu), but it does not work yet.

    ----------------------------------------- UPDATE -------------------------------

    OK, so after other 12 hours or so passed searching on Google and debugging the code, finally I found the problem. It has to do with font-face as explained here: Lava Lamp navigation jQuery Firefox

    This is the code that I am using now in functions.php

    function my_scripts_method() { // Creates the my_scripts_method function
        wp_deregister_script('jquery'); // Deregisters the built-in version of jQuery
        wp_register_script('jquery', 'http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', false, null, true); // Registers a CDN hosted version. If browsing on a secure connection, use HTTPS.
        wp_enqueue_script('jquery'); // Activates the jQuery script
        wp_register_script('jquery-ui', 'http' . ($_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js', false, null, true); // Registers a CDN hosted version. If browsing on a secure connection, use HTTPS.
        wp_enqueue_script('jquery-ui'); // Activates the jQuery script
    
        wp_register_script('spasticNav', get_template_directory_uri() . '/js/jquery.spasticNav.mod.js', false, null, true); // Registers your javascript file
        wp_enqueue_script('spasticNav'); // Actives your javascript file
    }
    add_action('wp_enqueue_scripts', 'my_scripts_method'); // Tells WordPress to run the my_scripts_method function
    

    And this is for the jquery.spasticNav.mod.js

    // JavaScript Document
    (function($) {
        jQuery.fn.spasticNav = function(options) {
            options = jQuery.extend({
                overlap : 20,
                speed : 500,
                reset : 1500,
                color : '#b80606',
                easing : 'easeOutExpo'
            }, options);
            return this.each(function() {
    
                var nav = jQuery(this), currentPageItem = jQuery('#selected', nav), blob, reset;
    
                jQuery('<li id="blob"></li>').css({
                    width : currentPageItem.outerWidth(),
                    height : currentPageItem.outerHeight() + options.overlap,
                    left : currentPageItem.position().left,
                    top : currentPageItem.position().top - options.overlap / 2,
                    backgroundColor : options.color
                }).appendTo(this);
    
                blob = jQuery('#blob', nav);
    
                jQuery('li:not(#blob)', nav).hover(function() {
                    // mouse over
                    clearTimeout(reset);
                    blob.animate({
                        left : jQuery(this).position().left,
                        width : jQuery(this).width()
                    }, {
                        duration : options.speed,
                        easing : options.easing,
                        queue : false
                    });
                });
    
            });
            // end each
    
        };
    
    })(jQuery); 
    
    jQuery(function() {
        jQuery.noConflict();
        jQuery(document).ready(function() {
        document.onreadystatechange = function () {  
        if (document.readyState == "complete") {
        jQuery('#nav').spasticNav();}}});});
    

    This is the error: Uncaught TypeError: Cannot read property 'left' of null

    and it refers to these statements:

    left : currentPageItem.position().left,
    top : currentPageItem.position().top - options.overlap / 2
    

    I hope you can help me now that i pinpointed the issue.

    --------------------------------- UPDATE 2 - FIXED -----------------------------------

    OK, it turns out that in the static version of the website, To feed something selected to spasticNav, I used:

    <li> <a href="#acasa" id="selected">Acasa</a> </li>
    

    WP provides already a script that changes automatically the status and it does so by adding the class .current_page_item Therefore, I modified this line accordingly:

    var nav = jQuery(this), currentPageItem = jQuery('#selected', nav), blob, reset;
    
    var nav = jQuery(this), currentPageItem = jQuery('.current_page_item', nav), blob, reset;
    
  • Community
    • 1
    • 1
    Davide Pugliese
    • 366
    • 7
    • 16
    • is there any error in the console? and what is not working – Arun P Johny Jul 03 '13 at 03:06
    • Can you tell us what is wrong/not working? It is not clear from the question. You have just dumped your code. – eggy Jul 03 '13 at 03:16
    • I cannot see the effect that you can appreciate on the author's page: http://net.tutsplus.com/tutorials/html-css-techniques/how-to-build-a-lava-lamp-style-navigation-menu/ This on the static version of the website that I made works properly. – Davide Pugliese Jul 03 '13 at 03:20
    • I cannot see any php error popping up. – Davide Pugliese Jul 03 '13 at 04:26
    • I checked again and this time I have enabled also the script debugging feature in wp and there was an error with wrong link to jquery-ui like: http://localhost/wordpresshttp://... which of course was wrong. I fixed it setting jquery-ui locally. Now there are no errors, but still, it doesn't work. :( – Davide Pugliese Jul 03 '13 at 05:15
    • Now in the console I see this nice error with the solution provided by Chris Fernandi: SCRIPT5009: '$' is undefined wordpress, line 63 character 19 – Davide Pugliese Jul 03 '13 at 06:09

    2 Answers2

    0

    You don't need that many functions and actuons to make this work. Try this: http://gomakethings.com/jquery-wordpress/

    Chris Ferdinandi
    • 1,892
    • 4
    • 26
    • 34
    0

    Even though you said you want to use the proper way to use jQuery, you're not doing it properly at all. What you'll need in your functions.php is this:

    wp_register_script('jquery');             
    

    Other than that you won't need anything at all, you'll only have to remember that jQuery in WordPress is in noConflict mode. So always start your scripts with jQuery rather than $.

    user2019515
    • 4,495
    • 1
    • 29
    • 42