Last active
August 29, 2015 14:19
-
-
Save bjork/8c0019acb3f6a39750d5 to your computer and use it in GitHub Desktop.
Helper functions to move scripts enqueued by WordPress or plugins to bottom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Move script(s) to bottom | |
* | |
* @param string|array $script_handles Script handles to be moved to bottom | |
* @param array $args Additional arguments. Defaults to array( 'move_deps' => false ). Set to true to also move scripts dependencies. | |
*/ | |
function h1_move_script_to_bottom( $script_handles, $args = array() ) { | |
$defaults = array( | |
'move_deps' => false | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
// Make it an array if not already. | |
if ( ! is_array( $script_handles ) ) { | |
$script_handles = array( $script_handles ); | |
} | |
global $wp_scripts; | |
// Loop through scripts about to be moved. | |
foreach ( $script_handles as $script_handle ) { | |
// Check if the script is enqueued. | |
if ( ! wp_script_is( $script_handle ) ) { | |
continue; | |
} | |
if ( $args['move_deps'] ) { | |
// Get script data. | |
$script = $wp_scripts->registered[ $script_handle ]; | |
// Move script's dependencies to bottom. | |
foreach ( $script->deps as $dep ) { | |
$wp_scripts->add_data( $dep, 'group', 1 ); | |
} | |
} | |
// Move the main script to bottom. | |
$wp_scripts->add_data( $script_handle, 'group', 1 ); | |
} | |
} | |
/** | |
* Move script(s) to bottom if none of scripts as the second parameter are enqueued. | |
* | |
* @param string|array $script_handles Script handles to be moved to bottom | |
* @param string|array $dependency_script_handles Script handles that cannot be enqueued | |
*/ | |
function h1_move_script_to_bottom_if_another_not_enqueued( $script_handles, $dependency_script_handles ) { | |
// Make it an array if not already | |
if ( ! is_array( $dependency_script_handles ) ) { | |
$dependency_script_handles = array( $dependency_script_handles ); | |
} | |
// Don't do anything if any of the scripts are enqueued | |
foreach ( $dependency_script_handles as $dependency_script_handle ) { | |
if ( wp_script_is( $dependency_script_handle ) ) { | |
return; | |
} | |
} | |
h1_move_script_to_bottom( $script_handles, array( 'move_deps' => true ) ); | |
} | |
// Below is an example how one could use the above helper functions. | |
// add_action call is disabled to prevent copy paste mistakes. | |
/** | |
* Moves Gravity Forms script to bottom. Moves jQuery to bottom as well, | |
* unless Gravity Forms scrips are enqueued. Which means we cannot move jQuery, | |
* because GF assumes it is loaded in header. Stupid Gravity Forms. | |
*/ | |
function h1_move_scripts() { | |
h1_move_script_to_bottom_if_another_not_enqueued( | |
array( | |
'jquery-core', | |
'jquery-migrate' | |
), | |
'gform_gravityforms' | |
); | |
h1_move_script_to_bottom( | |
array( | |
'jquery_json', | |
'gform_placeholder', | |
'gform_gravityforms', | |
'optin-monster-api-script' | |
) | |
); | |
} | |
// add_action( 'wp_enqueue_scripts', 'h1_move_scripts', 99 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment