Reputation: 137
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');
}
}
Upvotes: 0
Views: 3734
Reputation: 15979
There can be several things going on, But since the problem occurs only with functions.php
, One would think this is theme related .
You should be sure that the theme has both :
wp_head()
– ( immediately before </head>
)
And
wp_footer()
– ( immediately before closing </body>
)
If the theme author did not included both , or if the theme is missing even ONE of those functions , scripts might fail to load. ( and 80% of the plugins will have issues ) .
Also, you did not specify which theme you use (your own code ?? ) and if it is a child theme or not .
You are using get_template_directory_uri()
which can not be overriden by child themes , I do not know if this is a part of the problem, but you might want to also try using , get_stylesheet_directory_uri()
which in many cases is the better choice .
Seeing your update :
It is a very bad practice to replace the core jQuery with another one withpout a fallback. Anyhow ,you ca try dropping the !is_admin() condition, or alternatively log out and watch your sites code . ( I have a feeling that you are debugging it while logged in and with the wp admin bar on .. )
Upvotes: 2
Reputation: 6828
The proper hook to enqueue scripts on the front end is wp_enqueue_scripts
: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
Upvotes: 1