[wp-trac] [WordPress Trac] #36285: Inconsistency between the filters that can be used to override the default shortcode output for images/videos/audio
WordPress Trac
noreply at wordpress.org
Tue Mar 22 15:09:53 UTC 2016
#36285: Inconsistency between the filters that can be used to override the default
shortcode output for images/videos/audio
--------------------------+------------------------------
Reporter: gnotaras | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version:
Severity: normal | Resolution:
Keywords: | Focuses:
--------------------------+------------------------------
Comment (by gnotaras):
Replying to [ticket:36285 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.
Feedback is welcome.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/36285#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list