Hooks
modal_kit_render_button_action
action
Free
PHP
Add attributes to a button that uses your action, such as data for your script or a link for visitors without JavaScript.
Fires when a core Button with a registered action renders, after Modal Kit has added data-modal-kit-action and the runtime’s click handler. The WP_HTML_Tag_Processor is positioned on the button’s .wp-block-button__link element, an <a> or a <button>, and whatever you set on it is kept.
A link without an address has also been given role="button", tabindex="0" and a keyboard handler by then, so it works as a button. Pro uses this action to put step targets, cart products and coupons on its buttons.
Parameters
Parameter
Description
$processor
WP_HTML_Tag_Processor
Positioned on the button’s link element.
$action
string
The button’s action: any registered one except link, including close, copy and other add-ons’ actions.
$block
array
The parsed core/button block: blockName, attrs (with modalKitAction and any attributes your editor code saved), innerHTML and the rest.
Examples
Put your data on the button
myShareTitle is an attribute your editor code adds to core/button. Your script reads it from the button when modal-kit:action fires.
add_action(
'modal_kit_render_button_action',
function ( $processor, $action, $block ) {
if ( 'my-share' !== $action ) {
return;
}
$title = isset( $block['attrs']['myShareTitle'] )
? sanitize_text_field( (string) $block['attrs']['myShareTitle'] )
: '';
$processor->set_attribute( 'data-my-share-title', $title );
},
10,
3
);
Give the button a link for visitors without JavaScript
For an action registered as my-subscribe. With JavaScript the runtime cancels the click, so the address is only followed without it.
add_action( 'modal_kit_render_button_action', function ( $processor, $action ) {
if ( 'my-subscribe' !== $action || 'A' !== $processor->get_tag() ) {
return;
}
$processor->set_attribute( 'href', home_url( '/subscribe/' ) );
$processor->remove_attribute( 'role' );
}, 10, 2 );
Good to know
It fires for every registered action except link, including the free close and copy and other add-ons’ actions: check $action first.
Don’t move the processor with next_tag(): callbacks after yours expect it on the button.
The processor escapes values as it sets them (URL attributes with esc_url()). Sanitise what you read from block attributes.
It fires only for actions registered with modal_kit_button_actions. A button with an unknown action is left as it was saved.
Fired in modal-kit/includes/ButtonActions.php.