Skip to content

Instantly share code, notes, and snippets.

@maykbrito
Created October 21, 2018 20:16
Show Gist options
  • Save maykbrito/bbf0b66dfc93f3bae6ac68a08306ff3c to your computer and use it in GitHub Desktop.
Save maykbrito/bbf0b66dfc93f3bae6ac68a08306ff3c to your computer and use it in GitHub Desktop.
Script to upload images of a local folder to a wordpress server, create a Custom Post Type item, and an Attachment, to add as featured image of that CPT. We do that to help as a bulk operation to add lot of testimonials images for a client.
<?php
class addDepoimentosWithPhotosInFolder {
static $fullpath_dir;
static $files = [];
static $depoimento_section;
static $current_post_id;
static $current_attach_id;
static $current_filename;
static $wp_upload_dir;
static $total_files;
static function init($section) {
echo "\n preparing to add depoimentos to " . $section;
self::$wp_upload_dir = wp_upload_dir();
self::$fullpath_dir = getcwd();
self::$depoimento_section = $section;
self::readFilesInServer();
self::addEachFileToPost();
echo "\nposts added with success!";
}
static function readFilesInServer() {
echo "\n reading all images files...";
foreach (glob("*.{jpg,png,gif,JPG,PNG,GIF,bmp,webm,WEBM,BMP}", GLOB_BRACE) as $file) {
self::$files[] = $file;
}
self::$total_files = count(self::$files);
echo "\n done reading >>>>>>>> " . self::$total_files . " <<<<<<<<<<<< !\n\n";
}
static function addEachFileToPost() {
$couting = 1;
foreach (self::$files as $file) {
echo "\n\n ================================ " . $couting . " of " . self::$total_files;
self::$current_filename = static::$fullpath_dir . '/' . $file;
self::createPost();
self::setPostThumbnailAttachment();
$couting++;
}
}
static function createPost() {
echo "\n creating post...";
$depoimento = array(
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( self::$current_filename ) ),
'post_status' => 'publish',
'post_type' => 'depoimentos'
);
if (self::$depoimento_section)
$depoimento['meta_input'] = array(
'mblog-depoimentos-section' => self::$depoimento_section
);
self::$current_post_id = wp_insert_post( $depoimento );
echo "\n post inserted: ID: " . self::$current_post_id;
}
static function setPostThumbnailAttachment() {
echo "\n preparing to set post Thumbnail Attachment...";
self::createAttachment();
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
self::generateMetadataForAttachmentAndUpdateDatabase();
echo "\n add thumbnail to post...";
set_post_thumbnail( self::$current_post_id, self::$current_attach_id );
}
static function createAttachment() {
$file = self::moveFileToUploadsWpDirectory();
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( self::$current_filename ), null );
// Prepare an array of post data for the attachment.
$attachment = array(
// 'guid' => self::$wp_upload_dir['url'] . '/' . basename( self::$current_filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( self::$current_filename ) ) . '_media',
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
self::$current_attach_id = wp_insert_attachment( $attachment, $file, self::$current_post_id );
echo "\n attachment inserted: ID: " . self::$current_attach_id;
}
static function generateMetadataForAttachmentAndUpdateDatabase() {
echo "\n generating metadata and updating database...";
$attach_data = wp_generate_attachment_metadata( self::$current_attach_id, self::$current_filename );
wp_update_attachment_metadata( self::$current_attach_id, $attach_data );
}
static function moveFileToUploadsWpDirectory() {
$image_data = file_get_contents(self::$current_filename);
$filename = basename(self::$current_filename);
if(wp_mkdir_p(self::$wp_upload_dir['path']))
$file = self::$wp_upload_dir['path'] . '/' . $filename;
else
$file = self::$wp_upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
return $file;
}
}
printf 'zipping files \n\n'
zip -r testimonials.zip testimonials/
printf 'upload files to server \n\n'
scp testimonials.zip myserver:/home/client/www/wp-content/uploads/
scp add_testimonials.php myserver:/home/client/www/wp-content/uploads/
rm -rf testimonials.zip
printf 'login to server \n\n'
ssh myserver "bash -s" < ./server.sh
printf 'all done.'
printf 'going to uploads directory \n\n'
pwd && cd /home/client/www/wp-content/uploads/ && pwd
printf 'removing older folder \n\n'
rm -rf testimonials/
printf 'unzipping files and removing zip \n\n'
unzip testimonials.zip && rm -rf testimonials.zip
printf 'moving script php to folder \n\n'
mv add_testimonials.php testimonials/
printf 'changing perms \n\n'
cd testimonials && chown -R client:client ./
printf 'adding depoimentos to site... \n\n'
wp eval "include('./add_testimonials.php');addDepoimentosWithPhotosInFolder::init('client');" --allow-root
printf '\n service completed.. exiting... \n\n'
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment