Make WordPress Core

Opened 7 weeks ago

Last modified 6 weeks ago

#62752 new feature request

Enhance Audio and Video Shortcodes with Custom Features (Tracklist, Images, Artists, Track Numbers, and Media Handling)

Reported by: pressthemes1's profile pressthemes1 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.7.1
Component: Shortcodes Keywords: 2nd-opinion
Focuses: Cc:

Description

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
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);

Change History (4)

#1 @pressthemes1
7 weeks ago

Here the updated code

<?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);

Last edited 7 weeks ago by pressthemes1 (previous) (diff)

#2 @swissspidy
7 weeks ago

  • Focuses ui accessibility javascript css docs rtl administration template multisite rest-api performance privacy sustainability ui-copy coding-standards php-compatibility removed

#3 @poena
6 weeks ago

Thank you for a thorough description and ticket.
I think that before work progresses, the value of updating the shortcode needs to be considered thoroughly, I believe the current direction of WordPress is to focus on using blocks, not shortcodes.

#4 @poena
6 weeks ago

  • Keywords 2nd-opinion added
Note: See TracTickets for help on using tickets.