Developer guide
How the plugins are built, what they store, and the public PHP, CSS and JavaScript surface you can build on.
Architecture
Class (Free namespace Siteryn\SiterynVariationSwatches) | Role |
|---|---|
Plugin | Boots on plugins_loaded once PHP 7.4, WordPress 6.4 and WooCommerce 8.0 are met, then fires siteryn_vs_loaded |
AttributeTypes | The Swatch type field on Products → Attributes and the attribute ID → type map |
TermMeta | Term fields, the Swatch column and the term meta for color, image and label |
Swatches | Decides an attribute's display type and builds its items |
Renderer | Swatch group and button markup |
Frontend | Filters woocommerce_dropdown_variation_attribute_options_html (priority 20) and wraps the original select plus the swatch group |
Assets | Storefront CSS/JS and the settings-driven CSS variables |
Settings, Admin\FieldRegistry | Stored settings and the field schema shared with Pro |
Multilingual | WPML/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
| Where | Key | Contents |
|---|---|---|
| Option | siteryn_vs_settings | Free settings |
| Option | siteryn_vs_attribute_types | Attribute ID => swatch type (dropdowns are not stored) |
| Option | siteryn_vs_version | Installed Free version |
| Term meta | siteryn_vs_color · siteryn_vs_image_id · siteryn_vs_label | Swatch color, image attachment ID and label (max 40 characters) |
| Option (Pro) | siteryn_vs_pro_settings | Every Pro tab's settings |
| Option (Pro) | siteryn_vs_pro_attribute_presets | Attribute ID => preset 1 to 3 |
| Option (Pro) | siteryn_vs_pro_version | Installed 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_2 | A two-tone swatch's second color |
| Product meta (Pro) | _siteryn_vs_pro_swatches | Per-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
| Action | Type | Purpose |
|---|---|---|
siteryn_vs_save_settings | AJAX | Saves one settings tab (manage_woocommerce, option group nonce); Pro options are refused without a license |
siteryn_vs_preview_css | AJAX | Returns Live Preview CSS for unsaved values; read-only |
siteryn_vs_reset_settings | admin-post | Resets one Free tab to defaults |
siteryn_vs_pro_reset_settings | admin-post | Resets one Pro tab (refused without a license) |
siteryn_vs_pro_license_action | AJAX | Activates (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.
| Prefix | Variables |
|---|---|
--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-gap | Spacing between attributes |
--siteryn-vs-swatch-color · --siteryn-vs-pro-color-2 | Set 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:
| Member | Use |
|---|---|
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.showSelectedValue | Whether "Color: Blue" is on (from the settings). |
jQuery events, triggered on the [data-siteryn-vs-swatches] group:
| Event | Arguments | When |
|---|---|---|
siteryn_vs_selected | event, value, $swatch | After a swatch is picked |
siteryn_vs_group_synced | event, value | After 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
Settings::get_all(),Settings::get( $key ),Settings::sanitize_field_value(),Settings::sanitize_color_value( $raw, $default )AttributeTypes::get_types(),get_type( $attribute_id ),get_type_for_taxonomy( $taxonomy )TermMeta::get_color(),get_image_id(),get_label(),render_control( $type, $term_id ),save_control( $type, $term_id )Swatches::get_display_type( $attribute, $product ),get_items( $args, $type ),build_item( $item )Renderer::render_group( $items, $type, $context ),get_family( $type ),render_base_inner( $item, $type )Assets::enqueue_assets(),Assets::build_settings_css( $settings, $scope )Multilingual::get_term_meta(),get_original_term_id(),get_default_language()
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.