From 1615506c5b1add264e550244a7da5b02cad67855 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Sun, 13 Oct 2024 10:14:34 +0200 Subject: [PATCH] Add fictioneer_filter_body_attributes filter --- FILTERS.md | 27 +++++++++++++++++++++++++++ header.php | 3 +++ 2 files changed, 30 insertions(+) diff --git a/FILTERS.md b/FILTERS.md index 44b4cd15..512dec8b 100644 --- a/FILTERS.md +++ b/FILTERS.md @@ -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 )` Filters the array of breadcrumb tuples inside the `fictioneer_get_breadcrumbs( $args )` function before the HTML is build. diff --git a/header.php b/header.php index 27486bb9..cf1efbf9 100644 --- a/header.php +++ b/header.php @@ -98,6 +98,9 @@ if ( $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( function ( $key, $value ) { return $key . '="' . esc_attr( $value ) . '"'; }, array_keys( $body_attributes ),