JavaScript

The slider's front-end script builds each slider with Swiper when it comes within 200px of the screen. A slider far down the page may never be built if nobody scrolls to it, so there is no fixed moment at which every slider exists. Use the events below instead of waiting for page load.

When a slider is ready

Each slider dispatches glidepress-slider:ready from its block root when it builds, with the Swiper instance in detail.swiper. The event bubbles, so one listener on the document hears every slider:

document.addEventListener( 'glidepress-slider:ready', ( event ) => {
	const { swiper, rebuilt } = event.detail;
	swiper.on( 'slideChange', () => {
		// e.g. send an analytics event
	} );
} );
  • Nested sliders fire their own event; tell them apart with event.target.
  • A slider with slides hidden on some devices builds a new instance when the screen crosses a device boundary, and fires again with rebuilt: true. Attach your listeners again to the new instance.
  • A script that loads after a slider has built can find the instance as .swiper.swiper inside any block root with the data-glidepress-slider-ready attribute.

Content loaded after the page

The script scans the page once. If a theme or plugin adds or replaces content later (infinite scroll, AJAX filters, client-side navigation), tell it:

Event Effect
glidepress-slider:refresh Build any slider not built yet, and release any that has left the page
glidepress-slider:destroy Take down the sliders inside the element it is fired on (the document means all)
// After adding content:
document.dispatchEvent( new CustomEvent( 'glidepress-slider:refresh' ) );

// Before replacing a region, if nothing will refresh afterwards:
region.dispatchEvent(
	new CustomEvent( 'glidepress-slider:destroy', { bubbles: true } )
);

Both are safe to fire repeatedly. A refresh already releases sliders that have left the page, so an explicit destroy is only needed when no refresh follows. A slider taken down is left as it was served: Swiper's classes and inline styles removed, hidden slides put back, ready to be built again.

Add-on features

Another plugin can add a feature that loads the way the built-in thumbnails and transitions do: its script is fetched only for sliders that use it.

  1. Register the script's URL under a vendor/name key:

    add_filter( 'glidepress_slider_feature_urls', function ( array $urls ) {
     $urls['acme/ken-burns'] = plugins_url( 'ken-burns.js', __FILE__ ) . '?ver=1.0.0';
     return $urls;
    } );
    
  2. Turn it on for a slider with options under the same key in extensions, using the config filter:

    add_filter( 'glidepress_slider_config', function ( array $config, array $block ) {
     if ( ! empty( $block['attrs']['acmeKenBurns'] ) ) {
         $config['extensions']['acme/ken-burns'] = [ 'zoom' => 1.15 ];
     }
     return $config;
    }, 10, 2 );
    
  3. In the script, leave a setup function under the key. It runs on every build:

    ( window.glidepressSliderFeatures ??= {} )[ 'acme/ken-burns' ] = {
     setup( swiper, { sliderEl, options, cleanups } ) {
         const onChange = () => {
             /* … options.zoom … */
         };
         swiper.on( 'slideChange', onChange );
         // Anything attached outside the instance (window, document) must be undone:
         // cleanups.push( () => window.removeEventListener( … ) );
     },
    };
    

The options arrive as plain JSON (strings, numbers, booleans, arrays and objects), copied fresh with keys like __proto__ dropped, since they travel in post content. A feature can't replace a built-in one, a script that fails to load costs only its own feature, and an error thrown in setup is logged instead of breaking the slider.