wp easy-svg icons import
Copy a folder of SVGs, sanitized and hardened, into a drop-in folder under wp-content/easy-svg-icons that registers as an icon collection on the next load.
WP-CLI command · Easy SVG Pro · Status: Stable
This command takes a folder of SVG files from anywhere on the server and turns it into an icon collection. Every file is cleaned with the site's sanitizer and hardener and then written into wp-content/easy-svg-icons/<slug>/. On the next page load that folder registers as a collection and its icons appear in the block inserter, the pickers and easy_svg_icon().
Usage
wp easy-svg icons import <dir> --collection=<slug> [--label=<text>]
When to use it
- You received a folder of icons from a designer or a client and want it on the site as its own collection, without uploading files one by one.
- A deploy script should put a brand's icons on a server that does not get them from the theme repository.
- You want the icons as files on disk (a drop-in folder), not in the database as an icon set.
Do not use it when:
- You have a pack ZIP with a
manifest.json. Usewp easy-svg pack import, which keeps the manifest's labels and stores an icon set. - The icons belong to a theme you control. Put them into the theme's
assets/iconsfolder and commit them, or register them witheasy_svg_register_icon_collection(). - The server's file system is read only or rebuilt on each deploy. The files would be lost.
Parameters
The source folder. Only *.svg files directly inside it are read, not subfolders.
The collection slug. It is also the name of the folder created under wp-content/easy-svg-icons. The value is turned into a slug (ACME Icons becomes acme-icons). A value that makes no valid slug stops the command.
Documented as the human label, defaulting to the slug. The current code does not read this option. The label comes from a manifest.json in the target folder if there is one, otherwise from the slug with a capital first letter.
Return value
One warning per skipped file (Skipped <path>: <reason>) and a success line: Copied 12 file(s) into /path/wp-content/easy-svg-icons/acme; they register as the "acme" collection on the next load.
0 after the copy, even when files were skipped or none were copied. 1 when the licence or the WP-CLI feature is off, --collection is missing or invalid, the folder cannot be created, or Easy SVG Support is inactive or too old to harden icons.
Default
There is no default collection. --collection is required.
Examples
Import a client's icons
wp easy-svg icons import ~/Downloads/acme-icons --collection=acme
Warning: Skipped /home/me/Downloads/acme-icons/logo-old.svg: not-svg
Success: Copied 47 file(s) into /var/www/wp-content/easy-svg-icons/acme; they register as the "acme" collection on the next load.
Validate first, then import, in a deploy script
#!/usr/bin/env bash
set -euo pipefail
SRC="build/icons"
SLUG="acme"
wp easy-svg icons validate "$SRC" --strict
wp easy-svg icons import "$SRC" --collection="$SLUG"
wp easy-svg icons list
Replace a collection cleanly
The command never deletes files, so an icon removed from the source stays on the site. Clear the folder first when you want an exact copy:
DEST="$(wp eval 'echo WP_CONTENT_DIR;')/easy-svg-icons/acme"
rm -f "$DEST"/*.svg
wp easy-svg icons import ./icons --collection=acme
Defensive: refuse a path that does not exist
SRC="./icons"
if [ ! -d "$SRC" ] || [ -z "$(ls -A "$SRC"/*.svg 2>/dev/null)" ]; then
echo "No SVG files in $SRC, nothing imported." >&2
exit 1
fi
wp easy-svg icons import "$SRC" --collection=acme
How it works
- The guard checks the licence and the WP-CLI feature.
--collectionis turned into a slug and the target folder is created withwp_mkdir_p().- The command needs the sanitizer from Easy SVG Support and
easy_svg_harden_icon_markup(). Nothing is ever written unsanitized. - An
index.phpstub is added so the folder cannot be listed in a browser. - Each
*.svgfrom<dir>gets a slug from its file name, is read, sanitized and hardened, and is written as<slug>.svg. Skipped files are reported with the same reasons aswp easy-svg icons validate, plusunwritablewhen the file could not be written. - The folder's scan cache (a transient) is deleted, so the next request rescans it.
- The success line names the slug the folder will really register under. That is usually the folder name, but a folder already on record keeps the slug it was first registered with, and a
manifest.jsonin the folder can name another slug.
The collection only appears while Theme & folder sources is switched on under Icons → Features.
Pitfalls
--labeldoes nothing. Put amanifest.jsonwith alabelinto the target folder if the collection needs a readable name.- A wrong source path still creates the folder. The target folder and its
index.phpare created before the source is read. A typo in<dir>printsCopied 0 file(s)and exits0, and leaves an empty collection folder behind. - Nothing is ever deleted. Files with the same slug are overwritten, everything else in the target folder stays. Removing an icon from your source does not remove it from the site.
- Files are renamed.
Odd Name.svgis stored asodd-name.svg. Templates use the slug, not the original file name. - No size cap, no manifest. Files are not checked against the 2 MB source limit at import time and a
manifest.jsonin the source folder is not copied. A file over the limit is copied and later reported astoo-large. - Turn on the feature before the first import. The slug a folder gets is recorded the first time Theme & folder sources scans the drop-in directory. On a site where that never happened, existing folders keep the old naming for compatibility and register with a
-xsuffix (acme-x). The success line says so. Switch on Theme & folder sources and load one page before you import, and read the slug from the success line, not from your command. - Exit code 0 with warnings. Skipped files do not fail the command. Run
validate --strictfirst if a partial import should stop your pipeline.
wp-content/easy-svg-icons on the server, and that folder is served by the web server. That is why only sanitized, hardened markup is ever written there. Never copy SVG files into that folder by other means if they did not pass the sanitizer: the folder scan cleans what it registers, but the file on disk is still reachable at its own URL.