# PHP hooks and filters

> WordPress filters and actions for AjaxPress: login handling, block themes, settings, front-end variables and Super Cache by Cloudflare.

Source: https://arraystory.com/docs/ajaxpress/php-hooks-and-filters/

Filters and actions for customising AjaxPress from a theme or plugin. Put them in your theme's `functions.php`, or better, in a small site plugin or a code snippet, so they survive a theme change.

## Navigation

### ajaxpress_ajax_login

Whether `wp-login.php` runs with AjaxPress, so logging in and registering there happen without a page reload. Default `true`.

```php
// Keep wp-login.php as a normal page.
add_filter( 'ajaxpress_ajax_login', '__return_false' );
```

To send every login and registration form (WooCommerce's included) through a normal page load, turn on **Full Page Reload on Login & Registration** in the settings instead.

### ajaxpress_keep_block_navigation

On block themes, AjaxPress switches off WordPress's own client-side navigation scripts (the Interactivity API router and the Query Loop's navigation), because they compete with AjaxPress and can break the Back button. The Navigation block's script is kept, so mobile menus keep working. Return `true` to keep all of them. Default `false`.

```php
add_filter( 'ajaxpress_keep_block_navigation', '__return_true' );
```

## Settings

### ajaxpress_settings

Filters the stored settings, merged over their defaults, wherever AjaxPress reads them.

| Parameter | Type | Description |
| --- | --- | --- |
| `$settings` | array | Settings, keyed by name |
| `$options_from_db` | array | The raw `ajaxpress_*` options, as stored |

### ajaxpress_public_settings

Filters the settings used on the front end, after `ajaxpress_settings`. Use it to force a value on a site without touching the saved settings:

```php
// Always scroll new pages to the top, whatever the settings say.
add_filter( 'ajaxpress_public_settings', function ( $settings ) {
	$settings['scroll_to_top'] = true;
	return $settings;
} );
```

Setting names are listed in the [settings reference](https://arraystory.com/docs/ajaxpress/settings-reference/), and match the keys in an exported settings file.

### ajaxpress_default_options

Filters the default value of every setting. Add-ons use it to register their own settings.

### ajaxpress_settings_schema

Every setting has a type, and a saved value is cleaned by its type. A value that doesn't fit falls back to the setting's default, and a setting without a type is dropped. Add-ons that store their own settings must declare them here:

```php
add_filter( 'ajaxpress_default_options', function ( $defaults ) {
	$defaults['myaddon_accent'] = '#e11d48';
	return $defaults;
} );

add_filter( 'ajaxpress_settings_schema', function ( $schema ) {
	$schema['myaddon_accent'] = array( 'type' => 'color' );
	return $schema;
} );
```

The types are `bool`, `int`, `float`, `enum` (with `values`), `color`, `url`, `text`, `textarea`, `selectors`, `html_classes`, `html_id`, `css`, `css_length`, `duration`, `id_list` and `ref_list`.

## Front end

### ajaxpress_frontend_vars

Filters the data handed to AjaxPress's front-end script (the `ajaxpress_vars` object). Add your own values here for a module built on the [JavaScript extension API](https://arraystory.com/docs/ajaxpress/javascript-extension-api/):

```php
add_filter( 'ajaxpress_frontend_vars', function ( $vars ) {
	$vars['my_addon'] = array( 'station' => 'live-radio' );
	return $vars;
} );
```

### ajaxpress_inline_css

Filters the small stylesheet AjaxPress prints for its loader and cursor.

| Parameter | Type | Description |
| --- | --- | --- |
| `$css` | string | The CSS |

## Lifecycle

### ajaxpress_upgraded (action)

Fires once when the AjaxPress version on disk changes: after an update, a downgrade, or on a new install.

| Parameter | Type | Description |
| --- | --- | --- |
| `$from` | string | The previous version, or an empty string on a new install |
| `$to` | string | The version now installed |

```php
add_action( 'ajaxpress_upgraded', function ( $from, $to ) {
	// Clear your own page cache after AjaxPress updates, for example.
}, 10, 2 );
```

## Super Cache by Cloudflare (Pro)

These tune [Super Cache by Cloudflare](https://arraystory.com/docs/ajaxpress/cloudflare-super-cache/). Filters that change the cache rules (`ajaxpress_cf_bypass_patterns`, `ajaxpress_cf_bypass_cookies` and `ajaxpress_cf_edge_ttl`) reach Cloudflare the next time AjaxPress syncs its settings there, for example when you next change something on the Cache tab.

| Filter | Value | Default | What it controls |
| --- | --- | --- | --- |
| `ajaxpress_cf_bypass_patterns` | array of path prefixes | `/wp-admin`, `/wp-json`, `/wp-login`, `/checkout`, `/cart` | Paths that are never cached |
| `ajaxpress_cf_bypass_cookies` | array of cookie name fragments | `wordpress_logged_in_`, `woocommerce_`, `wp_woocommerce_session_`, `edd_`, `comment_author_`, `wordpress_sec_` | Visitors with a matching cookie always bypass the cache |
| `ajaxpress_cf_edge_ttl` | int, seconds | `3600` | How long a page stays cached at the edge |
| `ajaxpress_cf_purge_post_urls` | array of URLs, plus `$post_id` | The post, home page, archive, categories, tags, author | What is purged when a post is published or updated |
| `ajaxpress_cf_auto_purge` | bool | `true` | Purging on edit. Turn it off only if you purge yourself |
| `ajaxpress_cf_warm_seed_urls` | array of URLs, plus the cache object | Key pages | Pages cached first after connecting |
| `ajaxpress_cf_warm_daily_budget` | int | `900` | How many pages a day the background fill may cache |
| `ajaxpress_cf_kv_pages` | bool | `true` when available | The globally replicated copy of pages ("Global layer") |

```php
// Keep a members area out of the edge cache, and cache pages for six hours.
add_filter( 'ajaxpress_cf_bypass_patterns', function ( $patterns ) {
	$patterns[] = '/members';
	return $patterns;
} );
add_filter( 'ajaxpress_cf_edge_ttl', function () {
	return 6 * HOUR_IN_SECONDS;
} );

// Also purge a custom landing page whenever a post changes.
add_filter( 'ajaxpress_cf_purge_post_urls', function ( $urls, $post_id ) {
	$urls[] = home_url( '/latest/' );
	return $urls;
}, 10, 2 );
```
