# Code Snippets Reference

> Copy-paste examples for every public hook in the Aegis plugin. Each snippet is a working example you can adapt.

## Where to Put Snippets

Add snippets through a plugin, not a theme’s `functions.php`. Code in a plugin survives theme updates and keeps working if you switch themes.

### Aegis Code Snippets (recommended)

The free Aegis plugin has a built-in [Code Snippets](https://www.atmostfear-entertainment.com/docs/aegis/plugins/dash/code-snippets/) screen:

1. Go to **Aegis → Code Snippets → Settings** and turn on **PHP Enabled**.
2. Add a snippet and choose the **Functions (PHP)** type.
3. Paste the code, leave **Where to Run** on **Everywhere** (or pick **Frontend only** or **Admin only**), turn the snippet on, and save.

Functions snippets run early on `init`. A snippet that hooks something WordPress or the theme runs before `init` (for example `after_setup_theme`) belongs in a site-specific plugin instead. If a snippet causes a fatal error, Safe Mode pauses snippets so you can fix it.

### A Site-Specific Plugin

Create `wp-content/plugins/my-site-snippets/my-site-snippets.php`:

```
<?php/** * Plugin Name: My Site Snippets * Description: Custom code for this site. */defined( 'ABSPATH' ) || exit;// Paste snippets below.
```

Activate **My Site Snippets** under **Plugins**, then paste each snippet at the end of the file. Any other code snippets plugin works too.

> Filters must **return** a value. Test on a staging site first.

---

## Admin Screens

### Add a Tab to the Aegis Admin

`aegis_admin_tabs` — the tabs across the top of every Aegis screen, as `key => [ label, url, priority ]`.

```
add_filter( 'aegis_admin_tabs', function ( array $tabs ): array {	$tabs['site-health'] = array(		'label'    => 'Site Health',		'url'      => admin_url( 'site-health.php' ),		'priority' => 95,	);	return $tabs;} );
```

### Reorder the Tabs

`aegis_admin_tab_priorities` — default sort weights per tab key (lower comes first). Defaults: dashboard 0, blocks 10, modals 20, hook-patterns 30, snippets 40, visibility-presets 45, conditional-logic 50, integrations 60, connectors 65, performance 70, general-settings 90, license 100.

```
add_filter( 'aegis_admin_tab_priorities', function ( array $priorities ): array {	$priorities['snippets'] = 5; // Show Snippets right after Dashboard.	return $priorities;} );
```

### Add a Notice to an Aegis Screen

`aegis_admin_before_{screen}_page` — runs at the top of each screen. Screens: `blocks`, `conditionals`, `connectors`, `general_settings`, `hooks`, `integrations`, `license`, `modals`, `performance`, `snippets`, `visibility_presets`.

```
add_action( 'aegis_admin_before_snippets_page', function (): void {	echo '<div class="notice notice-info"><p>Ask the dev team before adding PHP snippets on production.</p></div>';} );
```

### Build an Add-on Screen

Add-ons such as Aegis Docs use these hooks to fit into the Aegis admin.

Load your screen’s styles and scripts on your own screen only (check the hook suffix or the `page` query argument), with `aegis-admin-ui` as a dependency for the shared kit. Aegis screens are normal page loads, so nothing else needs your files. Print the data your screen starts with into the page, for example as a `<script type="application/json">` block, so it draws without waiting for a REST request.

The Aegis menu is organised as **Aegis → Plugins → [Plugin]**. Screens without a `plugin` key belong to Aegis Dash. To list your screen as a plugin of its own, add `'group' => 'add-ons'` to its `aegis_admin_tabs` entry (the tab becomes one link under **Plugins**). To group several screens under one plugin name, give each the same `'plugin' => 'your-plugin'` and register the name with the `aegis_admin_plugin_labels` filter (`key => label`).

| Hook | Type | Arguments | Use |
| --- | --- | --- | --- |
| `aegis_general_settings_sections` | action | `Renderer $renderer` | Print your own section inside the **Aegis → Settings** form, so it saves with the rest |
| `aegis_admin_page_description` | filter | `string $description`, `string $tab_key` | The description under an admin tab’s title |
| `aegis_admin_settings_url` | filter | `string $url`, `string $page` | The **Settings** link in the top bar. Return an empty string for none |
| `aegis_admin_docs_url` | filter | `string $url`, `string $page` | The **Documentation** link in the top bar. Default: https://www.atmostfear-entertainment.com/docs/aegis/plugins/dash/ |
| `aegis_dashboard_addons` | filter | `array $addons` | Add a card to the Aegis dashboard. Each card: `label`, `icon` (dashicon slug), `status` (default `Active`), `tone` (`ok`, `warn`, `err`, `info`, or empty), `summary`, `url`, `action`, `settings_url` |

```
add_filter( 'aegis_admin_docs_url', function ( string $url, string $page ): string {	return 'my-addon' === $page ? 'https://example.com/docs/my-addon/' : $url;}, 10, 2 );add_filter( 'aegis_dashboard_addons', function ( array $addons ): array {	$addons['my-addon'] = array(		'label'        => 'My Add-on',		'icon'         => 'admin-generic',		'summary'      => '3 items',		'url'          => admin_url( 'admin.php?page=my-addon' ),		'action'       => 'Open My Add-on',		'settings_url' => admin_url( 'admin.php?page=my-addon&tab=settings' ),	);	return $addons;} );
```

### Extend the Conditionals and Connectors Screens

`aegis_admin_conditionals_sections` adds markup after the Conditionals sections. `aegis_connectors_nav_items`, `aegis_connectors_maps_section`, and `aegis_connectors_analytics_section` add items to the Connectors sidebar and panels.

These screens use the shared admin UI kit: a section list item is `.aegis-ui-subnav-item`, a panel is `.aegis-ui-section` (Aegis shows one at a time and adds `is-active`), and settings go in a card of rows (`.aegis-ui-card.aegis-ui-fields` holding `.aegis-ui-setting` rows). `Aegis\Plugin\Admin\UI::toggle()` prints a ready-made switch row.

```
add_action( 'aegis_connectors_nav_items', function (): void {	echo '<a href="#my-service" class="aegis-ui-subnav-item"><span class="dashicons dashicons-cloud" aria-hidden="true"></span>My Service</a>';} );add_action( 'aegis_connectors_analytics_section', function (): void {	?>	<section id="my-service" class="aegis-ui-section">		<div class="aegis-ui-head">			<div class="aegis-ui-head-text">				<h2>My Service</h2>				<p>Settings go here.</p>			</div>		</div>		<div class="aegis-ui-card aegis-ui-fields">			<?php echo \Aegis\Plugin\Admin\UI::toggle( 'my_service[enabled]', true, 'Enable My Service', 'Load the tracking script on the front end.' ); ?>		</div>	</section>	<?php} );
```

The old `aegis-nav-item`, `aegis-settings-section`, `aegis-settings-grid`, and `aegis-toggle-card` classes no longer have any styling or behaviour on these screens.

---

## Hook Locations

### Add a Custom Location

`aegis_injection_locations` — the locations offered by **Aegis → Snippets** and **Aegis → Hooks**, grouped as `global`, `admin`, `login`, `editor`, `theme`, and `integrations`. Each location is `hook => [ label, scope, requires ]`, where `scope` is `frontend`, `admin`, `login`, or `editor`.

```
add_filter( 'aegis_injection_locations', function ( array $locations ): array {	$locations['theme']['my_theme_after_hero'] = array(		'label'    => 'After hero (custom)',		'scope'    => 'frontend',		'requires' => null,	);	return $locations;} );
```

Your theme or plugin must call `do_action( 'my_theme_after_hero' )` where the content should appear.

### Print Content at an Integration Location

The plugin fires `aegis_before_{location}` and `aegis_after_{location}` around integration output while that integration is on. Use them from code like any action:

```
add_action( 'aegis_before_woocommerce_checkout', function (): void {	echo '<p class="checkout-note">Orders ship within 24 hours.</p>';} );
```

| Location hooks | Arguments |
| --- | --- |
| `aegis_{before,after}_woocommerce_checkout`, `aegis_{before,after}_woocommerce_cart` | none |
| `aegis_{before,after}_edd_download` | none |
| `aegis_{before,after}_affwp_dashboard` | none |
| `aegis_{before,after}_learndash_{course,lesson,topic}` | `$post_id, $course_id, $user_id` |
| `aegis_{before,after}_learndash_quiz` | `$quiz_id, $course_id, $user_id` |
| `aegis_learndash_focus_{header,footer}` | `$course_id` |
| `aegis_{before,after}_llms_{course,lesson}` | `$post_id` |
| `aegis_{before,after}_sensei_{course,lesson,quiz}` | `$post_id` |
| `aegis_{before,after}_fluentform` | none |
| `aegis_{before,after}_gform` | `$form` |
| `aegis_{before,after}_nf_form` | `$form_id` |
| `aegis_{before,after}_bbpress_{forum,topic}` | none |
| `aegis_{before,after}_post_author` | none |
| `aegis_{before,after}_map_block` | none (Google Maps connector on) |
| `aegis_{before,after}_video_block` | none (BunnyCDN connector on) |

Hooks with arguments need the argument count:

```
add_action( 'aegis_after_learndash_lesson', function ( $post_id, $course_id, $user_id ): void {	if ( learndash_course_completed( $user_id, $course_id ) ) {		echo '<p class="lesson-note">You finished this course. Nice work!</p>';	}}, 10, 3 );add_action( 'aegis_before_gform', function ( $form ): void {	if ( is_array( $form ) && 'Contact' === ( $form['title'] ?? '' ) ) {		echo '<p>We reply within one business day.</p>';	}} );
```

---

## Snippet Library

### Add Snippets to the Library

`aegis_snippet_library` — the entries in the **Library** button of the snippet editor, each as `array( 'group', 'type', 'label', 'desc', 'code' )`. `type` is `php`, `content`, `css`, or `js`. Add an optional `requires` (`woocommerce`, `yoast`, `gravityforms`, `aegis-ads`, `aegis-docs`, `aegis-forms`, `aegis-pro`, or the name of a function or class) to show the entry only while that plugin is active.

```
add_filter( 'aegis_snippet_library', function ( array $entries ): array {	$entries[] = array(		'group' => 'My Plugin',		'type'  => 'php',		'label' => 'Disable My Plugin Notices',		'desc'  => 'Hides the dashboard notices from My Plugin.',		'code'  => "<?php\nadd_filter( 'my_plugin_show_notices', '__return_false' );\n",	);	return $entries;} );
```

Add entries only while your plugin is active, so the group disappears with it.

---

## Blocks

### Load Extra Google Maps Libraries

`aegis_map_google_libraries` — Google Maps JavaScript libraries loaded for a Map block. Receives the list and the block attributes.

```
add_filter( 'aegis_map_google_libraries', function ( array $libraries, array $attributes ): array {	$libraries[] = 'places';	return $libraries;}, 10, 2 );
```

---

## SEO and Schema

### Change Co-Author Schema

`aegis_coauthor_schema_person` — one `Person` object per co-author in the article schema. Receives the person array and the co-author object.

```
add_filter( 'aegis_coauthor_schema_person', function ( array $person, $coauthor ): array {	$job = get_user_meta( $coauthor->ID, 'job_title', true );	if ( $job ) {		$person['jobTitle'] = $job;	}	return $person;}, 10, 2 );
```

### Add Author Schema to More Post Types

`coauthors_supported_post_types` (from Co-Authors Plus) — also controls which post types get Aegis author schema.

```
add_filter( 'coauthors_supported_post_types', function ( array $post_types ): array {	$post_types[] = 'case_study';	return $post_types;} );
```

### Change Schema From Your SEO Plugin

When an SEO plugin integration is on, Aegis hooks into that plugin’s schema output (priority 20) and passes it through an Aegis filter, so you can adjust it in one place.

```
// Yoast SEO: the full graph.add_filter( 'aegis_yoast_schema_graph', function ( array $graph, $context ): array {	foreach ( $graph as $i => $piece ) {		if ( ( $piece['@type'] ?? '' ) === 'Organization' ) {			$graph[ $i ]['foundingDate'] = '2012';		}	}	return $graph;}, 10, 2 );// Rank Math: the JSON-LD data.add_filter( 'aegis_rank_math_json_ld', function ( array $data, $context ): array {	return $data;}, 10, 2 );// All in One SEO: the schema output.add_filter( 'aegis_aioseo_schema_output', function ( array $schema ): array {	return $schema;} );// SEOPress: automatic schema. $type is faq, event, local_business, or video.add_filter( 'aegis_seopress_schemas_auto_schema', function ( array $schema, string $type ): array {	if ( 'video' === $type ) {		$schema['isFamilyFriendly'] = true;	}	return $schema;}, 10, 2 );
```

---

## Uninstall

`aegis_uninstall_purge` — fires when the plugin deletes its data with Aegis Pro still installed (only when **Delete data on uninstall** is on). Use it to clean up your own Aegis-related data.

```
add_action( 'aegis_uninstall_purge', function (): void {	delete_option( 'my_child_theme_aegis_settings' );} );
```

Because this runs during plugin deletion, put it in a site-specific plugin or mu-plugin rather than a snippet or a theme if you rely on it.

---

## Related

- [Theme Code Snippets](https://www.atmostfear-entertainment.com/docs/aegis/themes/theme/code-snippets/)
- [Pro Code Snippets](https://www.atmostfear-entertainment.com/docs/aegis/plugins/dash/)
- [Hook Patterns](https://www.atmostfear-entertainment.com/docs/aegis/plugins/dash/hook-patterns/)
- [Code Snippets screen](https://www.atmostfear-entertainment.com/docs/aegis/plugins/dash/code-snippets/)

---

Source: https://www.atmostfear-entertainment.com/docs/aegis/plugins/dash/code-snippets-reference/ · Updated 2026-11-25
