Created
August 5, 2021 01:30
-
-
Save freddielore/c47df5d3acbbd397d01b14d07fcf2c5a to your computer and use it in GitHub Desktop.
[smart-seo-featured-image] Using `smart_seo_export_fields` and `smart_seo_import_update` hooks to bulk update featured images by URL with duplicates detection #smartseo #wordpress #wp
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
<?php | |
// add feactured_image column in CSV | |
add_filter( 'smart_seo_export_fields', 'fsl_export_other_custom_fields', 8, 1 ); | |
function fsl_export_other_custom_fields( $fields ){ | |
$fields[] = array( 'featured_image', 'fsl_get_featuredimage' ); | |
return $fields; | |
} | |
function fsl_get_featuredimage( $post ){ | |
if( has_post_thumbnail( $post->ID ) ){ | |
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); | |
return $img[0]; | |
} | |
else{ | |
return ""; | |
} | |
} | |
// import featured_image by URL | |
add_action( 'smart_seo_import_update', 'fsl_import_featured_image_by_url', 10, 2 ); | |
function fsl_import_featured_image_by_url($post_id, $data){ | |
if( !empty( $data['featured_image'] ) ){ | |
// retrieve attachment id if file exists | |
$attach_id = fsl_is_media_exists( $data['featured_image'] ); | |
if( $attach_id ){ | |
set_post_thumbnail( $post_id, $attach_id ); | |
} | |
else{ | |
$insert = fsl_set_featured_image( $data['featured_image'], $post_id ); | |
if( !$insert ){ | |
// failed | |
} | |
} | |
} | |
} | |
function fsl_set_featured_image( $image_url, $post_id ){ | |
// Add Featured Image to Post | |
$image_name = basename( $image_url ); //preg_replace( '/.[^.]*$/', '', basename( $url ) ); | |
$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 | |
return set_post_thumbnail( $post_id, $attach_id ); | |
} | |
function fsl_is_media_exists($url){ | |
$filename = basename($url); | |
global $wpdb; | |
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid LIKE '%/$filename'"; | |
$result = $wpdb->get_row($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid LIKE '%/$filename'")); | |
if( !empty($result) ){ | |
return $result->ID; | |
} | |
else{ | |
$result = $wpdb->get_row($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_title LIKE '%$filename'")); | |
if( !empty($result) ){ | |
return $result->ID; | |
} | |
} | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment