Hi @aisforadam,
I’ve been able to replicate your issue.
It’s because the function does not run completely on the home page. Therefore, the filters never fire.
This is because of the empty “post ID”. This is probably because your home page is a blog (Page ID 0).
I was aught to fix this in an earlier version, but I believe that propagated towards more issues as WordPress core functionality misplaces the global $post object a lot (that’s why I created “get_the_real_ID”).
Nevertheless, what you should use are the following filters for the home page until further notice:
the_seo_framework_ogimage_output
the_seo_framework_twitterimage_output
An example piece of code (I commented out the namespace’d filters so other less-experienced users will benefit from this as well):
I also added caches and fine-tuned some calls for improved performance.
//add_filter( 'the_seo_framework_og_image_after_featured', __NAMESPACE__ . '\\seo_fallback_image' );
add_filter( 'the_seo_framework_og_image_after_featured', 'seo_fallback_image' );
/**
* wp_get_attachment_url is quite a heavy function.
* As we run it multiple times, we cache it.
*/
function seo_fallback_image() {
static $img = null;
if ( isset( $img ) )
return $img;
return $img = wp_get_attachment_url( 58 );
}
//add_filter( 'the_seo_framework_og_image_args', __NAMESPACE__ . '\\seo_page_image' );
add_filter( 'the_seo_framework_og_image_args', 'seo_page_image' );
function seo_page_image( $args = array() ) {
//* I held this for performance improvements.
if ( is_home() || ! has_post_thumbnail( $args['post_id'] ) )
$args['image'] = seo_fallback_image(); // Could be yournamespace\seo_fallback_image() depending on location of function.
$args['disallowed'] = array(
'header',
'icon',
// 'wpmudev-avatars', // This has been removed internally in favor for site-icon.
);
return $args;
}
//add_filter( 'the_seo_framework_ogimage_output', __NAMESPACE__ . '\\seo_homepage_image', 10, 2 );
//add_filter( 'the_seo_framework_twitterimage_output', __NAMESPACE__ . '\\seo_homepage_image', 10, 2 );
add_filter( 'the_seo_framework_ogimage_output', 'seo_homepage_image', 10, 2 );
add_filter( 'the_seo_framework_twitterimage_output', 'seo_homepage_image', 10, 2 );
/**
* Filter located in render.class.php
*
* @param string $image The found image URL. Already escaped.
* @param int $page_id The current term, post, page ID.
*/
function seo_homepage_image( $image = '', $page_id = 0 ) {
//* You'll need to escape here (sort of)!
if ( empty( $page_id ) || ! has_post_thumbnail( $page_id ) )
return esc_url( seo_fallback_image() ); // Could be yournamespace\seo_fallback_image() depending on location of function.
return $image;
}
I hope this helps! Best of luck 🙂