Make WordPress Core


Ignore:
Timestamp:
07/08/2014 05:47:53 PM (11 years ago)
Author:
wonderboymusic
Message:

When setting the poster image for a video shortcode, set that image as the featured image for that attachment (if found) in the background. This AJAX functionality could be used for audio as well.

Introduces attachment_url_to_postid() to attempt to turn URLs into post IDs.

Fixes #27891.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r28976 r29029  
    32193219    }
    32203220}
     3221
     3222/**
     3223 * Try to convert an attachment URL into a post ID.
     3224 *
     3225 * @since 4.0.0
     3226 *
     3227 * @global wpdb $wpdb WordPress database access abstraction object.
     3228 * @param string $url The URL to resolve.
     3229 * @return int The found post_id.
     3230 */
     3231function attachment_url_to_postid( $url ) {
     3232    global $wpdb;
     3233
     3234    $dir = wp_upload_dir();
     3235    $path = ltrim( $url, $dir['baseurl'] . '/' );
     3236
     3237    $sql = $wpdb->prepare(
     3238        "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
     3239        $path
     3240    );
     3241    $post_id = $wpdb->get_var( $sql );
     3242    if ( ! empty( $post_id ) ) {
     3243        return (int) $post_id;
     3244    }
     3245}
Note: See TracChangeset for help on using the changeset viewer.