Developers

Creating Add-ons for Super Speedy Ajax

Published

An add-on is a separate WordPress plugin that extends Super Speedy Ajax. It gets a switch in the add-ons list, a tab on the Super Speedy Ajax settings page, and a place in the one Save button, without adding a menu or a settings screen of its own.

This guide covers the parts every add-on needs. It is written so you can hand it to an AI coding agent along with the Developer Reference and get a working add-on back. The worked example is the Price Discounts add-on, which was built that way.

Available since 1.9.3.


1. Name the plugin ssap-addon-<something>

The folder and main file must start with ssap-addon-, for example ssap-addon-price-discounts/ssap-addon-price-discounts.php.

The prefix does a job. Fast Ajax, Fast REST and Ultra Ajax answer the per-visitor request with only a chosen set of plugins loaded. Super Speedy Ajax always loads WooCommerce, itself, and every active plugin whose folder starts with ssap-addon-. An add-on with any other name is left out of those requests until the store owner ticks it by hand, and a pricing add-on that is left out shows the wrong price.

Give your own functions, classes, options and hooks a prefix of five or more characters that belongs to your add-on (ssappd_, SSAPPD_). Do not reuse ssap_.

2. Declare the parent in the plugin header

/**
 * Plugin Name: Super Speedy Ajax - Price Discounts
 * Requires Plugins: super-speedy-ajax-prices
 * Requires PHP: 7.4
 * License: GPLv3
 */

Requires Plugins stops WordPress activating the add-on without the parent. It does not check the parent’s version, so check that yourself (step 4).

3. Register with the add-on registry, at file-load time

define( 'SSAPPD_ADDON_SLUG', 'price-discounts' );

add_filter( 'ssap_register_addons', function ( $addons ) {
    $addons[ SSAPPD_ADDON_SLUG ] = array(
        'label'       => 'Price Discounts',   // not translated, see below
        'plugin_file' => __FILE__,
        'version'     => SSAPPD_VERSION,
    );
    return $addons;
} );

Two things go wrong here if you guess.

Call add_filter() when your file loads, not inside a “is the parent active?” check. WordPress loads plugins alphabetically and ssap-addon-... sorts before super-speedy-ajax-prices, so when your file runs the parent has not loaded and SSAP_VERSION does not exist. A check at that point fails on every request and the add-on never registers. add_filter() only queues the callback, so it is safe to call early. The parent collects registrations on plugins_loaded at priority 5.

Do not wrap the label in __(). The filter runs before init, and loading a translation that early raises a “translation loading was triggered too early” notice on WordPress 6.7 and later. It is a product name.

Registering puts a checkbox for your add-on under Advanced > Add-ons. That switch is separate from WordPress’s Activate and Deactivate: it leaves the add-on loaded and its settings untouched, and tells it to do nothing. Store owners use it to find out whether a problem comes from an add-on without uninstalling it.

4. Check the parent on plugins_loaded, then honour the switch

add_action( 'plugins_loaded', function () {
    if ( ! defined( 'SSAP_VERSION' ) || version_compare( SSAP_VERSION, '1.9.3', '<' ) ) {
        // admin notice, then stop
        return;
    }

    if ( is_admin() ) {
        My_Addon_Settings_Tab::init();          // the tab renders even when switched off
    }

    if ( ! ssap_is_addon_active( SSAPPD_ADDON_SLUG ) ) {
        return;                                  // switched off: no hooks that change the shop
    }

    My_Addon_Pricing::init();
} );

ssap_is_addon_active( $slug ) returns true for an add-on the registry has never seen, so a freshly activated add-on works without a second switch.

Keep the settings tab outside the switch. Someone who has switched the add-on off needs to see their settings and switch it back on. Everything that changes what a shopper sees goes after the switch.

5. Add a settings tab

Three hooks, all on the Super Speedy Ajax settings page (admin.php?page=ssap-settings).

add_filter( 'ssap_settings_tabs', function ( $tabs ) {
    $tabs['price-discounts'] = __( 'Discounts', 'ssap-addon-price-discounts' );
    return $tabs;
} );

add_action( 'ssap_settings_panels', function () {
    echo '<div id="ssap-tab-price-discounts" class="ssap-tab-panel">';
    echo '<div class="ssap-panel"><h2 class="ssap-h">Discount rules</h2> ... </div>';
    echo '</div>';
} );

add_action( 'admin_init', function () {
    register_setting( 'ssap_settings', 'ssappd_rules', array(
        'type'              => 'array',
        'sanitize_callback' => array( 'SSAPPD_Rules', 'sanitize' ),
        'default'           => array(),
    ) );
} );
HookTypeWhat it does
ssap_settings_tabsfilterReturn slug => label. Adds a tab button.
ssap_settings_panelsactionPrint your panel. It must be <div id="ssap-tab-{slug}" class="ssap-tab-panel">, with the same slug.
ssap_settings_uninstall_optionsactionPrint one “delete my data on uninstall” checkbox on the Advanced tab, next to the parent’s.

The rules that are not obvious:

  • Your panel is inside the parent’s <form>. Do not print a form or a Save button. One Save writes the parent’s settings and yours.
  • Register your options in the ssap_settings group, with your own option names. WordPress only saves options registered in the group on that request, so the options of a deactivated add-on are never touched by a save.
  • Every checkbox needs a hidden input with the same name and the value no directly before it. WordPress calls update_option( $name, null ) for any registered option that was not posted, and an unticked checkbox posts nothing.
  • Never render a field disabled. A disabled input is not posted, so its option is saved as empty.
  • Every tab’s panel is in the page at once. The tab switcher shows and hides them with CSS. Saving from any tab posts your fields too, and a <template> element is the place for markup that must not be posted.
  • Your sanitise callback must return the same value when given its own output. WordPress runs it twice the first time an option is created.
  • Pass an explicit default to get_option() everywhere you read your option. The default given to register_setting() only exists on requests where settings were registered, which leaves out the front end, REST and WP-CLI.
  • Scope your admin script and CSS to your own panel (#ssap-tab-{slug}) and enqueue them only when the admin_enqueue_scripts hook name contains ssap-settings. The parent’s stylesheet styles form controls on that page, so check your selects and inputs in a browser.

The page’s own classes are available to your panel: ssap-panel (a card), ssap-h (a card heading), ssap-muted (help text) and ssap-plugin-item (a checkbox row).

6. Decide whether your content differs between visitors

This decides how much of Super Speedy Ajax your add-on has to talk to.

The same for every visitor. A sale price that applies to everybody is in this group. Use WooCommerce’s own filters and nothing else. Super Speedy Ajax fetches prices by calling WooCommerce, so the per-visitor request, the cart and the cached page all see your price. You do not need any ssap_* pricing hook.

Different per visitor. A price that depends on the visitor’s country, role or basket is in this group. The cached page is shared, so the page render must not work the visitor out. Do it in the per-visitor request instead:

HookUse it to
ssap_before_prices_data (action, int[] $product_ids)Resolve the visitor (geolocation, role) before any price is read. Fires in every Ajax type, including Ultra.
ssap_user_context_for_request (filter)Add whatever identifies your price group to the context array. WooCommerce’s variation price cache is keyed on it. Leave this out and one visitor’s variation prices are served to another.
ssap_prices_response_data (filter)Add your own data to the per-visitor response, so your add-on does not make a second request.
ssap_register_region( $id, $args )Register a block of per-visitor HTML that is filled into the cached page.

7. Say what happens to cached pages

Super Speedy Ajax has a Prices setting with two values, and your add-on behaves differently under each. Read it with ssap_prices_mode(), which returns visitor or page.

  • visitor (prices fetched per visitor): the cached page holds a placeholder and the price arrives from the per-visitor request. A change your add-on makes to a price is visible on the next page view. Nothing needs purging.
  • page (prices in the cached page): the cached HTML holds the price it was built with. A change is not visible until the page cache is purged.

If your add-on changes prices, tell the store owner which case they are in on your settings tab.

8. Changing a price: use WooCommerce’s filters, and leave edit context alone

If your add-on changes prices, filter them where WooCommerce reads them:

  • woocommerce_product_get_price, woocommerce_product_get_sale_price, woocommerce_product_get_regular_price
  • the same three with product_variation in place of product
  • woocommerce_variation_prices_price, woocommerce_variation_prices_sale_price, woocommerce_variation_prices_regular_price
  • woocommerce_get_variation_prices_hash, adding a string that changes whenever your prices change. WooCommerce caches variation prices in a transient keyed on that hash, so without it a variable product keeps showing the old range.
  • woocommerce_product_is_on_sale, if you set a sale price. WooCommerce also checks the product’s own sale dates, and a product with a sale booked for a future date reports “not on sale” whatever your sale price filter returned.

These filters only run in WooCommerce’s view context. A product save reads in edit context, so a filtered price is never written back to the product.

Country Based Prices converts currency on these filters at priority 5. Use a priority above that so your change applies to the price the visitor sees.

9. Test it on its own site, and make every test fail once

The pattern used by the first-party add-ons:

  1. Unit tests for arithmetic, in plain PHP with no WordPress.
  2. Integration tests run with wp eval-file on a dedicated test site, against real WooCommerce products. Follow each claim with its control: the same read with the feature switched off.
  3. Negative controls. For each behaviour, break that one line of the add-on, run the suite, and require it to fail. A test that has never failed has not proved anything. Price Discounts has 15 of these, and the first run found a filter that no test covered.
  4. One browser journey through the real entry points: save a setting on the settings tab, then follow the result through the shop, the product page, the basket and the checkout as a logged-out visitor.

Run the browser journey with Super Speedy Ajax switched to LIVE. It starts in TEST mode on a first activation, where visitors see the store unchanged.

Checklist for an AI agent

  1. Folder and main file start with ssap-addon-. Own prefix for everything else.
  2. Requires Plugins: super-speedy-ajax-prices in the header.
  3. add_filter( 'ssap_register_addons', ... ) at file-load time, untranslated label.
  4. Parent and version check on plugins_loaded. Settings tab before the ssap_is_addon_active() check, everything else after.
  5. Tab through ssap_settings_tabs and ssap_settings_panels. Options registered in ssap_settings. Hidden no before every checkbox. No disabled fields. No form, no Save button.
  6. Uninstall checkbox through ssap_settings_uninstall_options, and an uninstall.php that obeys it.
  7. Same for every visitor: WooCommerce filters only. Different per visitor: ssap_before_prices_data and ssap_user_context_for_request.
  8. Show a note on the tab when ssap_prices_mode() is page.
  9. Unit tests, integration tests with controls, negative controls, one browser journey in LIVE mode.

Further reading

every constant, filter, action and JavaScript event.
how the cached page, the placeholder and the per-visitor request fit together.
what Normal, Fast and Ultra load, and why the ssap-addon- prefix matters.

Comments