How it worksHow it works
JS Methods are actions done by the plugin. Public methods allows to trigger an action programmatically and at any time.
Methods can be set directly in your grid settings or in your own JS script.
Methods in grid settingsMethods in grid settings
All Method examples presented in this documentation can be directly applied in your grid settings under customization tab in the JS field and will be done 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.
console.dir( wpgb ); // Holds all instances.
// Where 'instanceName' can be: facets, grid, carousel, lightbox.
// Where 'methodName' is the method name of the instance.
wpgb.instanceName.methodName();
Methods in external scriptMethods 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:
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 trigger methods 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
:
// 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 'methodName' is the method name of the instance.
wpgb.instanceName.methodName();
} );