Just hook to wp_enqueue_scripts
for both registering and enqueueing. You’ve registered with in_footer
(last parameter in your code) true
so what you need is:
function print_my_royal_slider_script() {
global $add_my_script;
if ( ! $add_my_script ) return;
wp_enqueue_script('my-script');
}
add_action ('wp_enqueue_scripts', 'print_my_royal_slider_script');
The problem is going to be with $add_my_script
. That has to be set before wp_head
runs. If that is not possible– if, for example, you are setting it in your Loop–, I believe your only choice is to echo the script hooked to wp_footer
but you can (almost) do it properly by using the $wp_scripts
object to do the work.
function print_my_royal_slider_script() {
global $wp_scripts,$add_my_script;
if ( ! $add_my_script ) return;
wp_enqueue_script('my-script');
$wp_scripts->do_item('my-script');
}
add_action ('wp_footer', 'print_my_royal_slider_script');