# PHP hooks

SleekAI fires a few actions and filters for your own code. The
[hooks reference](/docs/ai/reference/hooks.md) lists each with its arguments and
return value; this page says which to use for what. Register them from a
plugin; a theme can use every hook here except `sleekAi/init`, which fires
before themes load.

SleekAI has no filter that changes a chatbot's instruction, model or widget
settings before it renders. To build a chatbot from data that differs per
page, render it from PHP with
[`sleekAi_render_chatbot()`](/docs/ai/chatbots/api.md#render-a-chatbot-from-php).

## By purpose

| To                                          | Use                                                                                            | Explained in                                                 |
| ------------------------------------------- | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
| Run code once SleekAI has booted            | `sleekAi/init`                                                                                 | [Developer API](/docs/ai/api.md)                                |
| Force polling or streaming                  | `sleekAi/polling`                                                                              | [Streaming](/docs/ai/streaming.md)                              |
| Set how long chats are kept                 | `sleekAi/retention/days`                                                                       | [Chat](/docs/ai/chat.md#keeping-chats-for-a-limited-time)       |
| Add chatbot display conditions              | `sleekAi/chatbot/conditions`                                                                   | [Conditions](/docs/ai/chatbots/conditions.md#custom-conditions) |
| Add chatbot data sources                    | `sleekAi/chatbot/sources`                                                                      | [Data](/docs/ai/chatbots/data.md#custom-sources)                |
| Add styles to chatbots                      | `sleekAi/chatbot/markup`, `sleekAi/chatbot/key={key}/markup`, `sleekAi/chatbot/id={id}/markup` | [Styling](/docs/ai/chatbots/styling.md)                         |
| Change the size limit of visitors' requests | `sleekAi/chatbot/maxRequestBytes`                                                              | [Model](/docs/ai/chatbots/model.md#limits-on-visitor-requests)  |

## Scheduled actions

SleekAI runs some work in the background. You can add your own callbacks to
these actions, for example to log when they run; SleekAI's own callbacks use
the default priority, 10.

| Action                                  | When it runs                                                                                                                                                                                    |
| --------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `sleekAi/altText/generate`              | Action Scheduler, group `sleek-ai`, once for every image uploaded while automatic alt text is on. Receives the attachment ID.                                                                   |
| `sleekAi/retention/prune`               | Action Scheduler, group `sleek-ai`, daily while chats have a retention period.                                                                                                                  |
| `sleek_ai_chatbot_file_sync`            | Action Scheduler, group `sleek-ai-chatbot-sync`, for each chatbot query variable with file synchronization on, at its interval. Receives the chatbot ID and the variable's index.               |
| `sleek_ai_chatbot_vector_store_upgrade` | Action Scheduler, group `sleek-ai-chatbot-sync`, five minutes after an update, until every chatbot with synchronized files has its OpenAI file store; it stops trying after twelve failed runs. |
| `sleek_ai_cleanup_completions`          | WordPress cron, hourly. Deletes polling files older than ten minutes.                                                                                                                           |

```php
add_action('sleekAi/altText/generate', function ($attachmentId) {
    error_log('SleekAI described attachment ' . $attachmentId);
}, 20);
```

Priority `20` runs the callback after SleekAI has written the text.

## Hooks of the workspace

The workspace assistant, the global settings panel and the shared AI service
belong to the workspace, and so do their hooks, such as the one that replaces
the assistant's completions provider. They are documented with the workspace,
not here.
