• Resolved aisforadam

    (@aisforadam)


    Hi. The og:image is working great on all pages except for the homepage (index.php). Currently, its blank:

    <meta property="og:image" content="" />

    My custom functions are:

    function seo_fallback_image() {
      return wp_get_attachment_url( 635 );
    }
    
    add_filter( 'the_seo_framework_og_image_after_featured', __NAMESPACE__ . '\\seo_fallback_image' );
    
    function seo_page_image( $args = array() ) {
    
      if( is_home() || !has_post_thumbnail() )
        $args['image'] = wp_get_attachment_url( 635 );
    
      $disallowed = array(
        'header',
        'icon',
        'wpmudev-avatars',
      );
    
      $args['disallowed'] = $disallowed;
    
      return $args;
    }
    
    add_filter( 'the_seo_framework_og_image_args', __NAMESPACE__ . '\\seo_page_image' );

    Is my configuration incorrect? Or is there another setting for the homepage? In the settings, I saw fields for the home description, etc. However, there was not a field for featured image.

    Thanks!

    • This topic was modified 9 years, 10 months ago by aisforadam.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    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 🙂

    • This reply was modified 9 years, 10 months ago by Sybre Waaijer.
    Thread Starter aisforadam

    (@aisforadam)

    That worked. Thanks for the quick reply and example code!

    Thread Starter aisforadam

    (@aisforadam)

    Adding a quick followup note in case somebody else runs across this thread. The twitter:card meta needed to be adjusted if wanting a setting other than the default ‘summary’.

    function seo_homepage_twitter_card( $card ) {
      return 'summary_large_image';
    }
    
    add_filter( 'the_seo_framework_twittercard_output', 'seo_homepage_twitter_card', 10, 2 );
    Plugin Author Sybre Waaijer

    (@cybr)

    These forums really needs a like button.
    Great catch @aisforadam! Cheers 😀

    P.S. WordPress 4.5’s Theme/Site Logo setting will be added in TSF 2.8.0 as a new fallback image.

    • This reply was modified 9 years, 10 months ago by Sybre Waaijer.
    • This reply was modified 9 years, 10 months ago by Sybre Waaijer.
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Home og:image missing’ is closed to new replies.