Joy
(@joyously)
That code simply defines a function. It does not invoke the function.
You typically use add_action() to have WP invoke your function at the appropriate time.
https://developer.wordpress.org/reference/functions/add_action/
Thread Starter
wyclef
(@wyclef)
I see. Let me expand upon this code a bit to see if you might be able to shed some more light on what is going on here. The first chunk of code is from an older build and works, the second chunk is from a newer build and does not work, specifically in regard to the metajs /admin/init.js portion. I realize, more context would probably be better, however this is an isolated issue where I can copy the jq_scripts chunk of code from the working one, into the one that isn’t working, and then my problem is solved but am trying to understand what they were trying to do by taking it out in the first place, and how to possibly solve it more simply. Not sure if this is entirely doable without more understanding of the what’s and the why’s but figure I’d put this out here for starters. Thanks for your time.
works
if (is_admin() ){
function admin_scripts(){
wp_register_script('metajs', INDEX_JS .'/admin/init.js', array('jquery','media-upload','thickbox'));
wp_enqueue_script('metajs');
}
}
if(!is_admin()) {
add_action('wp_enqueue_scripts', 'jq_scripts');
}
function jq_scripts(){
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js", false);
wp_enqueue_script('jquery');
}
add_action('admin_enqueue_scripts', 'admin_scripts');
Not Working
if (is_admin() ){
function admin_scripts(){
wp_register_script('metajs', INDEX_JS .'/admin/init.js', array('jquery','media-upload','thickbox'));
wp_enqueue_script('metajs');
}
}
if (!is_admin() ){
function front_scripts(){
global $smof_data, $theme_options;
$theme_data = wp_get_theme();
wp_enqueue_script('jquery');
wp_register_script('Init', INDEX_JS. '/init.js' ,array('jquery'), TRUE);
wp_register_script('QueryLoader', INDEX_JS .'/jquery.queryloader2.js', array('jquery'), true);
wp_register_script('scripts', INDEX_JS .'/scripts.js', array('jquery'), true);
wp_register_script('shortcodes', INDEX_JS .'/shortcodes.js', array('jquery'), true);
wp_enqueue_script('QueryLoader');
wp_enqueue_script('Init');
wp_enqueue_script('scripts');
wp_enqueue_script('shortcodes');
}
add_action('wp_footer', 'front_scripts');
-
This reply was modified 5 years, 1 month ago by
wyclef.
Joy
(@joyously)
Again, the one that is not working does not have add_action for the admin scripts. (the first one has it — last line)
The working one is using an old, external version of jQuery instead of the one bundled with WordPress. This is not a good thing to do.
The non-working one does things weird. You shouldn’t enqueue jquery by itself. You should use it as a dependency, so that the scripts come out in the right order, because there could be plugins or theme that use it also. Scripts don’t have to be registered if you immediately enqueue them. Simply put all the parameters onto the enqueue instead of the register. Use script handles with a prefix, so that you don’t accidentally use the same handle as another plugin or theme or WP.
The best action for front end scripts is ‘wp_enqueue_scripts’. Use the parameter to put it in the footer if needed, but don’t use ‘wp_footer’ since it is theme dependent. See https://codex.wordpress.org/Plugin_API/Action_Reference
Thread Starter
wyclef
(@wyclef)
Thanks for the info. I just ended up needing a simple add action line.