Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function get_attachment_attr_by_url( $image_url ) {
- $uploads = wp_upload_dir();
- $relative_url = str_replace( $uploads['baseurl'] . '/', '', $image_url );
- $query = new WP_Query( array(
- 'post_type' => 'attachment',
- 'post_status' => 'any',
- 'fields' => 'ids',
- 'posts_per_page' => 1,
- 'meta_query' => array(
- array(
- 'key' => '_wp_attached_file',
- 'value' => $relative_url,
- 'compare' => 'LIKE'
- )
- )
- ) );
- $attachment_id = current( $query->posts );
- if ( ! $attachment_id ) {
- return false;
- }
- $attr = array(
- 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
- 'title' => get_the_title( $attachment_id )
- );
- return $attr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement