What is the hook to add featured images?

This topic contains 8 replies, has 3 voices, and was last updated by  Kiran 5 years, 7 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #479394

    Derek Brown
    Expired Member
    Post count: 28

    First of all, your update completely, and I mean completely trashed our site. We are so incredibly at your lack of consideration and our loss of at least 50 man hours that went into setting up our directory that we looked into calling in the lawyers. And from what we can tell, the benefits are few to none.

    Moving on.

    We have been able to figure out the hooks to retrieve post information and to update information for all but one critical area – the featured image.

    The action which works on a normal WP post has no effect on your custom post type. I have enclosed the code which works on a normal post here.
    Can you please direct us in the right direction to allow the attachment of a featured image to your listings post type.

    // Add Featured Image to Post
    $image_url = $featured_image; // Define the image URL here
    $image_name = ‘compressor.jpg’;
    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents($image_url); // Get image data
    $unique_file_name = wp_unique_filename( $upload_dir[‘path’], $image_name ); // Generate unique name
    $filename = basename( $unique_file_name ); // Create image file name

    // Check folder permission and define file location
    if( wp_mkdir_p( $upload_dir[‘path’] ) ) {
    $file = $upload_dir[‘path’] . ‘/’ . $filename;
    } else {
    $file = $upload_dir[‘basedir’] . ‘/’ . $filename;
    }

    // Create the image file on the server
    file_put_contents( $file, $image_data );

    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );

    // Set attachment data
    $attachment = array(
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => sanitize_file_name( $filename ),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’
    );

    // Create the attachment
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );

    // Include image.php
    require_once(ABSPATH . ‘wp-admin/includes/image.php’);

    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );

    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );

    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
    // Otherwise, we’ll stop
    } else {

    // Arbitrarily use -2 to indicate that the page with the title already exists
    $post_id = -2;

    } // end if
    do_action( ‘save_post’, $post_id, $post_type, true );

    #479417

    Kor
    Moderator
    Post count: 16516

    Hi Derek Brown,

    Thanks for your feedback and I’m sorry to hear about the trouble. I will forward this to a developer for a second look.

    #479542

    Kiran
    Moderator
    Post count: 7069
    This reply has been marked as private.
    #479586

    Derek Brown
    Expired Member
    Post count: 28
    // Add Featured Image to Post
    $image_url = $featured_image; // Define the image URL here
    $image_name = ‘compressor.jpg’;
    $upload_dir = wp_upload_dir(); // Set upload folder
    $image_data = file_get_contents($image_url); // Get image data
    $unique_file_name = wp_unique_filename( $upload_dir[‘path’], $image_name ); // Generate unique name
    $filename = basename( $unique_file_name ); // Create image file name
    
    // Check folder permission and define file location
    if( wp_mkdir_p( $upload_dir[‘path’] ) ) {
    $file = $upload_dir[‘path’] . ‘/’ . $filename;
    } else {
    $file = $upload_dir[‘basedir’] . ‘/’ . $filename;
    }
    
    // Create the image file on the server
    file_put_contents( $file, $image_data );
    
    // Check image file type
    $wp_filetype = wp_check_filetype( $filename, null );
    
    // Set attachment data
    $attachment = array(
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => sanitize_file_name( $filename ),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’
    );
    
    // Create the attachment
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    
    // Include image.php
    require_once(ABSPATH . ‘wp-admin/includes/image.php’);
    
    // Define attachment metadata
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    
    // Assign metadata to attachment
    wp_update_attachment_metadata( $attach_id, $attach_data );
    
    // And finally assign featured image to post
    set_post_thumbnail( $post_id, $attach_id );
    // Otherwise, we’ll stop
    } else {
    
    // Arbitrarily use -2 to indicate that the page with the title already exists
    $post_id = -2;
    
    } // end if
    do_action( ‘save_post’, $post_id, $post_type, true );
    #479904

    Derek Brown
    Expired Member
    Post count: 28

    Okay then. Anything? Suggestions? Can’t be done? Anything?????

    #480064

    Kiran
    Moderator
    Post count: 7069

    Hello Derek,

    Try following code to upload image under the listing.

    
    
    if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
    	include_once( ABSPATH . 'wp-admin/includes/image.php' );
    }
    if ( ! function_exists( 'media_buttons' ) ) {
    	include( ABSPATH . 'wp-admin/includes/media.php' );
    }
    $wp_upload_dir = wp_upload_dir();
    
    $post_id 		= 542;
    $field_name 	= 'post_images';
    $image_url 		= 'https://mysite.com/wp-content/uploads/my-image.jpg';
    $title 			= 'Image Title';
    $caption 		= 'image-caption';
    $order 			= 0; // 0 for featured image & 1 for normal attachment
    
    $attachment = GeoDir_Media::insert_attachment( $post_id, $field_name, $image_url, $title, $caption, $order );
    
    if ( is_wp_error( $attachment ) ) {
    	$error = $attachment->get_error_message();
    } else {
    	geodir_save_post_meta( $post_id, 'featured_image', $attachment['file'] );
    
    	$image = $wp_upload_dir['baseurl'] . $attachment['file'];
    }

    Regards,
    Kiran

    #480214

    Derek Brown
    Expired Member
    Post count: 28
    This reply has been marked as private.
    #480363

    Derek Brown
    Expired Member
    Post count: 28

    Here’s what is happening, and I can not for the life of me figure out why. The image is being uploaded to /2017/11/imagefile.jpg, but the entry going into the database is 2019/04 !?
    I would think that 2019/04 would be correct as images are usually updated to a folder that corresponds to the year/month
    So, why is your script uploading the image, all images, to 2017/11 ??
    Any assistance suggestions would be very much appreciated.
    Thanks.
    D

    #481028

    Kiran
    Moderator
    Post count: 7069
    This reply has been marked as private.
Viewing 9 posts - 1 through 9 (of 9 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount