$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Adding Facebook Open Graph Code does not work for Media Library Attachment Pages|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Adding Facebook Open Graph Code does not work for Media Library Attachment Pages

matteradmin9PV0评论

I have code in my page header which properly creates the Open Graph tags for pages, posts, etc. However, when I attempt to Share an attachment post from the Media Library (not the actual attachment image, but rather the URI), the FB Object Debugger complains

  "Error parsing input URL, no data was cached, or no data was scraped."

I've examined the header on generated pages and they appear identical to other Pages, Posts and other Custom Post types. We were using Yoast and I -thought- that this was the problem, but I disabled it and still not working.

Eg. the WordPress URL is

     /

Which contains a link to:

     .pdf

Which works fine.

Here is the code I put in my header. Which (again) works FINE for all other post types. But not attachments.

function insert_fb_in_head() {

    $og_title = get_post_meta( get_the_ID(), '_yoast_wpseo_title', true);
    if( ! $og_title)    
        $og_title = get_the_title();

    echo ('<meta property="og:title" content="' . $og_title . '"/>' . PHP_EOL);
    echo ('<meta property="og:type" content="article"/>' . PHP_EOL);
    echo ('<meta property="og:url" content="' . get_permalink() . '"/>' . PHP_EOL);
    echo ('<meta property="og:site_name" content="Noise Information"/>' . PHP_EOL);
    echo ('<meta property="og:description" content="' . strip_tags( get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true), '' ) . '"/>' . PHP_EOL);

    if( has_post_thumbnail( $post->ID )) { //the post has a featured image, use a default image
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
        echo ('<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>' . PHP_EOL);
        echo ('<meta property="og:image:width" content="' . esc_attr( $thumbnail_src[1] ) . '"/>' . PHP_EOL);
        echo ('<meta property="og:image:height" content="' . esc_attr( $thumbnail_src[2] ) . '"/>' . PHP_EOL);      
    }

    $default_image=".png"; 
    echo ('<meta property="og:image" content="' . $default_image . '"/>' . PHP_EOL);
    echo ('<meta property="og:image:width" content="250"/>' . PHP_EOL);
    echo ('<meta property="og:image:height" content="250"/>' . PHP_EOL);        
 }
 add_action( 'wp_head', 'insert_fb_in_head', 5 );

Here is the section of an example rendered page source HTML:

  <meta property="og:title" content="2017 POS CAFR FINAL 5.2.2018"/>
  <meta property="og:type" content="article"/>
  <meta property="og:url" content="/"/>
  <meta property="og:site_name" content="Example Site"/>
  <meta property="og:description" content="2017 POS CAFR FINAL 5.2.2018"/>
  <meta property="og:image" content=".png"/>
  <meta property="og:image:width" content="250"/>
  <meta property="og:image:height" content="250"/>

AGAIN, I asked this question in another thread and it was suggested to try using Yoast and changing the setting in the "Redirect attachment URLs to the attachment itself?" But that made no difference and in fact, de-activating Yoast entirely made no difference.

Ideas?

Post a comment

comment list (0)

  1. No comments so far