Add preload for header image + filter

Also extends the filter for the header image.
This commit is contained in:
Tetrakern 2024-12-09 01:20:03 +01:00
parent 056d5eb54c
commit 939013dd06
2 changed files with 32 additions and 8 deletions

View File

@ -972,12 +972,24 @@ Filters the font array compiled from all valid font folders and Google Fonts lin
---
### `apply_filters( 'fictioneer_filter_header_image', $header_image_url, $post_id )`
### `apply_filters( 'fictioneer_filter_header_image', $header_image_url, $post_id, $source )`
Filters the URL of the header image in the `header.php` template.
**Parameters:**
* $header_image_url (string) URL of the current header image, either global or custom.
* $post_id (int|null) Current post ID. Unsafe since this may be an archive, author, etc.
* $source (string) Either `'default'`, `'post'`, or `'story'`.
---
### `apply_filters( 'fictioneer_filter_header_image_preload', $tag, $header_image_url, $post_id, $source )`
Filters the preload link tag of the header image in the `header.php` template.
**Parameters:**
* $tag (string) Link tag for preloading the header image.
* $header_image_url (string) URL of the current header image, either global or custom.
* $post_id (int|null) Current post ID. Unsafe since this may be an archive, author, etc.
* $source (string) Either `'default'`, `'post'`, or `'story'`.
---

View File

@ -64,19 +64,31 @@ if ( $post_id ) {
// Custom header image?
if ( get_post_meta( $post_id, 'fictioneer_custom_header_image', true ) ) {
$header_image_url = get_post_meta( $post_id, 'fictioneer_custom_header_image', true );
$header_image_url = wp_get_attachment_image_url( $header_image_url, 'full' );
$header_image_id = get_post_meta( $post_id, 'fictioneer_custom_header_image', true );
$header_image_url = wp_get_attachment_image_url( $header_image_id, 'full' );
$header_image_source = 'post';
} elseif ( ! empty( $story_id ) && get_post_meta( $story_id, 'fictioneer_custom_header_image', true ) ) {
$header_image_url = get_post_meta( $story_id, 'fictioneer_custom_header_image', true );
$header_image_url = wp_get_attachment_image_url( $header_image_url, 'full' );
$header_image_id = get_post_meta( $story_id, 'fictioneer_custom_header_image', true );
$header_image_url = wp_get_attachment_image_url( $header_image_id, 'full' );
$header_image_source = 'story';
}
}
// Filter header image
if ( ! empty( $post_id ) && $header_image_url ) {
$header_image_url = apply_filters( 'fictioneer_filter_header_image', $header_image_url, $post_id );
$header_image_preload = '';
if ( $header_image_url ) {
$header_image_url = apply_filters( 'fictioneer_filter_header_image', $header_image_url, $post_id, $header_image_source );
if ( $header_image_url ) {
$header_image_preload = apply_filters(
'fictioneer_filter_header_image_preload',
"<link rel='preload' href='{$header_image_url}' as='image' fetchPriority='high'>",
$header_image_url,
$post_id,
$header_image_source
);
}
}
// Action arguments
@ -131,7 +143,7 @@ $body_attributes = array_map(
<html <?php language_attributes(); ?> <?php fictioneer_root_attributes(); ?>>
<head><?php wp_head(); ?></head>
<head><?php echo $header_image_preload; wp_head(); ?></head>
<body <?php body_class( 'site-bg scrolled-to-top' ); echo implode( ' ', $body_attributes ); ?>>
<?php