[wp-trac] [WordPress Trac] #62752: Enhance Audio and Video Shortcodes with Custom Features (Tracklist, Images, Artists, Track Numbers, and Media Handling)

WordPress Trac noreply at wordpress.org
Sun Dec 29 19:29:19 UTC 2024


#62752: Enhance Audio and Video Shortcodes with Custom Features (Tracklist, Images,
Artists, Track Numbers, and Media Handling)
-------------------------------------------------+-------------------------
 Reporter:  pressthemes1                         |      Owner:  (none)
     Type:  feature request                      |     Status:  new
 Priority:  normal                               |  Milestone:  Awaiting
                                                 |  Review
Component:  Shortcodes                           |    Version:  6.7.1
 Severity:  normal                               |   Keywords:
  Focuses:  ui, accessibility, javascript, css,  |
  docs, rtl, administration, template,           |
  multisite, rest-api, performance, privacy,     |
  sustainability, ui-copy, coding-standards,     |
  php-compatibility                              |
-------------------------------------------------+-------------------------
 This feature request aims to enhance the existing WordPress audio and
 video shortcodes by allowing for additional customization options.
 Currently, the audio and video shortcodes in WordPress are basic and only
 support direct source files. I propose the following updates to give users
 more flexibility when embedding media:

 Dynamic Support for Media Types: Automatically support multiple
 audio/video formats (e.g., mp3, wav, ogg for audio; mp4, webm, ogv for
 video) without requiring additional custom attributes.
 Additional Custom Attributes: Include options for:
 Tracklist: Display an optional tracklist for audio files.
 Track Numbers: Option to show track numbers in the tracklist.
 Artists: Option to display artist names.
 Images: Option to display cover art images.
 Style: Allow a style attribute to specify visual presentation (e.g., dark
 or light theme).
 Improved Handling of Multiple Formats: Automatically handle multiple media
 sources (audio and video) to ensure compatibility with various file types
 without additional user input.
 This functionality would streamline media management, especially for
 content creators who use multiple media formats.

 {{{#!php
 <?php
 function sbp_audio_shortcode($output, $atts) {
     // Get default supported audio file types from WordPress
     $default_types = wp_get_audio_extensions();

     // Set default attributes, including 'src' and all media types from
 $default_types
     $defaults_atts = array(
         'src' => '',
         'style' => 'dark',
         'images' => true,
         'artists' => true,
         'tracklist'=> true,
         'tracknumbers' => true,
     );

     // Dynamically add default values for each media type (mp3, wav, ogg,
 etc.)
     foreach ( $default_types as $type ) {
         $defaults_atts[ $type ] = '';  // Initialize each file type as an
 empty string
     }

     // Merge default attributes with user-supplied ones
     $atts = shortcode_atts( $defaults_atts, $atts );

     // Initialize the output container
     $container = '';
     $media_id = 0;

     // First, check if 'src' is set (general media URL)
     if (!empty($atts['src'])) {
         $media_id = attachment_url_to_postid(esc_url($atts['src']));
     } else {
         // Loop through default types (e.g., mp3, wav, ogg) and check if
 any are provided
         foreach ( $default_types as $ext ) {
             if ( !empty($atts[ $ext ]) ) {
                 // Check if the file type is valid
                 $type = wp_check_filetype( $atts[ $ext ],
 wp_get_mime_types() );

                 if ( strtolower( $type['ext'] ) === $ext ) {
                     // Extract the media ID from the source URL
                     $media_id = attachment_url_to_postid(esc_url($atts[
 $ext ]));
                     break;  // Use the first valid file type found
                 }
             }
         }
     }

     // Prepare and display the playlist if a valid media ID was found
     if ($media_id) {
         $playlist_attr = array(
             'type' => 'audio',
             'order' => 'ASC',
             'orderby' => 'post__in',
             'ids' => $media_id,
             'style' => $atts['style'],
             'tracklist'   => false,
             'tracknumbers'=> false,
             'images' => $atts['images'],
             'artists' => $atts['artists'],
         );

         // Generate the playlist using wp_playlist_shortcode
         $container .= wp_playlist_shortcode($playlist_attr);
     } else {
         $container .= '<p>No valid media found for the given
 sources.</p>'; // Fallback message if no media found
     }

     return $container; // Return the playlist or message
 }
 add_filter('wp_audio_shortcode', 'sbp_audio_shortcode', 10, 2);

 // Add the filter to handle video shortcodes
 function sbp_video_shortcode($output, $atts) {
     // Get default supported video file types from WordPress
     $default_types = wp_get_video_extensions();

     // Set default attributes, including 'src' and all media types from
 $default_types
     $defaults_atts = array(
         'src' => '',
         'style' => 'dark',
         'images' => true,
         'artists' => true,
         'tracklist'=> true,
         'tracknumbers' => true,
     );

     // Dynamically add default values for each media type (mp3, wav, ogg,
 etc.)
     foreach ( $default_types as $type ) {
         $defaults_atts[ $type ] = '';  // Initialize each file type as an
 empty string
     }

     // Merge default attributes with user-supplied ones
     $atts = shortcode_atts( $defaults_atts, $atts );

     // Initialize the output container
     $container = '';
     $media_id = 0;

     // First, check if 'src' is set (general media URL)
     if (!empty($atts['src'])) {
         $media_id = attachment_url_to_postid(esc_url($atts['src']));
     } else {
         // Loop through default types (e.g., mp4, wemb, ovg) and check if
 any are provided
         foreach ( $default_types as $ext ) {
             if ( !empty($atts[ $ext ]) ) {
                 // Check if the file type is valid
                 $type = wp_check_filetype( $atts[ $ext ],
 wp_get_mime_types() );

                 if ( strtolower( $type['ext'] ) === $ext ) {
                     // Extract the media ID from the source URL
                     $media_id = attachment_url_to_postid(esc_url($atts[
 $ext ]));
                     break;  // Use the first valid file type found
                 }
             }
         }
     }

     // Prepare and display the playlist if a valid media ID was found
     if ($media_id) {
         $playlist_attr = array(
             'type' => 'video',
             'order' => 'ASC',
             'orderby' => 'post__in',
             'ids' => $media_id,
             'style' => $atts['style'],
             'tracklist'   => false,
             'tracknumbers'=> false,
             'images' => $atts['images'],
             'artists' => $atts['artists'],
         );

         // Generate the playlist using wp_playlist_shortcode
         $container .= wp_playlist_shortcode($playlist_attr);
     } else {
         $container .= '<p>No valid media found for the given
 sources.</p>'; // Fallback message if no media found
     }

     return $container; // Return the playlist or message
 }
 add_filter('wp_video_shortcode', 'sbp_video_shortcode', 10, 2);
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62752>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list