Guide

Admin JavaScript hooks

The configurator editor is a Backbone application too. These hooks cover its views, the save pipeline, global layers, import/export, and the admin 3D preview.

They are all wp.hooks and need a namespace as the second argument. See the hook reference index for the mechanics.

Most of these are for add-ons that extend the editor itself. If you only want to add a field to a layer or a choice, the admin PHP settings filters are simpler and give you storage and translation for free.

Editor views

HookTypeArgumentsPurpose
PC.fe.models.layer.initactionmodelA layer model was created
PC.fe.models.choice.initactionmodelA choice model was created
PC.admin.layer_form.renderactionviewA layer form rendered
PC.admin.layer_form.render.on.change.eventsfiltereventsModel changes that re-render the layer form
PC.admin.choice_form.render.on.change.eventsfiltereventsThe same for the choice form
PC.admin.choiceDetails.initactionviewThe choice details view was created
PC.admin.choiceDetails.renderactionviewIt rendered
PC.admin.choiceDetails.add_angleactionangle, angleView, viewAn angle was added to a choice
PC.admin.choices.editModeChangedactionmodel, viewThe choices edit mode changed
PC.admin.admin_dialog.renderedactionviewA dialog rendered
PC.admin.admin_dialog.openedactionviewIt opened
PC.admin.admin_dialog.closedactionviewIt closed
PC.field.repeater.get_default_optionfilterdefaultsThe blank row added to a repeater field
mkl_pc_get_icon_htmlfilterhtml, id, optionsRendered markup for an editor icon
mkl_pc_admin_nav_iconfilter'', menuIdIcon for an editor nav item
mkl_pc_layer_type_dashiconfiltericon, typeDashicon class for a layer type

Saving

HookTypeArgumentsPurpose
PC.admin.choices.saveactionmodel, layerView, handledA layer's choices are being saved
PC.admin.choices.fetchactionmodel, view, handledThey are being fetched
mkl_pc_admin.save_timeoutfiltertimeoutMilliseconds before a save request times out
mkl_pc_admin_layer_save_batch_maxfilter5Layers per save batch
mkl_pc_admin_content_save_max_choices_per_batchfilter$maxChoices per content batch
mkl_pc_admin_content_save_max_bytes_per_batchfilter524288Bytes per content batch

Global layers

HookTypeArgumentsPurpose
PC.admin.layer.makeGlobalactionmodel, view, handledA layer is being made global
PC.admin.layer.saveGlobalactionlayerModel, nullA global layer is being saved
PC.admin.layer.savedGlobalactionlayerModel, form, responseIt was saved
PC.admin.layer.unlinkGlobalactionmodel, viewA layer was unlinked from its global
PC.admin.global_layer_importedactionnewLayer, globalIdA global layer was imported
mkl_pc_layer_save_global_use_defaultfiltertrue, layerModel, nullWhether the default save routine runs

Import and export

HookTypeArgumentsPurpose
PC.admin.import.process_importactionImportAn import is being processed
PC.fe.import.collectionsfiltercollectionsWhich collections an import covers

Choice selection

HookTypeArgumentsPurpose
PC.choices.canSelectChoicefiltertrue, choice, collection, contextWhether a choice may be selected
PC.choices.canDeselectSimpleChoicefiltercanDeselect, collectionWhether a simple choice may be deselected
PC.choices.comparatorfiltercomparatorHow choices are sorted
PC.fe.choice.changeactionchoiceA choice changed

3D preview

HookTypeArgumentsPurpose
PC.admin.3d_settings.renderactionviewThe 3D settings panel rendered
PC.admin.3d_settings.three_readyactiondepsThree.js and its helpers are loaded
PC.admin.3d_settings.viewer_readyactionview, three, THREEThe admin 3D preview scene is built
PC.admin.show_choice_imagesfiltertrue, dataWhether choice images appear in the editor

PC.fe.models.layer.init and PC.fe.models.choice.init

Fire as each model is constructed, in the editor and on the frontend — the model layer is shared. Extra Price uses the choice hook at priority 30 to compute its display price.

javascript custom.js
wp.hooks.addAction( 'PC.fe.models.choice.init', 'my-plugin/admin', function ( model ) {	if ( ! model.get( 'my_field' ) ) {		model.set( 'my_field', '', { silent: true } );	}} );
PC.fe.models.layer.init and PC.fe.models.choice.init.

Use { silent: true } when seeding defaults, or you will trigger a change event before the view exists.

PC.admin.layer_form.render.on.change.events and PC.admin.choice_form.render.on.change.events

A space-separated list of Backbone change events that cause the form to re-render. If you add a field whose presence depends on another field's value, add that field's change event here or your field will not appear until the panel is reopened.

