2

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?

7
  • To answer this question, we need to see actual code. It's really not practical to answer in generalities, in this case. Jan 22, 2013 at 19:51
  • I am trying to enqueue hundreds of scripts. Which of them would you like to see the source code for? Jan 22, 2013 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? Jan 22, 2013 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? Jan 22, 2013 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... Jan 22, 2013 at 19:57

1 Answer 1

3

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().

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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