Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to enqueue jQuery UI in a plugin under a class method. I have this so far:

class AV_Slider {

function slider_load_js(){
        wp_register_script('jquery-ui-core', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js');
        wp_enqueue_script('jquery-ui-core', array('jquery') );
        wp_enqueue_script('av_slider_js', AV_SLIDER_BASE_URL . 'includes/js/av_slider.js', array( 'jquery' ));
}


function __construct(){
    $this->hooks();
}
function hooks(){
    add_action('wp_enqueue_scripts', 'slider_load_js');
}

And I get this error on the page:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'slider_load_js' not found or invalid function name

I'm not sure how __construct() works other than that it creates a new Simple XML object. I also tried adding the action add_action('wp_enqueue_scripts', 'slider_load_js'); after the function but this caused an error as well.

Any help?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

First of all - I'm not sure why you are registering jquery-ui-core. WordPress has already registered jQuery UI scripts (see here). You should use the shipped jquery and jquery-ui scripts rather than registering your own.

As for the error - the second argument add_action (and apply_filter for that matter) is a callback. To have a class method as a callback, you need to specify the class. In your case, using $this:

add_action('wp_enqueue_scripts', array($this,'slider_load_js'));
share|improve this answer
    
Also you first use of wp_enqueue_script is incorrect, and would be, in any case, unnecessary. –  Stephen Harris Jun 20 '12 at 19:06
    
Thanks got it. Do you use the __construct() method each time you need to add actions/filters in a class? –  AlxVallejo Jun 20 '12 at 19:12
    
? - you would generally construct the class once and then all your actions/filers are added then... –  Stephen Harris Jun 20 '12 at 19:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.