# PHP

Pixel's public hooks, with an example for each. Their parameters and return
values are in the [hooks reference](/docs/pixel/reference/hooks.md), generated
from the code.

Several filters receive the image design being rendered, with its `settings`,
`elements`, `other` and `locations`, but not the image's ID. To target one
image, compare its name, `settings.name`, which is the name you gave it in the
image library.

## Conditions

`sleekPixel/conditions` adds conditions to the [Conditions](/docs/pixel/locations.md#custom)
view. Each needs an `id`, a `label` and a `value` computed for the current
request; an `id` that starts with `is` or `has` makes a boolean condition. Use
lowercase IDs, because the editor stores them lowercased.

```php
add_filter('sleekPixel/conditions', function ($conditions) {
  $conditions[] = [
    'id' => 'is_campaign',
    'label' => 'Is campaign link',
    'value' => isset($_GET['campaign']),
  ];

  return $conditions;
});
```

## Data

`sleekPixel/data` changes the [dynamic data](/docs/pixel/dynamic-data.md) before
an image uses it. The second argument holds `postId`, the post being rendered,
and the image design.

```php
add_filter('sleekPixel/data', function ($data, $args) {
  $imageName = $args['settings']['name'] ?? '';

  // Change the post title for one image only.
  if ($imageName === 'Blog post card') {
    $data['post']['title'] = 'New Post Title';
  }

  return $data;
}, 10, 2);
```

## Custom data

`sleekPixel/data/custom` adds entries to the Custom group of the dynamic data.
[Custom data](/docs/pixel/custom-data.md) has an example and the rules the
entries follow.

## Element data

`sleekPixel/image/element/data` changes one element of a design just before
it renders. The element holds its `type` (`text`, `image` or `section`), its
`data` and its `styles`; the second argument is the image design.

```php
add_filter('sleekPixel/image/element/data', function ($element, $image) {
  $imageName = $image['settings']['name'] ?? '';
  $isText = ($element['type'] ?? '') === 'text';

  // Colour every text element of one image blue.
  if ($imageName === 'Blog post card' && $isText) {
    $element['data']['color']['value'] = '#007bff';
  }

  return $element;
}, 10, 2);
```

## Page output

To write its Open Graph and Twitter Card tags, Pixel buffers a front-end page
from `wp_head` to `wp_footer` when an image's Open Graph or Twitter Card
conditions match it, or when `sleekPixel/buffer/output` has a callback.
`sleekPixel/buffer/output` receives that HTML after Pixel has changed it and
returns what is sent. When another plugin opened an output buffer after
Pixel's and has not closed it by the end of `wp_footer`, Pixel leaves both
buffers as they are and the page goes out unchanged.

```php
add_filter('sleekPixel/buffer/output', function ($html) {
  return str_replace('</head>', "<!-- Images by Pixel -->\n</head>", $html);
});
```

## Init

`sleekPixel/init` fires once after Pixel has booted, with the plugin instance
that `sleekPixel()` also returns.

```php
add_action('sleekPixel/init', function ($pixel) {
  // Pixel's post types, shortcode and commands are registered.
});
```
