[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:45:05 UTC 2024


#62752: Enhance Audio and Video Shortcodes with Custom Features (Tracklist, Images,
Artists, Track Numbers, and Media Handling)
-------------------------+-------------------------------------------------
 Reporter:               |       Owner:  (none)
  pressthemes1           |
     Type:  feature      |      Status:  new
  request                |
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Shortcodes   |     Version:  6.7.1
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:  ui, accessibility, javascript,
                         |  css, docs, rtl, administration, template,
                         |  multisite, rest-api, performance, privacy,
                         |  sustainability, ui-copy, coding-standards, php-
                         |  compatibility
-------------------------+-------------------------------------------------

Comment (by pressthemes1):

 here the updated code {{{#!php
 <?php
 function wps_media_shortcode($output, $atts, $media_type) {
     // Get default supported file types from WordPress
     $default_types = ($media_type === 'audio') ? wp_get_audio_extensions()
 : wp_get_video_extensions();

     // Set default attributes
     $defaults_atts = array(
         'src' => '',
         'style' => 'dark',
         'images' => true,
         'artists' => true,
         'tracklist' => false,
         'tracknumbers' => false,
     );

     // Dynamically add default values for each media type
     foreach ($default_types as $type) {
         $defaults_atts[$type] = '';
     }

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

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

     // Check 'src' or loop through default types
     if (!empty($atts['src'])) {
         $media_id = attachment_url_to_postid(esc_url($atts['src']));
     } else {
         foreach ($default_types as $ext) {
             if (!empty($atts[$ext])) {
                 $type = wp_check_filetype($atts[$ext],
 wp_get_mime_types());
                 if (strtolower($type['ext']) === $ext) {
                     $media_id =
 attachment_url_to_postid(esc_url($atts[$ext]));
                     break;
                 }
             }
         }
     }

     // Generate output
     if ($media_id) {
         $playlist_attr = array(
             'type' => $media_type,
             'order' => 'ASC',
             'orderby' => 'post__in',
             'ids' => $media_id,
             'style' => $atts['style'],
             'tracklist' => $atts['tracklist'],
             'tracknumbers' => $atts['tracknumbers'],
             'images' => $atts['images'],
             'artists' => $atts['artists'],
         );
         $container .= wp_playlist_shortcode($playlist_attr);
     } else {
         $container .= '<p>No valid ' . esc_html($media_type) . ' media
 found.</p>';
     }

     return $container;
 }

 // Register the filters
 add_filter('wp_audio_shortcode', function($output, $atts) {
     return wps_media_shortcode($output, $atts, 'audio');
 }, 10, 2);

 add_filter('wp_video_shortcode', function($output, $atts) {
     return wps_media_shortcode($output, $atts, 'video');
 }, 10, 2);

 }}}

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


More information about the wp-trac mailing list