Created
June 5, 2015 07:10
-
-
Save manchumahara/672d5cc5028109fd9e6c to your computer and use it in GitHub Desktop.
wordpress inline js enqueue
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
| //http://wordpress.stackexchange.com/a/139530/6343 | |
| /** | |
| * Enqueue inline Javascript. @see wp_enqueue_script(). | |
| * | |
| * KNOWN BUG: Inline scripts cannot be enqueued before | |
| * any inline scripts it depends on, (unless they are | |
| * placed in header, and the dependant in footer). | |
| * | |
| * @param string $handle Identifying name for script | |
| * @param string $src The JavaScript code | |
| * @param array $deps (optional) Array of script names on which this script depends | |
| * @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body> | |
| * | |
| * @return null | |
| */ | |
| function enqueue_inline_script( $handle, $js, $deps = array(), $in_footer = false ){ | |
| // Callback for printing inline script. | |
| $cb = function()use( $handle, $js ){ | |
| // Ensure script is only included once. | |
| if( wp_script_is( $handle, 'done' ) ) | |
| return; | |
| // Print script & mark it as included. | |
| echo "<script type=\"text/javascript\" id=\"js-$handle\">\n$js\n</script>\n"; | |
| global $wp_scripts; | |
| $wp_scripts->done[] = $handle; | |
| }; | |
| // (`wp_print_scripts` is called in header and footer, but $cb has re-inclusion protection.) | |
| $hook = $in_footer ? 'wp_print_footer_scripts' : 'wp_print_scripts'; | |
| // If no dependencies, simply hook into header or footer. | |
| if( empty($deps)){ | |
| add_action( $hook, $cb ); | |
| return; | |
| } | |
| // Delay printing script until all dependencies have been included. | |
| $cb_maybe = function()use( $deps, $in_footer, $cb, &$cb_maybe ){ | |
| foreach( $deps as &$dep ){ | |
| if( !wp_script_is( $dep, 'done' ) ){ | |
| // Dependencies not included in head, try again in footer. | |
| if( ! $in_footer ){ | |
| add_action( 'wp_print_footer_scripts', $cb_maybe, 11 ); | |
| } | |
| else{ | |
| // Dependencies were not included in `wp_head` or `wp_footer`. | |
| } | |
| return; | |
| } | |
| } | |
| call_user_func( $cb ); | |
| }; | |
| add_action( $hook, $cb_maybe, 0 ); | |
| } | |
| // Usage | |
| enqueue_inline_script('test','alert(\'enqueue inline script test\');',array( 'jquery')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment