Developer guide

How the plugins are built, what they store, and the public PHP, CSS and JavaScript surface you can build on.

FreePro

Architecture

Class (Free namespace Siteryn\SiterynVariationSwatches)Role
PluginBoots on plugins_loaded once PHP 7.4, WordPress 6.4 and WooCommerce 8.0 are met, then fires siteryn_vs_loaded
AttributeTypesThe Swatch type field on Products → Attributes and the attribute ID → type map
TermMetaTerm fields, the Swatch column and the term meta for color, image and label
SwatchesDecides an attribute's display type and builds its items
RendererSwatch group and button markup
FrontendFilters woocommerce_dropdown_variation_attribute_options_html (priority 20) and wraps the original select plus the swatch group
AssetsStorefront CSS/JS and the settings-driven CSS variables
Settings, Admin\FieldRegistryStored settings and the field schema shared with Pro
MultilingualWPML/Polylang term meta handling

Pro (SiterynPro\SiterynVariationSwatches) starts on siteryn_vs_loaded and is built only on Free's hooks: AdvancedTypes, Tooltips, StockStates, ArchiveSwatches, Presets, CustomImages and Admin\ProductSwatchesPanel, Responsive, and License. It fires siteryn_vs_pro_loaded when ready.

Storefront markup

<div class="siteryn-vs-attribute siteryn-vs-attribute--color" data-siteryn-vs-attribute>
	<select id="pa_color" name="attribute_pa_color" ...>...</select>  <!-- WooCommerce's own, hidden -->
	<div class="siteryn-vs-swatches siteryn-vs-swatches--color" role="radiogroup" aria-label="Color"
		data-siteryn-vs-swatches data-type="color" data-attribute_name="attribute_pa_color">
		<button type="button" class="siteryn-vs-swatch siteryn-vs-swatch--color is-selected" role="radio"
			aria-checked="true" aria-label="Black" tabindex="0" data-value="black">
			<span class="siteryn-vs-swatch__color" style="--siteryn-vs-swatch-color:#000000"></span>
			<span class="siteryn-vs-swatch__name">Black</span>
		</button>
	</div>
</div>

Image swatches contain img.siteryn-vs-swatch__image; labels span.siteryn-vs-swatch__label. State classes: is-selected, is-unavailable (Free) and is-out-of-stock, has-tooltip (Pro). The selected value is span.siteryn-vs-selected-value inside the row's label. Archive swatches are wrapped in div.siteryn-vs-pro-archive; presets add siteryn-vs-pro-preset-1 to 3 to the group.

Data stored

WhereKeyContents
Optionsiteryn_vs_settingsFree settings
Optionsiteryn_vs_attribute_typesAttribute ID => swatch type (dropdowns are not stored)
Optionsiteryn_vs_versionInstalled Free version
Term metasiteryn_vs_color · siteryn_vs_image_id · siteryn_vs_labelSwatch color, image attachment ID and label (max 40 characters)
Option (Pro)siteryn_vs_pro_settingsEvery Pro tab's settings
Option (Pro)siteryn_vs_pro_attribute_presetsAttribute ID => preset 1 to 3
Option (Pro)siteryn_vs_pro_versionInstalled Pro version
Options (Pro)siteryn_license_variation-swatches_{key,status,update}License key, cached status and update info
Term meta (Pro)siteryn_vs_pro_color_2A two-tone swatch's second color
Product meta (Pro)_siteryn_vs_pro_swatchesPer-product overrides: attribute key => type and items (option => color, image_id)

Uninstalling (deleting) a plugin removes its own rows above, on every site of a multisite network.

AJAX and admin-post endpoints

ActionTypePurpose
siteryn_vs_save_settingsAJAXSaves one settings tab (manage_woocommerce, option group nonce); Pro options are refused without a license
siteryn_vs_preview_cssAJAXReturns Live Preview CSS for unsaved values; read-only
siteryn_vs_reset_settingsadmin-postResets one Free tab to defaults
siteryn_vs_pro_reset_settingsadmin-postResets one Pro tab (refused without a license)
siteryn_vs_pro_license_actionAJAXActivates (do=activate) or deactivates the Pro license

Swatch types, presets, term fields and the product Swatches tab save with WooCommerce's and WordPress's own forms, each with its own nonce.

