Not sure why @RockerMaruf answer got downvoted. That is the best way to do this.
The example you have in your question can be used like this below (as seen in roots/soil plugin), but as @Rarst suggested it will break some things (like html5shiv) meant to be loaded in the header.
function all_js_to_footer() {
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
}
add_action('wp_enqueue_scripts', 'all_js_to_footer');
If you know what the script is that needs to be loaded in the head you could just dequeue it and add it using wp_head
... or have a fallback/check, maybe something like this:
add_action( 'wp_head', 'wp_theme_check_html5shiv', 1 );
function wp_theme_check_html5shiv() {
if ( !has_filter( 'wp_head', 'wp_enqueue_scripts' ) && !wp_script_is( 'html5shiv', 'done' ) ) {
add_action('wp_head', 'wp_theme_echo_html5shiv');
wp_dequeue_script('html5shiv');
}
}
function wp_theme_echo_html5shiv() {
echo '<!--[if (gt IE 5)&(lt IE 9)]> <script>window.html5={shivCSS:!1};</script> <script src="https://cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script> <![endif]-->' . "\n";
}
While the above fallback seems to work, it's pretty ugly and ridiculous.
So using the @kaiser method is the best approach, although here's a slightly modified approach ( thanks to a [discussion] with Theme Review Team's Ulrich Pogson on Github(https://github.com/roots/soil/issues/33#issuecomment-173838447) ) using new wordpress functions and not accessing globals directly.
Best Known Approach as of Jan 2016
function move_js_to_footer() {
foreach( wp_scripts()->registered as $script ) {
if ( 'html5' == $script->handle ) {
wp_script_add_data( $script->handle, 'group', 0 );
} else {
wp_script_add_data( $script->handle, 'group', 1 );
}
}
}
add_action( 'wp_enqueue_scripts', 'move_js_to_footer', 99 );