I'm experiencing a weird thing in WordPress about wp_enqueue_script(), what I want to happen is to add a script on the page by adding an action hook to functions.php but when I do, It doesn't add the script on the page but when I put the code as a plugin it now adds the script on the page
NOTE: in functions.php when I add the script in the footer: add_action('wp_footer','function_name'); it works fine but when I add it on the wp_head and init it doesn't add the script when I put the code on the functions.php, but it works perfectly when I do it as a plugin even if I add it on wp_head and init
I NEED TO ADD THE SCRIPT ON THE INIT OR WP_HEAD BUT IT DOESN'T WORK WHEN I PLACE THE CODE ON FUNCTIONS.PHP I NEED THE SCRIPT TO BE PART OF THE THEME NOT AS A PLUGIN
//functions.php
add_action('init', 'function_name');
function function_name(){
wp_enqueue_script( 'script_holder', get_template_directory_uri() . '/script.js');
}
//as a plugin
add_action('init', 'function_name');
function function_name() {
$plugin_location=WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
if( !is_admin()){
wp_enqueue_script( 'script_holder', $plugin_location . '/script.js');
}
}
I know it should work even in functions.php but I'm really wondering why is this happening can anyone help me and explain to me why is this happening, please?
thank you very much
Basically This is Exactly What I'm doing:
add_action('init', 'override_jquery');
function override_jquery() {
if( !is_admin()){
wp_deregister_script('jquery');
wp_enqueue_script( 'nashgraphics_jquery_library', get_template_directory_uri() . '/bootstrap/js/jquery-1.9.0.min.js');
}
}