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 TicketTagged: featured image, snippet
-
AuthorPosts
-
April 9, 2019 at 4:10 am #479394
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 );April 9, 2019 at 6:24 am #479417Hi 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.
April 9, 2019 at 2:11 pm #479542This reply has been marked as private.April 9, 2019 at 4:53 pm #479586// 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 );
April 10, 2019 at 7:14 pm #479904Okay then. Anything? Suggestions? Can’t be done? Anything?????
April 11, 2019 at 11:07 am #480064Hello 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,
KiranApril 11, 2019 at 9:25 pm #480214This reply has been marked as private.April 12, 2019 at 4:05 pm #480363Here’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.
DApril 16, 2019 at 8:17 am #481028This reply has been marked as private. -
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket