DocsoPWA16. Developer Filters & Actions
Developer

16. Developer Filters & Actions

oPWAorravo.com/docs/opwa/16-developer-filters-actions

Filters

opwa_manifest_data

Modify the manifest array before it is JSON-encoded and served.

phpadd_filter( 'opwa_manifest_data', function( array $manifest ): array {
    $manifest['categories'] = [ 'news', 'blog' ];
    $manifest['prefer_related_applications'] = false;
    return $manifest;
} );

Parameters:

  • $manifest (array) — The full manifest data structure

opwa_service_worker_routes

Modify or extend the array of custom routes before the SW is generated.

phpadd_filter( 'opwa_service_worker_routes', function( array $routes ): array {
    $routes[] = [
        'pattern'     => '/api/.*',
        'strategy'    => 'network-only',
        'max_age'     => 0,
        'max_entries' => 0,
    ];
    return $routes;
} );

opwa_sw_extra_code

Append raw JavaScript to the generated service worker.

phpadd_filter( 'opwa_sw_extra_code', function( string $code ): string {
    $code .= "\nself.addEventListener('message', e => { if (e.data === 'MY_MSG') { /* … */ } });";
    return $code;
} );

opwa_precache_urls

Modify the auto-generated precache URL list.

phpadd_filter( 'opwa_precache_urls', function( array $urls ): array {
    $urls[] = home_url( '/offline-assets/hero.webp' );
    $urls[] = home_url( '/offline-assets/logo.svg' );
    return $urls;
} );

opwa_push_payload

Modify the push notification payload before it is encrypted and sent.

phpadd_filter( 'opwa_push_payload', function( array $payload, object $subscriber ): array {
    // Add a personalised action for logged-in subscribers
    if ( $subscriber->user_id ) {
        $payload['actions'] = [ [ 'action' => 'view', 'title' => 'View now' ] ];
    }
    return $payload;
}, 10, 2 );

opwa_analytics_beacon_data

Filter the raw beacon data before it is written to the DB.

phpadd_filter( 'opwa_analytics_beacon_data', function( array $data ): array {
    // Strip the URL to path-only for privacy
    if ( isset( $data['url'] ) ) {
        $data['url'] = parse_url( $data['url'], PHP_URL_PATH );
    }
    return $data;
} );

opwa_icon_sizes

Control which icon sizes are generated.

phpadd_filter( 'opwa_icon_sizes', function( array $sizes ): array {
    // Remove sizes your manifest doesn't use
    return array_filter( $sizes, fn( $s ) => in_array( $s, [ 192, 512 ], true ) );
} );

Actions

opwa_after_subscribe

Fires after a new push subscriber is successfully stored.

phpadd_action( 'opwa_after_subscribe', function( int $subscriber_id, string $endpoint ): void {
    // e.g., send a welcome notification
}, 10, 2 );

opwa_after_push_send

Fires after a push campaign completes.

phpadd_action( 'opwa_after_push_send', function( array $result, array $payload ): void {
    // $result = [ 'sent' => 140, 'failed' => 2, 'total' => 142 ]
    error_log( 'Push sent: ' . wp_json_encode( $result ) );
}, 10, 2 );

opwa_after_cache_clear

Fires after the SW version is bumped (cache clear).

phpadd_action( 'opwa_after_cache_clear', function( int $new_version ): void {
    do_action( 'my_plugin_purge_cdn' );
} );

opwa_on_activate

Fires after the plugin runs its full activation routine (tables created, defaults set).

phpadd_action( 'opwa_on_activate', function(): void {
    // One-time setup for your own plugin
} );

16. Developer Filters & Actions — oPWA Docs — Orravo