Here’s couple examples on how to determine which page you’re on within a filter and how to return different data based on it.
On these examples I’m using the apply_filter()
part of the code you linked to to hook the custom code to the theme code. The custom function recieves the variabels from apply_filter()
as parameters.
The custom function should return a value with a matching type and format as the first parameter.
In my example the option 1 uses a hard-coded array of pages and logos (with a image id and image source array examples). Option 2 assumes the page specific logo is saved as an integer in the page post_meta. The custom function returns custom image data, if there’s a page match and the data is found. The function returns the first parameter as a fallback / default, meaning nothing was changed.
function my_prefix_filter_astra_replace_header_logo( $image ) {
// option 1 - logo from hard coded value
global $post;
$logos_for_pages = array(
'page-a' => 123,
'page-b' => array( // using wp_get_attachment_image_src() return format
'/wp-content/themes/my-theme/logos/logo-b.png', // url
100, // width
30, // height
false // is_intermediate
),
);
if ( isset( $logos_for_pages[$post->post_name] ) ) {
if ( is_int( $logos_for_pages[$post->post_name] ) ) {
$replace_logo = wp_get_attachment_image_src( $logos_for_pages[$post->post_name], 'ast-logo-size' );
if ( false !== $replace_logo ) {
return $replace_logo;
}
} else {
return $logos_for_pages[$post->post_name];
}
}
// option 2 - logo from post meta
global $post;
$logos_for_pages = array(
'page-a',
'page-b'
);
foreach ( $logos_for_pages as $page_name ) {
if ( is_page( $page_name ) ) {
$logo_id = get_post_meta( $post->ID, 'meta_key_for_page_specific_logo', true );
if ( $logo_id ) {
$replace_logo = wp_get_attachment_image_src( $logo_id, 'ast-logo-size' );
if ( false !== $replace_logo ) {
return $replace_logo;
}
}
}
}
return $image;
}
add_filter( 'astra_replace_header_logo', 'my_prefix_filter_astra_replace_header_logo' );