Quantcast
Channel: When to use add_action('init') vs add_action('wp_enqueue_scripts') - WordPress Development Stack Exchange
Viewing all articles
Browse latest Browse all 4

When to use add_action('init') vs add_action('wp_enqueue_scripts')

$
0
0

In my theme's functions.php, I'm calling an add_action in order to gain a measure of control on where jquery is loaded (in the footer along with my theme's other scripts).

The problem I'm having is that when I use add_action('wp_enqueue_scripts'), it only appears to fire if no plugins are loaded. However, the add_action('init') method works in all cases.

I can't recall why, but I believe that add_action('wp_enqueue_scripts') is preferred in this case. If that's true, how can I get it to work in all cases?

In functions.php

//if(!is_admin()){add_action('init', 'my_theme_init');} //THIS WORKS ALL THE TIME//add_action('wp_enqueue_scripts', 'my_theme_init'); //THIS ONLY WORKS WHEN NO PLUGINS PRESENTif(!is_admin()){    require_once(TEMPLATEPATH . '/functions_public.php');   }

In functions_public.php

function my_theme_init(){/* PREVENT DUPLICATE COPIES OF JQUERY FROM PLUGINS**************************************************/wp_deregister_script('jquery');/* LOAD THE LOCAL WORDPRESS COPY OF JQUERY AND THEME CUSTOM SCRIPTS IN THE FOOTER***********************************************/wp_register_script('jquery', get_bloginfo('template_directory').'/scripts.mythemescripts.js',false,false,true);wp_enqueue_script('jquery');}

The 2nd method, using add_action('wp_enqueue_scripts') apparently does not get executed in conditions where a plugin is present that writes out script dependencies to the theme.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images