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.

I know I can add a script file to the footer of wordpress that requires jquery using this code:

<?php
function my_scripts_method() {
   // register your script location, dependencies and version
   wp_register_script('custom_script',
       get_template_directory_uri() . '/js/custom_script.js',
       array('jquery'),
       '1.0',
       true);
   // enqueue the script
   wp_enqueue_script('custom_script');
}
add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

But what if I want to add just a normal snippet of jquery inline, not a file. How can this be done.

Edit

I know I can use this script to add something to the footer:

<?php
function myscript() {
?>
<script type="text/javascript">
 // This depends on jquery
</script>
<?php
}
add_action('wp_footer', 'myscript');

But how do I specify the script that is used requires jquery to run.

share|improve this question
    
Do you even bother to read around the codex before voting down people's answers? Have you ever read about the in_footer argument to wp_enqueue_script? Read this: codex.wordpress.org/Function_Reference/wp_enqueue_script. It tells you how to use it with the wp_footer action. –  Rutwick Gangurde Nov 6 '11 at 15:41
    
There is no built in function to do this that I know, it is why wp_register_script and wp_enqueue_script exist, but you can check using jquery itself. –  Wyck Nov 6 '11 at 15:42
1  
Sorry for the downvote, but I do not think you understand my question. I have read the codex, and know about the option. But I do not want to wp_enqueue_script a script file into the footer, I want to add a snippet into the footer that requires jquery. I downvoted the answer directly taken from the codex. I read the codex, and did not find an answer, that's why I ask a question. If you redirect me to the codex again I downvote. –  Saif Bechan Nov 6 '11 at 15:46
    
Thanks wyck, I think this is the case, thank you for understanding the question correctly. I think I will just use an external JS script instead than to be sure the script can be run. –  Saif Bechan Nov 6 '11 at 15:47

3 Answers 3

up vote 6 down vote accepted

The easiest way to do this is actually a combination of wp_enqueue_script and the footer actions that Saif and v0idless already referenced. I frequently use jQuery in my themes and plugins, but I put all of the other scripts in the footer as well.

The best way to actually queue things up would be this:

function myscript() {
?>
<script type="text/javascript">
  if ( undefined !== window.jQuery ) {
    // script dependent on jQuery
  }
</script>
<?php
}
add_action( 'wp_footer', 'myscript' );

function myscript_jquery() {
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_head' , 'myscript_jquery' );

But this code snipped assumes that you're the one queueing the script, i.e. you're doing this in your own plugin or theme.

At the moment, there is no way to add an in-line script to the WordPress footer and also load it with dependencies. But there is an alternative, if you're willing.

The alternative

When WordPress works with scripts, it loads them internally to an object to keep track of them. There are essentially 3 lists inside the object:

  • registered
  • queue
  • done
  • to_do

When you first register a script with wp_register_scripts() it's placed in the registered list. When you wp_enqueue_script() the script, it's copied to the queue list. After it's been printed to the page, it's added to the done list.

So, rather than queueing your footer script to require jQuery, you can document that it needs to use jQuery and just check programatically to make sure jQuery is loaded.

This uses wp_script_is() to check where the script is listed.

function myscript() {
    if( wp_script_is( 'jquery', 'done' ) ) {
    ?>
    <script type="text/javascript">
      // script dependent on jQuery
    </script>
    <?php
    }
}
add_action( 'wp_footer', 'myscript' );

You might be able to use this same code to force jQuery to load in the footer as well, but I haven't tested that ... so your mileage my vary.

share|improve this answer
    
Nice, this is what I was looking for. Accepted answer. –  Saif Bechan Nov 7 '11 at 11:16

You can use the wp_footer action for doing this. Check the codex for wp_footer.

share|improve this answer
    
I know, but how can you specify that the script you add to the footer is depending on jquery. Check my edit to be more spicific. –  Saif Bechan Nov 6 '11 at 14:14
    
Do you even bother to read around the codex before voting down people's answers? Have you ever read about the in_footer argument to wp_enqueue_script? Read this: codex.wordpress.org/Function_Reference/wp_enqueue_script. It tells you how to use it with the wp_footer action. –  Rutwick Gangurde Nov 6 '11 at 15:42
    
Yes I read the codex, and this is not an answer to my question. Therefor the downvote. I can check the codex myself. I do not think the thing I want can be done. –  Saif Bechan Nov 6 '11 at 15:52

Use the wp_footer action like this:

add_action( 'wp_footer', 'wpse33008');
function wpse33008() {
?>
<script type="text/javascript">
    /** INSERT SCRIPT HERE **/
</script>
<?php
}
share|improve this answer
    
This is not a good answer. I asked how I could specify the script required jQuery. If the user does not have jquery running your implementation will not run. –  Saif Bechan Nov 6 '11 at 14:18

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.