easy_svg_pro_set_icons_writing
Run your own code just before an icon set's icons are saved, while the old icons are still stored, for example to keep the previous state or compute what changed.
Action, Easy SVG Pro, Status: Stable
easy_svg_pro_set_icons_written hands you the new state of a set after every save. This action fires one step earlier, right before the write, so the stored meta still holds the old icons. Together they give you before and after for every change, from every path.
Usage
do_action( 'easy_svg_pro_set_icons_writing', int $post_id )
Fired in includes/editor/sets.php, inside the internal function set_icons_save(), after the set lock is taken and before update_post_meta() writes _easy_svg_set_icons.
When to use it
- You want the previous state of a set: a snapshot, an undo copy of your own, or a diff against the new state in
easy_svg_pro_set_icons_written. - You log what changed and need the drawing that is about to be replaced.
Do not use it when:
- You only need the new state. Hook
easy_svg_pro_set_icons_written, which also hands you the icons. - You want to stop or change the save. Nothing you return is used, and the write happens anyway.
Parameters
The ID of the easy_svg_set post whose icons are about to be stored. The new icons are not passed; read them in easy_svg_pro_set_icons_written.
Return value
Nothing. It is an action, so any return value is ignored.
Default
Nothing in Easy SVG Pro listens to it.
Examples
Keep the icons before and after
/**
* Remember the stored icons of a set before every save, then compare.
*/
function acme_before_set_write( $post_id ) {
$raw = get_post_meta( $post_id, '_easy_svg_set_icons', true );
$GLOBALS['acme_set_before'][ (int) $post_id ] = is_string( $raw ) ? (array) json_decode( $raw, true ) : array();
}
add_action( 'easy_svg_pro_set_icons_writing', 'acme_before_set_write' );
function acme_after_set_write( $post_id, $icons ) {
$before = $GLOBALS['acme_set_before'][ (int) $post_id ] ?? array();
$removed = array_keys( array_diff_key( $before, $icons ) );
$added = array_keys( array_diff_key( $icons, $before ) );
error_log( sprintf( '[icons] set %d: +%s -%s', $post_id, implode( ',', $added ), implode( ',', $removed ) ) );
}
add_action( 'easy_svg_pro_set_icons_written', 'acme_after_set_write', 10, 2 );
Defensive: only when Easy SVG Pro is there
add_action(
'plugins_loaded',
function () {
if ( defined( 'EASY_SVG_PRO_FILE' ) ) {
add_action( 'easy_svg_pro_set_icons_writing', 'acme_before_set_write' );
}
}
);
How it works
Every path that changes a set's icons ends in set_icons_save(): the set editor, adding, removing and relabelling icons, bulk editing, moving and copying icons into a set, pack, share link, Font Awesome and preset imports, WP-CLI and the abilities. In order:
- In a write context the set lock is taken (see
easy_svg_pro_set_write_context). - This action fires with the post ID.
- The new icons are stored as one JSON string in
_easy_svg_set_icons. easy_svg_pro_set_icons_writtenfires with the post ID and the new icons.
Pitfalls
- Read the raw meta, not the registry.
get_post_meta()gives you the stored JSON. The collection registry of the request may already be built and does not change here. - The stored value is not hardened again. Easy SVG Pro hardens set markup when it reads a set for a page. Compare or keep what you read here, never print it.
- Direct meta writes skip it. Code that calls
update_post_meta()on_easy_svg_set_iconsitself fires neither action. - It fires even when nothing changes. The save that follows may store the same icons again.