WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Unable to enqueue jQuery (2 posts)

  1. CGAdmin
    Member
    Posted 1 year ago #

    For one reason or another, I am unable to get my script to properly enqueue even though it works (nearly) perfectly embedded in the footer. The script is to allow for the overlapping/hiding of multiple pages on a small user login form on the header. I have attempted various versions of the following enqueue function:

    function my_scripts_method() {
    	wp_register_script('login_script',
    	get_template_directory_uri() . '/js/login.js',
    	array('jquery')
    	);
    	wp_enqueue_script('login_script'); }
    add_action('wp_enqueue_scripts', 'my_scripts_method');

    The login.js file is as follows:

    jQuery(document).ready(function() {
    	jQuery(".tab_content_login").hide();
    	jQuery("ul.tabs_login li:first").addClass("active_login").show();
    	jQuery(".tab_content_login:first").show();
    	jQuery("ul.tabs_login li").click(function() {
    		jQuery("ul.tabs_login li").removeClass("active_login");
    		jQuery(this).addClass("active_login");
    		jQuery(".tab_content_login").hide();
    		var activeTab = jQuery(this).find("a").attr("href");
    		if (jQuery.browser.msie) {jQuery(activeTab).show();}
    		else {jQuery(activeTab).show();}
    		return false;
    	});
    });

    As I mentioned, I am able to embed this same code in no conflict mode directly in the footer, but I would prefer to be able to keep these files as clean as possible.

    Am I performing the enqueue function incorrectly?

  2. CGAdmin
    Member
    Posted 1 year ago #

    I just got to thinking, the enqueue function references a JS file located at "/js/login.js". We are creating a multisite network and have been doing much of the testing on one of the "child" sites. This means the function was looking in the wrong directory for the JS file since the architecture is set up as http://mysite.com/child/ and the JS file is located at http://mysite.com/js/login.js.

    For those with the same stupid hat on as me, be sure to include a "../" in front of url references when working with multisite installations! (Otherwise, the code is just fine.)

    New line in function should be:

    get_template_directory_uri() . '../js/login.js',

Topic Closed

This topic has been closed to new replies.

About this Topic