WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get and resize Thumbnails with images taken from custom fields (7 posts)

  1. refulez
    Member
    Posted 2 years ago #

    Hi,
    i need to generate thumbnails for a image taken from a custom field.
    how do i do that?
    i need to set thumbnail sizes like this
    "set_post_thumbnail_size( 50, 50 ); // 50 pixels wide by 50 pixels tall, resize mode"
    but for a image taken from a custom field of a post.

    Thanks.

  2. security_man
    Member
    Posted 2 years ago #

    i would just upload it and then paste the url of the thumbnail into the custom field...

  3. refulez
    Member
    Posted 2 years ago #

    i have a script that inserts the link of the image into the custom field. but what i need is: the wordpress thumbnail generator to use that image and generate thumbs of other sizes.

    so i don;t have to use resizing scripts like timthumb.

    so how do i get the wordpress thumbnail generator to use the image in a custom field?

  4. security_man
    Member
    Posted 2 years ago #

    are you uploading the image to your wordpress install?

  5. refulez
    Member
    Posted 2 years ago #

    i'm uploading them to a folder on the server and the i have a script that parses the folder and inserts into the database the link to the image.

    so any idea how to use a custom field for the thumbnail generation?

  6. security_man
    Member
    Posted 2 years ago #

    check out the link below, it has what you are looking for and specifically wp_insert_attachment() is the function you need.

    http://wordpress.stackexchange.com/questions/4307/how-can-i-add-an-image-upload-field-directly-to-a-custom-write-panel/4413#4413

  7. refulez
    Member
    Posted 2 years ago #

    thanks!

    here is the script in case someone else is looking for something like this

    $value is the filename
    
    $save_path = "folder/";
    $file = realpath($save_path).'/'.$value;
    $wp_filetype = wp_check_filetype(basename($file), null );
    
        // Construct the attachment array
        $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'guid' => $file,
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($post_name)),
        'post_content' => '',
        'post_parent' => $post_id,
    'post_status' => 'inherit'
        );
    
        // Save the data
        $id = wp_insert_attachment($attachment, $file, $post_id);
     $attach_data = wp_generate_attachment_metadata( $id, $file );
      wp_update_attachment_metadata( $id, $attach_data );
    set_post_thumbnail( $post_id, $id );

Topic Closed

This topic has been closed to new replies.

About this Topic