# PHP hooks

Byte fires these hooks for site code. Add them in a must-use plugin or your
theme's `functions.php`, not in a snippet, because Byte reads most of them
before snippets run. Their parameters and return values are listed in the
[hooks reference](/docs/byte/reference/hooks.md).

## Where snippets come from

`sleekByte/snippets/paths` returns the source folders Byte reads snippets
from. [Sources](/docs/byte/snippets/sources.md) explains the entries and where
new files go.

```php
add_filter('sleekByte/snippets/paths', function ($sources) {
  $sources[] = [
    'name' => 'Theme Snippets',
    'path' => get_stylesheet_directory() . '/snippets',
    'sort' => 50,
  ];

  return $sources;
});
```

## Defaults for snippets

`sleekByte/snippets/defaults/context` sets the context of every snippet whose
`snippet.json` has no `context`, `frontend` by default. Return a context or a
list of them:

```php
add_filter('sleekByte/snippets/defaults/context', function ($context) {
  return wp_get_environment_type() === 'staging' ? ['admin'] : $context;
});
```

`sleekByte/snippets/defaults/hook` sets the default hook, `init`. PHP files
without a `hook` of their own or of their snippet run on it, and the CSS, SCSS
and JavaScript files of snippets without conditions are registered on it. A
front-end-only hook such as `wp` therefore also stops those stylesheets and
scripts in wp-admin and the block editor, and every PHP file without a hook in
REST and AJAX requests, which never fire `wp`.

`sleekByte/snippets/defaults/php/hook` receives the hook each PHP file has
already resolved, its own, its snippet's or the default, and returns the one
it runs on. Unlike the default hook it also replaces a `hook` set in
`snippet.json`:

```php
add_filter('sleekByte/snippets/defaults/php/hook', function ($hook) {
  return $hook === 'init' ? 'wp_loaded' : $hook;
});
```

Byte registers snippet files during `after_setup_theme`, so none of these
filters can move a file onto a hook that has already fired.

## Git history

`sleekByte/snippets/git` turns per-snippet history on; it is off by default.
[History](/docs/byte/snippets/history.md) describes what it records.

```php
add_filter('sleekByte/snippets/git', '__return_true');
```

## Whether a snippet runs

`sleek_byte/snippet/run` decides whether a snippet runs on the current
request, after its enabled state, environments and schedule did; its
conditions still decide where it loads, and WP-CLI's snippet listing reports
the same answer. It receives the decision and the snippet's `snippet.json`.
Safe mode and the skip after three fatal errors stop a snippet whatever it
returns.

```php
add_filter('sleek_byte/snippet/run', function (bool $run, array $config) {
  return $run && !(($config['title'] ?? '') === 'Promo banner' && is_user_logged_in());
}, 10, 2);
```

## Boot and lifecycle

`sleekByte/init` fires once Byte has booted, with the Byte instance, which
the `sleekByte()` function also returns. Byte boots only on a request that
reaches it: its workspace, REST routes, WP-CLI commands and `sleekByte()`,
and on a site with snippets a page's enqueue hooks (front end, wp-admin and
the block editor), the hooks the snippets' PHP files run on and the hourly
preview cleanup. A request that reaches none of them, such as a REST request
on a site whose PHP snippets all run on `wp_footer`, does not load Byte, and
`sleekByte/init` does not fire there. `sleekByte/activated` fires when Byte
is activated and `sleekByte/deactivated` when it is deactivated; deactivating
keeps every snippet.

```php
add_action('sleekByte/init', function ($byte) {
  // Byte's CLI commands, snippet runtime and REST routes are registered.
});
```

An [exported plugin](/docs/byte/snippets/export.md) fires its own copies of the
snippet filters under a prefix unique to it, so these hooks do not reach it.
