How to Bulk upload pictures to wordpress

From vpsget wiki
Jump to: navigation, search

We will show how to bulk upload/ add images into wordpress so you will be able to see it under /uploads.php .

Assuming that you already copied the all files into proper directory like it by default scheme: /var/www/html-dev/wp-content/uploads/<year>/<month>/*.jpg Usually this is usefull if you need to copy images form one wordpress to another, like from developmetn wp to production wp:

for example you have uploaded the folder/flies intor /var/www/html-dev/wp-content/uploads/ /var/www/html-dev/wp-content/uploads/2020/ install wp cli on your server:

cd /opt/
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
cp wp-cli.phar /usr/bin/wp
wp --info
wp cli version
wp cli check-update


You may update it and manipulate. but you need to su - <user> as wordpress/http server user biore for EAMPLE:

 su - dev 
 wp cli update
 wp core version
 wp plugin list
 wp plugin update --all

In most cases you need to add --path="<you_wp_dir> to all commands.

issue the command to import all files recursively in a dir and sub-dirs:

wp media import <full_directory_path>/**\/*.* --path="/var/www/<you_wp_dir>"

or issue the command to import only *.jpg files:

wp media import <full_directory_path>/**\/*.jpg --path="/var/www/<you_wp_dir>"

For example:

wp media import /var/www/html-dev/wp-content/uploads/2020/**\/*.* --path="/var/www/html-dev/"

command will try to add all files under the directory /var/www/html-dev/ recursively.

________

if you got error during importing like: -bash: /bin/wp: Argument list too long try to issue command like:

#xargs: argument line too long
[dev@in3d ~]$ find /var/www/html-dev/wp-content/uploads/2020/08/ -maxdepth 1 -name "*.png" -print0 | xargs "wp media import"
xargs: WARNING: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
#so you will see more error details and will find the way to fix it


OR you may give a trick with think_differ:

 find /var/www/html-dev/wp-content/uploads/2020/08/ -type f -maxdepth 1 -name "*.png" | xargs -n1 wp media import --path=/var/www/html-dev


___ ALso you may import images from URL, like:

wp media import http://s.wordpress.org/style/images/wp-header-logo.png --title='The WordPress logo' --alt="Semantic personal publishing" --path="/var/www/html-dev/"


___

    • If you like to clone media images from another wordpress instance and keep the dates /timestampts you need to copy the exact files with keeping attributes for example:

Issue commands from source wordpress installiation server via SSH:

rsync -avxtHAX --progress -e 'ssh -p2266' /var/www/html-prod/wp-content/uploads/2020/  root@176.9.100.100:/var/www/html-dev/wp-content/uploads/2020/
rsync -avxtHAX --progress -e 'ssh -p8752' /var/www/html-prod/wp-content/uploads/2019/  root@176.9.100.100:/var/www/html-dev/wp-content/uploads/2019/
rsync -avxtHAX --progress -e 'ssh -p8752' /var/www/html-prod/wp-content/uploads/2018/  root@176.9.100.100:/var/www/html-dev/wp-content/uploads/2018/
#etc...

And after that you may import with params on destination wordpress ssh cli:

wp media import wp-content/uploads/2019/09/logo_black4k-min-1.png --title='ndi test' --alt="ndi test" --path="/var/www/html-dev/"  --preserve-filetime --skip-copy
   #--skip-copy in current case will do not copy the file into uploads as we assume it's already in those dir/subdir.
  • Also if you need to import only "original images" in current example we select only fies that do not have resolutions in filenames (means no 100x100 or 1024x780 strings in filesnames):
find /var/www/html-dev/wp-content/uploads/2019/ -type f -name "*.png" ! -name '*x1*'  ! -name '*x2*' ! -name '*x3*' ! -name '*x4*'  ! -name '*x5*' ! -name '*x6*' ! -name '*x7*' ! -name '*x8*' ! -name '*x9*'
# we removed -maxdepth 2 option in case there many directories inside

And now let's combine: import only images without resolutions in filenames and preserving the origianl date/timestamp:

#all PNG: 
find /var/www/html-dev/wp-content/uploads/2019/ -type f -name "*.png" ! -name '*x1*'  ! -name '*x2*' ! -name '*x3*' ! -name '*x4*'  ! -name '*x5*' ! -name '*x6*' ! -name '*x7*' ! -name '*x8*' ! -name '*x9*' | xargs -n1 wp media import --path=/var/www/html-dev --preserve-filetime --skip-copy
#all GIF:
find /var/www/html-dev/wp-content/uploads/2019/ -type f -name "*.gif" ! -name '*x1*'  ! -name '*x2*' ! -name '*x3*' ! -name '*x4*'  ! -name '*x5*' ! -name '*x6*' ! -name '*x7*' ! -name '*x8*' ! -name '*x9*' | xargs -n1 wp media import --path=/var/www/html-dev --preserve-filetime --skip-copy
#all JPG:
find /var/www/html-dev/wp-content/uploads/2019/ -type f -name "*.jpg" ! -name '*x1*'  ! -name '*x2*' ! -name '*x3*' ! -name '*x4*'  ! -name '*x5*' ! -name '*x6*' ! -name '*x7*' ! -name '*x8*' ! -name '*x9*' | xargs -n1 wp media import --path=/var/www/html-dev --preserve-filetime --skip-copy
#all SVG:
find /var/www/html-dev/wp-content/uploads/2019/ -type f -name "*.svg" ! -name '*x1*'  ! -name '*x2*' ! -name '*x3*' ! -name '*x4*'  ! -name '*x5*' ! -name '*x6*' ! -name '*x7*' ! -name '*x8*' ! -name '*x9*' | xargs -n1 wp media import --path=/var/www/html-dev --preserve-filetime --skip-copy