CSS variables

All settings are printed as custom properties on :root (the Live Preview scopes them to .siteryn-vs-preview). Override them in your theme to restyle everything, or on a class to restyle one group.

PrefixVariables
--siteryn-vs-color-*size, radius, gap, border-width, border-color, selected-color, selected-width, hover-color, hover-transform, justify
--siteryn-vs-image-*size, radius, fit, gap, border-width, border-color, selected-color, selected-width, hover-color, justify
--siteryn-vs-label-*font-size, font-weight, transform, padding-y, padding-x, min-width, border-width, border-color, radius, bg, text, selected-bg, selected-text, selected-border, hover-bg, hover-text, hover-border, gap, justify
--siteryn-vs-row-gapSpacing between attributes
--siteryn-vs-swatch-color · --siteryn-vs-pro-color-2Set inline on each color (and two-tone) swatch
--siteryn-vs-pro-*Pro: dual-angle, radio-size, radio-font-size, radio-gap, radio-color, radio-border, image-label-size, image-label-color, tip-*, overlay-color, overlay-width, archive-justify
:root {
	--siteryn-vs-color-size: 40px;
	--siteryn-vs-label-radius: 999px;
}

JavaScript API

The storefront script (siteryn-vs, needs jQuery) exposes window.siterynVs:

MemberUse
siterynVs.init( container )Initialize swatch groups added after page load. Forms that fire WooCommerce's wc_variation_form event are initialized automatically. Safe to call more than once.
siterynVs.sync( $group )Re-sync one group's selected and unavailable state from its select.
siterynVs.showSelectedValueWhether "Color: Blue" is on (from the settings).

jQuery events, triggered on the [data-siteryn-vs-swatches] group:

EventArgumentsWhen
siteryn_vs_selectedevent, value, $swatchAfter a swatch is picked
siteryn_vs_group_syncedevent, valueAfter the group's state is refreshed from its select
jQuery( document ).on( 'siteryn_vs_selected', '[data-siteryn-vs-swatches]', function ( event, value ) {
	console.log( jQuery( this ).data( 'attribute_name' ), value );
} );

Pro's script (siteryn-vs-pro, config in window.siterynVsPro) listens to these events for stock states and archive swatches. In the admin, Pro dispatches a siteryn_vs_pro_license_changed DOM event (detail.active) on document when the license is activated or deactivated.

Adding a swatch type

add_filter( 'siteryn_vs_swatch_types', function ( $types ) {
	$types['pattern'] = __( 'Pattern', 'my-plugin' );
	return $types;
} );
add_filter( 'siteryn_vs_swatch_type_family', function ( $family, $type ) {
	return 'pattern' === $type ? 'image' : $family;
}, 10, 2 );
add_filter( 'siteryn_vs_swatch_item_html', function ( $inner, $item, $type ) {
	return 'pattern' === $type ? \Siteryn\SiterynVariationSwatches\Renderer::render_base_inner( $item, 'image' ) : $inner;
}, 10, 3 );
add_action( 'siteryn_vs_term_fields', function ( $taxonomy, $type, $term ) {
	if ( 'pattern' === $type ) {
		\Siteryn\SiterynVariationSwatches\TermMeta::render_control( 'image', $term ? $term->term_id : 0 );
	}
}, 10, 3 );
add_action( 'siteryn_vs_save_term_fields', function ( $term_id, $taxonomy, $type ) {
	if ( 'pattern' === $type ) {
		\Siteryn\SiterynVariationSwatches\TermMeta::save_control( 'image', $term_id );
	}
}, 10, 3 );

An unknown type with no markup of its own is drawn as a text label in the label family.

PHP API

To draw swatches outside a variation form (as Pro's archive swatches do), pair Renderer::render_group() with a <select data-attribute_name> inside a [data-siteryn-vs-attribute] wrapper and call siterynVs.init().

Extending the settings page

Add tabs with siteryn_vs_settings_sections and fields with siteryn_vs_settings_fields. A section with its own option_group/option_name saves into your option (register it with register_setting() and reuse Settings::register_section_fields(), render_control(), sanitize_option_input() and reset_option_section()). Field types: checkbox, select, button_group, number, color, text. System Status is added at priority 30, Pro's License at 20. See the settings hooks.