Make WordPress Core

Opened 9 years ago

Last modified 7 years ago

#36285 new defect (bug)

Inconsistency between the filters that can be used to override the default shortcode output for images/videos/audio

Reported by: gnotaras's profile gnotaras Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Media Keywords: needs-patch
Focuses: Cc:

Description

Currently, in order to override the default output of the caption, video and audio shortcodes, you have to use the following respectively:

apply_filters( 'img_caption_shortcode', '', $attr, $content );

apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance );

apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance );

Imho, there should be some consistency between these regarding the following:

  • hook name.
  • arguments passed to the filtering function.

Also, it would be very useful if the attachment ID was also passed to the filtering function.

Right now, the attachment ID of images can be obtained from $attr['id'], which gives attachment_IDNUMBER, while the ID of video and audio attachments can only be obtained by using the media file URL found in the $attr array and directly querying the database in order to get the post ID based on the guid field.

Change History (5)

#1 @SergeyBiryukov
9 years ago

  • Component changed from General to Media

#2 @gnotaras
9 years ago

Another point that I think is worth mentioning in this ticket is that the final HTML code of the caption shortcode is not filterable, while the final HTML code of the video and audio shortcodes is. I had filed another ticket (#36270) about it.

#3 in reply to: ↑ description @gnotaras
9 years ago

Replying to gnotaras:

[...] while the ID of video and audio attachments can only be obtained by using the media file URL found in the $attr array and directly querying the database in order to get the post ID based on the guid field.

Regarding the attachment ID in case of audio/video attachments, the following workaround works for me.

This exists in a filtering function attached to the wp_video_shortcode and wp_audio_shortcode hooks.

    // Attachment ID
    // First collect all the audio/video media file URLs in $attachments_urls.
    $attachments_data = get_post_meta( $post->ID, 'enclosure', false );
    $attachments_urls = array();
    foreach ( $attachments_data as $attachment_data ) {
        $parts = preg_split('#\R#u', $attachment_data);
        $attachments_urls[] = $parts[0];
    }
    // Then check which media file URL exists in the $atts array so as to
    // determine which attachment we are currently processing.
    $atts_values = array_values($atts);
    // Find the URL of the attachment we are processing
    $current_attachment_url = '';
    foreach ( $attachments_urls as $attachment_url) {
        if ( in_array($attachment_url, $atts_values) ) {
            $current_attachment_url = $attachment_url;
            break;
        }
    }
    // Now use this URL to directly query the database for the post ID of the
    // attachment with guid = $current_attachment_url
    global $wpdb;
    $res = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $current_attachment_url ));
    $id = $res[0];

Wrote this fast. Simple, but works.

The db query could have been saved if the post meta field also contained the attachment ID. This is the only way I can figure out about how to get the attachment ID at this point.

Update: this code won't work when multiple sources are used.

Version 1, edited 9 years ago by gnotaras (previous) (next) (diff)

This ticket was mentioned in Slack in #core-media by desrosj. View the logs.


7 years ago

#5 @joemcgill
7 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

Hi @gnotaras,

At this point, I don't think we can change the filter name of img_caption_shortcode, but your other suggestions seem sensible to me.

Note: See TracTickets for help on using tickets.