javascript custom.js
wp.hooks.addFilter(	'PC.admin.choice_form.render.on.change.events',	'my-plugin/admin',	function ( events ) {		return events + ' change:my_field';	});
PC.admin.layer_form.render.on.change.events and PC.admin.choice_form.render.on.change.events.

PC.admin.choices.save and PC.admin.choices.fetch

Both receive a handled callback as their third argument. Calling it tells the editor you have taken over that operation, and the default routine is skipped:

javascript custom.js
wp.hooks.addAction( 'PC.admin.choices.save', 'my-plugin/admin', function ( model, layerView, handled ) {	if ( ! model.get( 'my_external_source' ) ) return;	mySave( model ).then( function () {		handled();	} );} );
PC.admin.choices.save and PC.admin.choices.fetch.

Only take over when you genuinely mean to. Calling handled() and then failing leaves the editor believing the save succeeded.

The batching filters

Large configurators are saved in batches so a single request does not exceed the server's POST limits or its execution time. Three filters tune that, and one sets the timeout:

javascript custom.js
// A slow shared host: smaller batches, longer timeout.wp.hooks.addFilter( 'mkl_pc_admin_layer_save_batch_max', 'my-plugin/admin', function () {	return 2;} );wp.hooks.addFilter( 'mkl_pc_admin.save_timeout', 'my-plugin/admin', function () {	return 60000;} );
The batching filters.

Defaults are 5 layers per batch, 512 KB per content batch, and the Admin save timeout setting (30 seconds). Values must be positive integers — anything else falls back to the default.

Raising the batch sizes makes saving faster on a healthy server and makes it fail on a constrained one. If merchants report saves timing out on large products, lower them rather than raising the timeout.

mkl_pc_layer_save_global_use_default

Return false to stop the editor's own global-layer save from running, leaving the work to your PC.admin.layer.saveGlobal handler.

javascript custom.js
wp.hooks.addFilter(	'mkl_pc_layer_save_global_use_default',	'my-plugin/admin',	function ( useDefault, layerModel ) {		return layerModel.get( 'my_external' ) ? false : useDefault;	});
mkl_pc_layer_save_global_use_default.

PC.fe.import.collections

Which collections a configuration import covers, [ 'layers', 'content', 'angles', 'conditions' ] by default. Add your own key and the importer will walk it, provided the export produced it.

javascript custom.js
wp.hooks.addFilter( 'PC.fe.import.collections', 'my-plugin/admin', function ( collections ) {	collections.push( 'my_collection' );	return collections;} );
PC.fe.import.collections.

See Importing and exporting a configuration for what the format looks like.

PC.choices.comparator

The Backbone comparator used to sort choices, sorting by the order attribute by default.

javascript custom.js
wp.hooks.addFilter( 'PC.choices.comparator', 'my-plugin/admin', function ( comparator ) {	return function ( choice ) {		return choice.get( 'name' ).toLowerCase();	};} );
PC.choices.comparator.

This affects the editor and the frontend. Sorting by anything other than order means shop managers can no longer arrange choices by dragging them.

PC.admin.3d_settings.three_ready and …viewer_ready

The admin 3D preview's equivalents of the frontend runtime hooks. three_ready hands you the loaded modules — THREE, OrbitControls, loadEnvMap, setSceneEnvironment, FakeShadow and the postprocessing creator — before a scene exists. viewer_ready fires once it does, with the view, its internal scene bag and THREE.

javascript custom.js
wp.hooks.addAction( 'PC.admin.3d_settings.viewer_ready', 'my-plugin/admin', function ( view, three, THREE ) {	three.scene.add( new THREE.AxesHelper( 1 ) );} );
PC.admin.3d_settings.three_ready and …viewer_ready.

These mirror, but are not the same as, the frontend's PC.fe.viewer.runtime.ready. The admin preview has no runtime API object — see The 3D runtime API for the frontend equivalent.

mkl_pc_get_icon_html and mkl_pc_admin_nav_icon

Editor icon rendering. mkl_pc_get_icon_html receives the rendered markup, the icon id and the options; mkl_pc_admin_nav_icon supplies the icon for a nav item by menu id.

javascript custom.js
wp.hooks.addFilter( 'mkl_pc_admin_nav_icon', 'my-plugin/admin', function ( icon, menuId ) {	return 'my_tab' === menuId ? '<span class="dashicons dashicons-star-filled"></span>' : icon;} );
mkl_pc_get_icon_html and mkl_pc_admin_nav_icon.

Register SVGs through the PHP filter mkl_pc_admin_icon_registry so they are available by id.