1

I'm using the following function, and am using FB's debugger on a page that I'm certain has a featured image set:

function fb_opengraph() {
    global $post;

    if(is_page()) {

        if(has_post_thumbnail($post->ID)) {
            $img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');
        } else {
            $img_src = get_stylesheet_directory_uri() . '/img/opengraph_image.jpg';
        }
        if($excerpt = $post->post_excerpt) {
            $excerpt = strip_tags($post->post_excerpt);
            $excerpt = str_replace("", "'", $excerpt);
        } else {
            $excerpt = get_bloginfo('description');
        }
        ?>

    <meta property="og:title" content="<?php echo the_title(); ?>"/>
    <meta property="og:description" content="<?php the_content(); ?>"/>
    <meta property="og:type" content="article"/>
    <meta property="og:url" content="<?php echo the_permalink(); ?>"/>
    <meta property="og:site_name" content="<?php echo get_bloginfo(); ?>"/>
    <meta property="og:image" content="<?php echo $img_src; ?>"/>

<?php
    } else {
        return;
    }
}
add_action('wp_head', 'fb_opengraph', 5);

What's currently happening is that $img_src is coming back with the value "Array", and not with the URL for the featured image for that page. I'm not sure where this "Array" value is even coming from, but more importantly am trying to pull in the featured image URL with no luck.

Any ideas? Thanks!

hudsonian
  • 349
  • 5
  • 21

2 Answers2

2

https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/

This should give you an array

wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');

for getting the url you need to get appropriate array element

$img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium')['url'];

Give it a try

Nick Surmanidze
  • 1,671
  • 1
  • 11
  • 20
  • Thanks for the speedy response. I've added that code and it's currently coming back with no value at all for $img_src. "Provided og:image URL, was not a valid URL.", according to FB's debugger. I've doublechecked that there is indeed a featured image there. – hudsonian Apr 27 '16 at 15:44
  • Try looking into page source via the browser. What is in src there? – Nick Surmanidze Apr 27 '16 at 15:44
  • It's simply "" for the og:image: – hudsonian Apr 27 '16 at 16:07
  • 1
    then try $img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium')[0]; and if it does not work then I would print_r(wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium')) and see that the output is. Maybe it cannot fetch an image array at all but I cannot really spot any error. technically it should be working if I am not missing something. – Nick Surmanidze Apr 27 '16 at 16:15
  • That worked! Thanks so much--very much appreciated. Cheers! – hudsonian Apr 27 '16 at 17:28
  • $img_src = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium')['url']; echo $img_src[0]; – Karra Max Jan 12 '20 at 16:48
1
wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'medium');

it will return an array. The 1st element of this array will contain the image URL.

So you need to mention the first index of this array this way: $img_src[0]

The full line will be:

<meta property="og:image" content="<?php echo $img_src[0]; ?>"/>
Ebrahim Khalilullah
  • 556
  • 1
  • 6
  • 10