Created
June 7, 2023 03:55
-
-
Save jeff-at-livecanvas/1edd9fad7931ad240e42b91d7c403a39 to your computer and use it in GitHub Desktop.
WordPress: Import Image (and set as Featured image) from an ACF custom field URL: custom_field_name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import Image and set as Featured image from an ACF custom field URL: custom_field_name | |
// Import is done when the post is visited and logged in as admin | |
// so you can process and test one by one | |
function does_file_exists($filename) { | |
global $wpdb; | |
$data = $wpdb->get_results( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%/$filename'" ); | |
foreach ($data as $ID){ | |
$ID = $ID->post_id; | |
if (get_post_type($ID) == "attachment"){ | |
return $ID; | |
} | |
} | |
return null; | |
} | |
add_action("template_redirect",function(){ | |
if(!current_user_can('administrator')) return; | |
if( /*isset($_GET) && isset($_GET['ii']) && */ is_single()) { | |
global $post; | |
set_featured_image ($post -> ID); | |
wp_die(" set_featured_image completed"); | |
} | |
}); | |
function upload_image_from_URL($image_url, $post_id){ | |
// Add Featured Image to Post | |
$image_name = pathinfo($image_url)['basename']; | |
$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 ); | |
return $attach_id; | |
} | |
function set_featured_image($post_id){ | |
if (wp_is_post_revision($post_id)) return; | |
if (has_post_thumbnail($post_id)) return; | |
$image_url = get_field('custom_field_name', $post_id); | |
//wp_die($image_url); | |
if ($image_url){ | |
$attachment_ID = does_file_exists(pathinfo($image_url)['basename']); | |
$attachment_ID = ($attachment_ID ? $attachment_ID : upload_image_from_URL($image_url, $post_id)); | |
if ($attachment_ID){ | |
$message = "Setting attachment ". $attachment_ID ." to post ". $post_id; | |
error_log($message); | |
set_post_thumbnail($post_id, $attachment_ID); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment