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 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(); ?>>
    <?php
    $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(); ?>
</article>

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
add comment

1 Answer 1

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
add comment

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.