Created
October 10, 2015 19:12
-
-
Save wp-kitten/99d0902e4579a2421ff6 to your computer and use it in GitHub Desktop.
Dynamically set post featured image to existent posts
This file contains hidden or 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 | |
| /* | |
| * This script will process all images from the specified directory from your server and set them as featured images | |
| * for posts. | |
| * | |
| * 1) Upload all images to wp-content/uploads/DIR_NAME -- Only images should be in that directory! | |
| * 2) Update BHWP_UPLOADS_DIR to match the name of your directory | |
| */ | |
| /* | |
| * Holds the full system path to the images directory | |
| * Replace DIR_NAME with your directory name | |
| */ | |
| define('BHWP_UPLOADS_DIR', ABSPATH.'wp-content/uploads/DIR_NAME'); | |
| /** | |
| * Retrieve all images from the directory | |
| * @return array | |
| */ | |
| function bhwp_getLocalFiles(){ | |
| return glob(BHWP_UPLOADS_DIR.'/*'); | |
| } | |
| /** | |
| * Set the featured image to the specified post | |
| * @param string $localFilePath The system path to the image | |
| * @param int $postID The post ID | |
| * @param bool|false $force Whether or not to replace the existent featured image if the post has one | |
| */ | |
| function bhwp_setPostThumbnail( $localFilePath, $postID, $force = false ) | |
| { | |
| if(! $force && has_post_thumbnail($postID)){ | |
| return; | |
| } | |
| // The ID of the post this attachment is for. | |
| $parent_post_id = $postID; | |
| // Check the type of file. We'll use this as the 'post_mime_type'. | |
| $filetype = wp_check_filetype( basename( $localFilePath ), null ); | |
| // Get the path to the upload directory. | |
| $wp_upload_dir = wp_upload_dir(); | |
| // Prepare an array of post data for the attachment. | |
| $attachment = array( | |
| 'guid' => $wp_upload_dir['url'] . '/' . basename( $localFilePath ), | |
| 'post_mime_type' => $filetype['type'], | |
| 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $localFilePath ) ), | |
| 'post_content' => '', | |
| 'post_status' => 'inherit' | |
| ); | |
| // Insert the attachment. | |
| $attach_id = wp_insert_attachment( $attachment, $localFilePath, $parent_post_id ); | |
| // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. | |
| require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
| // Generate the metadata for the attachment, and update the database record. | |
| $attach_data = wp_generate_attachment_metadata( $attach_id, $localFilePath ); | |
| wp_update_attachment_metadata( $attach_id, $attach_data ); | |
| // Set post's featured image | |
| set_post_thumbnail($postID, $attach_id); | |
| } | |
| /* | |
| * Get all posts | |
| */ | |
| $posts = get_posts(array( | |
| 'post_type' => 'post', | |
| 'numberposts' => -1, // Loop through all posts | |
| )); | |
| /* | |
| * Loop through all posts and set a featured image to each of them | |
| */ | |
| if($posts){ | |
| $images = bhwp_getLocalFiles(); | |
| $ix = 0; | |
| $numImages = count($images); | |
| foreach($posts as $post) | |
| { | |
| setup_postdata($post); | |
| if($ix >= $numImages){ | |
| $ix = 0; | |
| } | |
| $imagePath = $images[$ix]; | |
| bhwp_setPostThumbnail($imagePath, $post->ID); | |
| $ix++; | |
| } | |
| wp_reset_postdata(); | |
| echo 'Done'; | |
| } | |
| else { echo 'no posts found';} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment