• Resolved Olly343434

    (@olly343434)


    Hi,

    I have been trying to add a scriptto my site that depends on jquery, but I can’t get it to work. Any help would be much appreciated. Thank you.

    Here’s what I have in my functions.php:

    /*Adds accordion script*/
    
    function accordion_script(){
          wp_register_script('accordion', get_template_directory_uri() . 'http://kubetest.kube.co.uk/wp-includes/js/accordion-script.js', 'jquery');
          wp_enqueue_script('accordion');
        }
    
    add_action( 'wp_enqueue_scripts', 'accordion_script');

    Here is the contents of accordion-script.js:

    $j(document).ready(function(){
    
    var $j = jQuery.noConflict();
    
      $j(function($) {
    
    	var allPanels = $j('.post-list > li > .entry-summary').hide();
    
    	$j('.post-list > li').click(function() {
    	  allPanels.slideUp();
    	  $j(this).parent().next().slideDown();
    	  return false;
    	});
    
      })(jQuery);
    
    });
Viewing 5 replies - 1 through 5 (of 5 total)
  • Where exactly did you upload your accordion.js script to?

    Moderator bcworkz

    (@bcworkz)

    There is already an “accordion” script registered for the jQuery UI version, you should register the kube.co.uk script under a different handle (assuming it is not the jQuery UI version). Also, your script reference doesn’t make sense, it ends up looking like http://www.yourdomain.com/wp-content/themes/yourtheme/http://kubetest.kube.co.uk/wp-includes/js/accordion-script.js

    All dependency arguments should be in array form, even if there is only one dependency. Finally, you don’t really need to enqueue the kube.co.uk script, though it doesn’t hurt, just register it. You do need to enqueue your local script and list both the handle for the kube.co.uk script and “jquery” as dependencies.

    Thread Starter Olly343434

    (@olly343434)

    Hi and thanks for the response from you both.

    I have changed the handle to another name. For the url, I am not sure of the correct way to reference the url. It is in the wp-includes/js folder (ie. not in my theme folder)?

    How would I do this?

    Many thanks

    Thread Starter Olly343434

    (@olly343434)

    to clarify, I would like my relative url to start from the root directory and not from the theme directory.

    Thanks

    Thread Starter Olly343434

    (@olly343434)

    Finally got it working with this code :

    /*Adds accordion script*/
    
    function accordion_script(){
    
          wp_register_script(
    
    	  'slidedown-posts',
    	  get_bloginfo('stylesheet_directory') .   '/js/accordion-script.js' ,
    	  array ('jquery'),
    	  '1.0',
    	  true  
    
    	  );
          wp_enqueue_script('slidedown-posts', array ('jquery') );
        }
    
    add_action( 'wp_enqueue_scripts', 'accordion_script');

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Can't load my script in WP’ is closed to new replies.