Method_Modals
Overview
Singleton registry that collects modals from any source while the page renders and prints them once, directly under <body>, on wp_footer.
Description
A Bootstrap modal printed where its block sits ends up inside the Section block’s z-indexed stacking context, where the body-level backdrop paints over it. So a modal’s content is rendered where it is declared — early enough for the content’s own asset enqueues — handed to this registry, and the markup is printed in the footer. The registry does not care where a modal comes from: the Modal block registers from its render callback, so a block in post content, a template part or a synced pattern all work the same way, and any other source calls method_register_modal() or hooks method/register_modals. The system is described end to end on the Modals page; this page is the class reference.
The instance is created when the file loads (Method_Modals::instance() at the end of lib/class-method-modals.php, required by functions.php) and wires its own hooks: init 9 → register_script(); wp_enqueue_scripts 5 → collect(); wp_footer 5 → print_modals(); wp_footer 19 → print_late_modals(). It also attaches the usual content filters to method/modal_content, because hoisted content never reaches the the_content filters that run after do_blocks(): wptexturize, shortcode_unautop, wp_replace_insecure_home_url, do_shortcode (11), wp_filter_content_tags (12), convert_smilies (20).
Source
File: lib/class-method-modals.php
Constants
| Constant | Value | Description |
|---|---|---|
SCRIPT_HANDLE | 'method-modal-view-script' | Handle of the trigger script (lib/blocks/utils/modals.js, built into the Modal block’s view script). Registered on init 9 so core reuses it for the block’s viewScript. |
DEFAULT_CLOSE_ICON | SVG | The stroke × used when a modal names no close icon. |
Properties
| Property | Visibility | Type | Description |
|---|---|---|---|
$instance | private static | ?self | Singleton instance. |
$modals | private | array | Registered modals keyed by id, in registration order. |
$printed | private | array | Ids already printed, so the second footer pass skips them. |
$context | private | array | Stack of scope / source defaults from push_context(). |
$asset_snapshots | private | array | Stack of asset queues from snapshot_assets(). |
$closed | private | bool | True once the last footer pass has run; registrations after that are refused. |
Methods
instance(): self (static)
Returns the singleton, creating it on first call.
register( array $args ): string|false
Registers a modal. Applies method/modal_args (return false to skip), sanitises the id, drops a duplicate id (firing method/modal_duplicate and still returning the id), refuses once closed (_doing_it_wrong()), filters the content through method/modal_content, collects the modal’s CSS under #<id>.method-modal and the shared full-screen rule for its tier, enqueues the trigger script, stores the modal and fires method/modal_registered. See method_register_modal() for the argument list.
is_registered( string $id ): bool
Whether an id is taken. The Modal block checks it before registering, so a synced pattern used twice renders its content once.
all( ?string $scope = null ): array
| Param | Type | Description |
|---|---|---|
$scope | string|null | Limit to one scope (post, site, …); null for every modal. |
Registered modals keyed by id.
push_context( array $context ): void / pop_context(): void
Set, and unset, the scope and source given to modals registered in between, unless a modal passes its own. For sources that register by running stored block content through do_blocks() inside method/register_modals.
snapshot_assets(): void / assets_since_snapshot(): array
Remember which styles, scripts and script modules are enqueued now, and later return what was enqueued since, as styles, scripts and modules — pair every snapshot with one read. The Modal block takes a snapshot on render_block_data and reads it at the top of its render callback, so the registry can enqueue again what the content enqueued, since WordPress 6.9 dequeues the assets of a block that prints nothing. The script-module queue is read through wp_script_modules()->get_queue() on WordPress 6.9+ and through the per-module enqueue flags on 6.5–6.8 (get_script_module_queue(), private).
collect(): void
Fires method/register_modals with the registry, then enqueue_assets(). Hooked to wp_enqueue_scripts at priority 5: block themes render the template before wp_head, so by then every modal declared in the template or post content is registered and wins an id collision with anything added here.
enqueue_assets(): void
Enqueues the trigger script and every registered modal’s recorded assets. Nothing is enqueued on a page without modals. Called again from the footer pass for modals registered late.
register_script(): void
Registers SCRIPT_HANDLE from the Modal block’s build, deferred, in the footer, depending on the build’s own dependencies plus whatever method/modal_script_dependencies returns (default the method bundle, which provides window.bootstrap.Modal). Does nothing when the build is missing.
default_options(): array (static)
size default, fullscreen_below '', centered, scrollable, static_backdrop, hide_title all false, title_tag h2, close_icon '', close_label “Close”.
get_fullscreen_css( string $tier ): string (static)
| Param | Type | Description |
|---|---|---|
$tier | string | mobile or tablet; anything else returns ''. |
The “full screen below” rule for a tier: a max-width media query at the tier’s maximum from method_get_block_breakpoints(), making .modal-dialog.method-modal-fullscreen-<tier> fill the viewport with no border or radius. Collected once per tier however many modals use it.
get_markup( array $modal ): string
Builds the modal element — .modal.fade.method-modal with the size’s dialog class, centered / scrollable / full-screen-below classes, --method-modal-width, aria-labelledby or aria-label, data-bs-backdrop="static", the heading in the chosen tag (visually hidden when asked), the button.method-modal-close with the default or theme icon, and the content in .modal-body — then passes it through method/modal_markup. The result is shown on the Modal page under Frontend Rendering.
print_modals(): void / print_late_modals(): void
Print every registered modal not yet printed; the late pass runs the same and then closes the registry. Both run ahead of the CSS collector’s flush and the footer scripts (both wp_footer 20).
Global Helpers
method_register_modal()—Method_Modals::instance()->register( $args ).method_sanitize_modal_id()— the id rules, mirrored bysanitizeModalId()in the editor.method_get_modal_sizes()— the size map, filterable withmethod/modal_sizes.
Usage
// A modal every page carries, from a child theme.
add_action( 'method/register_modals', function ( Method_Modals $registry ) {
if ( $registry->is_registered( 'contact' ) ) {
return; // a page declared its own
}
$registry->push_context( array( 'scope' => 'site', 'source' => 'child-theme' ) );
do_blocks( get_option( 'child_contact_modal_blocks' ) ); // the Modal block inside registers itself
$registry->pop_context();
} );