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('');
}
?>
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('' . $medias . '');
?>
Now obviously this always returns two players no matter what, I need to make this a single query that either executes the shortcode if only a single audio is available and 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 Answers
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 = "";
if( count( $media ) > 1 )
{
$ids = join( ',', wp_list_pluck( $media, 'ID' ) );
$sc = "";
}
echo do_shortcode( $sc );
}
$post = $tmp; // restore the right global $post object
where we use that the stripped 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 = "";
if( count( $media ) > 1 )
$sc="";
echo do_shortcode( $sc );
}
$post = $tmp;
where the stripped 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;
});