SPRING SALE

00000000

30% OFF - Limited Time Deal

JS Events

How it worksHow it works

JS Events are triggered every time change occurs. It can be triggered by an user interaction or reflects a programmatically change. Events are emitted in certain order as described in this documentation.

Events can be catched directly in your grid settings or in your own JS script.

Events in grid settingsEvents in grid settings

All Event examples presented in this documentation can be directly applied in your grid settings under customization tab in the JS field and will be triggered only for this grid. In this case the variable wpgb represents the main instance of the plugin holding all sub instances of the current grid.

JS
Grid settings – JS field
console.dir( wpgb ); // Holds all instances.

// Where 'instanceName' can be: facets, grid, carousel, lightbox.
// Where 'eventName' is the event name attached to an instance.
wpgb.instanceName.on( 'eventName', function( args ) {

	console.log( 'eventName', args );

} );

Events in external scriptEvents in external script

To use your own .js file to listen events from the plugin, you need at first to register your script to Gridbuilder ᵂᴾ thanks to this PHP filter:

PHP
functions.php
function prefix_register_script( $scripts ) {

	$scripts[] = [
		'handle'  => 'my-script',
		'source'  => 'my-url/script.js',
		'version' => '1.0.0',
	];

	return $scripts;

}

add_filter( 'wp_grid_builder/frontend/register_scripts', 'prefix_register_script' );

Now that we have registered our own script we will be able to listen events from the plugin.

Because a page can contain several grids and facets (attached to each grid), we need to listen for each grid/template/content initialization thanks to the global event manager of the plugin stored in the global window object WP_Grid_Builder:

JS
your-script.js
// We listen every time a grid/template/content is initialized.
window.WP_Grid_Builder && WP_Grid_Builder.on( 'init', function( wpgb ) {

    console.dir( wpgb ); // Holds all instances.

    // Where 'instanceName' can be: facets, grid, carousel, lightbox.
    // Where 'eventName' is the event name attached to an instance.
    wpgb.instanceName.on( 'eventName', function( args ) {

        console.log( 'eventName', args );

    } );

} );