3

My reason for doing this is for a music & entertainment site where users will have the ability to share music and videos on other websites via embed code. I'm using the audio.php and video.php attachment pages to load the default MediaElement.js HTML5 Player with all the attached media from it's parent post. I then link to that attachment page via Iframe, thus resulting in a embed code for each audio and video post.

I'm sure a plugin would be much better solution but this is what I can do with my current skill level.

This is what I've gotten thus far.

For the playlist:

<?php
 global $post; 
    $args = array(
    'post_parent' => $post->post_parent, 
    'numberposts' => -1, 
    'post_status' => 'null', 
    'post_type' => 'attachment', 
    'post_mime_type' => array('audio'), 
    'order' => 'ASC' ); 

    $attachments = get_posts($args);
    $medias = $attachments;

    if (count($medias) > 0) {
        $array = array();
        foreach($medias as $media) {
            $array[] = $media->ID;
        }

        $id_comma_separated = implode(',', $array);
        echo do_shortcode('[playlist ids="' . $id_comma_separated . '"]');
    }
?>

And for single audio.

<?php
 global $post; 
    $args = array(
    'post_parent' => $post->post_parent, 
    'numberposts' => 1, 
    'post_status' => 'null', 
    'post_type' => 'attachment', 
    'post_mime_type' => array('audio'), 
    'order' => 'ASC' ); 

    $attachments = get_posts($args);
    $medias = $attachments;

    $medias = wp_get_attachment_url( $attachment_id, 'full' );
    echo do_shortcode('[audio src="' . $medias . '"]');
?>

Now obviously this always returns two players no matter what, I need to make this a single query that either executes the [audio] shortcode if only a single audio is available and [playlist] if there is more than one audio attachment in the parent post.

This is as far as I've gotten, everything I tried after this broke the audio attachment page.

I'm a designer not a developer so my PHP skills are very limited, so if there's a better way to do this whole thing, that would be great.

Thanks.

2

Some ideas for your attachments file:

A simplification:

$tmp   = $post;                          // Store the current global post object
$post  = get_post( $post->post_parent ); // Use the post parent post object
$media = get_attached_media( 'audio' );
if( $media )
{
    $sc = "[audio]";
    if( count( $media ) > 1 )
    {
        $ids = join( ',', wp_list_pluck( $media, 'ID' ) );
        $sc = "[playlist ids=\"{$ids}\"]";      
    }
    echo do_shortcode( $sc );
}
$post = $tmp;                            // restore the right global $post object

where we use that the stripped [audio] shortcode, plays the current post's attached audio file.

We can simplify it more:

$tmp  = $post;  
$post = get_post( $post->post_parent ); 
if( $media = get_attached_media( 'audio' ) )
{
    $sc = "[audio]";
    if( count( $media ) > 1 )
        $sc = '[playlist]';
    echo do_shortcode( $sc );
}
$post = $tmp; 

where the stripped [playlist] shortcode, plays the current post's attached audio files.

Even shorter version:

$tmp  = $post;  
$post = get_post( $post->post_parent );
if( $media = get_attached_media( 'audio' ) )            
    echo count( $media ) > 1 ? wp_playlist_shortcode() : wp_audio_shortcode();
$post = $tmp; 

where we use the audio and playlist shortcode callbacks directly.

... and finally:

$tmp  = $post;  
$post = get_post( $post->post_parent ); 
echo count( get_attached_media( 'audio' ) ) > 1 ? wp_playlist_shortcode() : wp_audio_shortcode();
$post = $tmp; 

Notice that we assume that we are in the loop.

Modify the attachment's page through a filter:

If you want to append the audio/playlist player to the attachment's content, you can try for example:

/**
 * Append the audio/playlist from the parent post, to the attachment's content.
 *
 * @see http://wordpress.stackexchange.com/a/176600/26350
 */

add_filter( 'the_content', function( $content )
{
    if( is_attachment() )
    {
        global $post;
        $tmp = $post;
        $post = get_post( $post->post_parent );
        $content .= count( get_attached_media( 'audio' ) ) > 1 ? wp_playlist_shortcode() : wp_audio_shortcode();
        $post = $tmp;
    }
    return $content;
});            
0

OK so I figured it out, I'm sure they're much more elegant ways of doing this but it works for what I needed to do.

    <?php
        // Single Audio & Playlist player   
     global $post; 
        $args = array(
        'post_parent' => $post->post_parent, 
        'numberposts' => -1, 
        'post_status' => 'null', 
        'post_type' => 'attachment', 
        'post_mime_type' => array('audio', 'video'), 
        'order' => 'ASC' ); 

        $medias = get_posts($args);

        // If the available audio attachments are less than or equal to 1   
        if (count($medias) <= 1) {

            $medias = wp_get_attachment_url( $attachment_id, 'full' );
            echo do_shortcode('[audio src="' . $medias . '"]'); 

            // Get the artwork for single audio
            $attachment_id = $attachment->ID;
            $attachment_page = get_attachment_link( $attachment_id );   
            echo wp_get_attachment_image( $attachment_id );

            // The parent post link and title
            $parentTitle = get_the_title($post->post_parent);
            echo '<div class="parent-post-name">' . $parentTitle . '</div>';                        
            echo '<a class="parent-post-link" href=" ' . get_permalink($post->post_parent) . ' "> ' . $parentTitle . ' </a>';

        }

        // If the available audio attachments are more than or equal to 2
        elseif (count($medias) >= 2) {
            $array = array();
            foreach($medias as $media) {
                $array[] = $media->ID;
            }

            $id_comma_separated = implode(',', $array);
            echo do_shortcode('[playlist ids="' . $id_comma_separated . '"]');  
        } 

    ?> 

Hopefully this is helpful to somebody, I echoed a few things that I needed for the single audio but not the playlist which is the conditional statement I needed the most to style each player a certain way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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