0

I have implemented a front-end form to submit posts. The image upload is handled with the Wordpress Media Library

  1. Users click on "Upload Image"
  2. Media Library opens
  3. Users either select an existing image or upload a new one
  4. The path to the image (URL) gets populated into an <input> field

Problem: My question concerns the featured image: If users choose an existing image from the Media Library, this image get's added to the Media Library again, which I wanted to avoid for two reasons:

a) Save disk space

  • Right now I have one image saved twice:http://localhost/?attachment_id=2523 as well as http://localhost/images/picture.jpg

b) Avoiding duplicates in Media Lirbary

  • Users can access the Media Library from the front-end and I don't want them so see duplicate images

Is it possible to assign one image as a featured image to multiple posts?

Here's what I got:

    // $featuredimg contains the URL to the image: http://mydomain.com/wp-content/uploads...

    $filetype = wp_check_filetype( basename( $featuredimg ), null );
    $wp_upload_dir = wp_upload_dir();

    // Prepare an array of post data for the attachment.
    $attachment = array(    
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $featuredimg ), 
        'post_mime_type' => $filetype['type'],
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $featuredimg ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );


    $thumb_id = wp_insert_attachment( $attachment, $featuredimg,  $pid );

    // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    // Generate the metadata for the attachment, and update the database record.
    $attach_data = wp_generate_attachment_metadata( $thumb_id, $featuredimg );
    wp_update_attachment_metadata( $thumb_id, $attach_data );

    update_post_meta( $pid, '_thumbnail_id', $thumb_id );
0

I'd suggest you check the $featuredimg URL. If there is already an attachment with this URL just get the ID and call

update_post_meta( $pid, '_thumbnail_id', $thumb_id );

There's a useful function to get the ID for an existing URL: Philip Newcomer

function pn_get_attachment_id_from_url( $attachment_url = '' ) {
    global $wpdb;
    $attachment_id = false;

    // If there is no url, return.
    if ( '' == $attachment_url )
        return;

    // Get the upload directory paths
    $upload_dir_paths = wp_upload_dir();

    // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
    if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

        // If this is the URL of an auto-generated thumbnail, get the URL of the original image
        $attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

        // Remove the upload path base directory from the attachment URL
        $attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

        // Finally, run a custom database query to get the attachment ID from the modified attachment URL
        $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

    }

    return $attachment_id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.