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>";
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: