WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created multiple post formats, and when i use an audio post format, i want to include some additional javascript for the audio player.

So, i include the content-audio.php like this:

<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'blog/content', get_post_format() ); ?>
<?php endwhile; ?>

This loads the blog/content-audio.php as expected:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    $att_args = array(
        'post_type'      => 'attachment',
        'numberposts'    => 1,
        'post_status'    => null,
        'post_parent'    => $post->ID,
        'post_mime_type' => 'audio',
        'orderby'        => 'menu_order'
    $attachments = get_posts( $att_args );
    if( $attachments ): ?>
        <?php wp_enqueue_script('mediaelement-js'); ?>
        <?php foreach( $attachments as $attachment ): ?>
            <?php $attachmenturl=wp_get_attachment_url($attachment->ID); ?>
            <audio id="player2" src="<?php echo $attachmenturl; ?>" type="audio/mp3" controls="controls"></audio>   
        <?php endforeach; ?>
    <?php endif; ?>
    <?php the_title(); ?>

Can i use the wp_enqueue_script() function inside this file? It's working fine, but i'm curious if this is the right way to do it.

share|improve this question
up vote 4 down vote accepted

As of WordPress 3.4, calls to wp_enqueue_script() can be made inline. So, it is perfectly acceptable to call it where needed, such as inside a shortcode callback, or conditionally based upon returned queries, as you've done here.

share|improve this answer
And it's good practice, why have the overhead of files that aren't needed on a given page? – Stephen Jan 25 '13 at 16:45

Your Answer


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.