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.

My customer has many pages with <script> elements including javascript.

<script src="myscriptforthispageonly.js"></script>

I'm trying to get all scripts to load in the footer using wp_enqueue_script(), but I'm not sure how to enqueue a script from within a page.

Again, this is within a Wordpress page. Is it possible to enqueue an external script from a page or post?

If that's not possible, is it possible to delay loading the script until the onload event fires?

share|improve this question
    
To answer this question, we need to see actual code. It's really not practical to answer in generalities, in this case. –  Chip Bennett Jan 22 '13 at 19:51
    
I am trying to enqueue hundreds of scripts. Which of them would you like to see the source code for? –  brentonstrine Jan 22 '13 at 19:53
    
We need some sort of code to have any idea of context. Perhaps post the template file, with a representative example of a script call? –  Chip Bennett Jan 22 '13 at 19:55
    
This is from within a page. As in a Wordpress page. No templates. Imagine you are writing a Wordpress blog post update, you switch to HTML view, and include a script with <script src="something"></script>. How do I enqueue that? –  brentonstrine Jan 22 '13 at 19:56
    
How are you adding script calls in post_content? More importantly: why? There's almost certainly a better way to skin that cat... –  Chip Bennett Jan 22 '13 at 19:57

1 Answer 1

General answer: you can call wp_enqueue_script() directly inline in the template, as of WordPress 3.4 (IIRC).

So, if you have:

<script src="myscriptforthispageonly.js"></script>

You could replace it with:

<?php wp_enqueue_script( 'this-page-script', get_template_directory_uri() . '/myscriptforthispageonly.js', array( 'jquery' ), null, true ); ?>

Edit

From this comment:

This is from within a page. As in a Wordpress page. No templates. Imagine you are writing a Wordpress blog post update, you switch to HTML vie

Your best course of action would be to define a shortcode for the user to put into the post content, instead of putting a <script> call itself directly in the post content. Then, in your shortcode callback, you can call wp_enqueue_script().

share|improve this answer
    
+1 Shortcode seems the best way to go here. –  akTed Jan 23 '13 at 0:25

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.