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 attempting to use wp_enqueue_script('jquery') to load the builtin WP jquery library but my jquery functions aren't working when I do this. Only when I hardcode a reference to the script do my functions work...

Example that works... (this is inside my functions.php file, which is adding a dashboard widget)...

/* Dashboard Widget */
if(is_admin())
{
function my_dashboard_widget_function() 
{ 
    $rss = fetch_feed( "http://mysite/my.rss" );
    if ( is_wp_error($rss) ) 
    {
        if ( is_admin() || current_user_can('manage_options') ) 
        {
            echo '<p>';
            printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
            echo '</p>';
        }
     return;
    }

    if ( !$rss->get_item_quantity() ) 
    {
        echo '<p>This feed is currently offline</p>';
        $rss->__destruct();
        unset($rss);
        return;
    }
echo "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>"

echo "<script type='text/javascript'>//some jquery methods here</script>";

Example that does not work...

/* Dashboard Widget */
if(is_admin())
{
function my_dashboard_widget_function() 
{ 
    $rss = fetch_feed( "http://mysite/my.rss" );
    if ( is_wp_error($rss) ) 
    {
        if ( is_admin() || current_user_can('manage_options') ) 
        {
            echo '<p>';
            printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
            echo '</p>';
        }
     return;
    }

    if ( !$rss->get_item_quantity() ) 
    {
        echo '<p>This feed is currently offline</p>';
        $rss->__destruct();
        unset($rss);
        return;
    }
wp_enqueue_script('jquery');
echo "<script type='text/javascript'>//some jquery methods here</script>";
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You need to enqueue your script at certain points. Rather than just placing the wp_enqueue_script call inside your dashboard widget function, you need to place it in a separate function and hook on to the proper action.

So:

function add_jquery_to_my_widget() {
    wp_enqueue_script( 'jquery' );
}
add_action( 'init', 'add_jquery_to_my_widget' );
share|improve this answer
    
Thanks EAMann :) –  Scott B Oct 14 '10 at 3:31
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.