Add fictioneer_filter_body_attributes filter

This commit is contained in:
Tetrakern 2024-10-13 10:14:34 +02:00
parent 0f09c011c8
commit 1615506c5b
2 changed files with 30 additions and 0 deletions

View File

@ -115,6 +115,33 @@ Filters the intermediate output array in the `_article-cards.php` partial before
--- ---
### `apply_filters( 'fictioneer_filter_body_attributes', $attributes, $args )`
Filters the intermediate output array of additional body attributes (except `class` which is handled by `body_class()`) before it is mapped, escaped, imploded, and rendered in the `header.php` template.
**$attributes:**
* `data-post-id` (int) Current post ID (or -1 if null).
* `data-story-id` (int|null) Current story ID (if any). Unsafe.
**$args:**
* `post_id` (int|null) Current post ID. Unsafe.
* `post_type` (string|null) Current post type. Unsafe.
* `story_id` (int|null) Current story ID (if chapter). Unsafe.
* `header_image_url` (string|boolean) URL of the filtered header image or false.
* `header_image_source` (string) Source of the header image. Either `'default'`, `'post'`, or `'story'`.
* `header_args` (array) Arguments passed to the header.php partial.
**Example:**
```php
function child_add_body_attributes( $attributes ) {
$attributes['data-permalink'] = get_permalink();
return $attributes;
}
add_filter( 'fictioneer_filter_body_attributes', 'child_add_body_attributes' );
```
---
### `apply_filters( 'fictioneer_filter_breadcrumbs_array', $breadcrumbs, $args )` ### `apply_filters( 'fictioneer_filter_breadcrumbs_array', $breadcrumbs, $args )`
Filters the array of breadcrumb tuples inside the `fictioneer_get_breadcrumbs( $args )` function before the HTML is build. Filters the array of breadcrumb tuples inside the `fictioneer_get_breadcrumbs( $args )` function before the HTML is build.

View File

@ -98,6 +98,9 @@ if ( $story_id ) {
$body_attributes['data-story-id'] = $story_id; $body_attributes['data-story-id'] = $story_id;
} }
$body_attributes = apply_filters( 'fictioneer_filter_body_attributes', $body_attributes, $action_args );
unset( $body_attributes['class'] ); // Prevent mistakes
$body_attributes = array_map( $body_attributes = array_map(
function ( $key, $value ) { return $key . '="' . esc_attr( $value ) . '"'; }, function ( $key, $value ) { return $key . '="' . esc_attr( $value ) . '"'; },
array_keys( $body_attributes ), array_keys( $body_attributes ),