Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to use wp_enqueue_script() for inline scripts?

I'm doing this because my inline script depends on another script and i would like the flexibility of inserting it after it's loaded.

Also, It's an inline script because i'm passing php variables into the javascript (like theme path, etc)

Thanks in advance.

share|improve this question

5 Answers 5

up vote 11 down vote accepted

Well, you have wp_localize_script(), but that's only for passing data.

Otherwise, you can do this:

function print_my_inline_script() {
  if ( wp_script_is( 'some-script-handle', 'done' ) ) {
?>
<script type="text/javascript">
// js code goes here
</script>
<?php
  }
}
add_action( 'wp_footer', 'print_my_inline_script' );

The idea is that you shouldn't rely on your inline script being printed exactly after the script it depends on, but later.

share|improve this answer
    
Surely this only works if the script you're depending on has already been output? If the script you're depending on is also registered in wp_footer, and executes after the inline script code, the inline script simply won't get written to the client, and hence executed? –  Sam Peacey May 31 at 4:19

This solution is similar to @scribu's answer, but it follows the form of wp_enquque_script(), and will place the script in the header if its dependencies are included in the header.

/**
 * 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'));

Note: this uses use of anonymous functions, but for PHP versions prior to 5.3 this can be easily converted to a class.

share|improve this answer

scribu is absolutely correct. Anyway, I want to add some info:

I want my function to output a script once, no matter how often it's called. It is a content-related function, so I cannot wait for any hooks. I used scribus infos and some reading of the WP core to come up with this:

function print_script_once() {
    if(!wp_script_is('my-handle', 'done')) {
        echo "<script>alert('once!');</script>";
        global $wp_scripts;
        $wp_scripts->done[] = 'my-handle';
    }
}
add_action('get_footer', print_script_once);

Using this method I'm able to print an inline script once. In my case I have a function that creates a share-button and needs one script to be executed for all buttons. But only once.

share|improve this answer

Just don't make it an inline script and then pass the dynamic parameters to it as variables. Otto has written a great guide on how to do this effectively.

WordPress 3.3 will also make this more powerful: https://core.trac.wordpress.org/ticket/11520

share|improve this answer

I believe you could put your inline scripts into a inline-scripts.js file within your theme, then call that via wp_enqueue_script. Would that work for your needs? Best of luck!

share|improve this answer
4  
Well then it wouldn't be an inline script. –  chrisjlee Aug 3 '11 at 22:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.