• First let me say that the auto-posting portion of the plugin works fine for me – at least for Twitter and Facebook.

    However earlier today someone shared my page on Facebook and the image and description text did not match the post that was posted (the title/link was the only accurate item).

    I’m somewhat familiar with php so I opened up the plugin and (NextScripts_SNAP.php file) and immediately saw a problem with the description. Note the following beginning at line 803:

    $decsription = preg_match( '/<meta name="description" content="(.*)"/', $content, $description_matches );
        if ( $description !== false && count( $description_matches ) == 2 ) $ogD = '<meta property="og:description" content="A' . $description_matches[1] . '" />'."\r\n"; {
          if (is_singular()) {
            if(has_excerpt($post->ID))$ogD=strip_tags(nxs_snapCleanHTML(get_the_excerpt($post->ID)));else $ogD= str_replace("  ", ' ', str_replace("\r\n", ' ', trim(substr(strip_tags(nxs_snapCleanHTML(strip_shortcodes($post->post_content))), 0, 200))));
          } else $ogD = get_bloginfo('description');  $ogD = preg_replace('/\r\n|\r|\n/m','',$ogD);
          $ogD = '<meta property="og:description" content="B'.esc_attr( apply_filters( 'nxsog_desc', $ogD ) ).'" />'."\r\n";
        }

    “$description” is misspelled on the first line, and then the ‘if’ statement that follows it has un-bracketed code followed then by bracketed code. I put brackets around the un-bracketed code followed by an ‘else’ – I think that’s how it is supposed to be. The code now properly parses the ‘description’ meta tag from the post content.

    The other problem which I’m still trying to figure out is that anything that pulls from post->ID is incorrect. It’s referencing a different post entirely. When I echo out the post->ID I can confirm it’s not the current page’s ID. What I *think* is happening is that it is pulling from the last post that gets looped through to make other post-related content on the page (either from my ‘related posts’ or ‘popular’ posts lists.) It’s been a while since I’ve had to dabble with WP code but my guess is that the plugin needs to execute earlier than it is, or somehow reset the post->ID to what it should be. If I can get a fix I’ll report back.

    http://wordpress.org/extend/plugins/social-networks-auto-poster-facebook-twitter-g/

Viewing 1 replies (of 1 total)
  • Thread Starter mferris77

    (@mferris77)

    Well, that was quick. Looks like there’s a useful function to reset the global post ID back to the original query. Simply adding “wp_reset_postdata();” to the beginning of the function works for me.

    I also identified another problem. I wondered why the og:image associated with my post was getting selected as I have a number of images in a particular post and it was choosing the last image in the post. Looks like there is some problems with the image code as well. OpenGraph protocol says we should be able to have multiple images associated with a post, this allows users to select the image to use when sharing something on facebook (I believe). The plugin first attempts to find a ‘post thumbnail’ (now referred to as the featured image). If it does, it stores that image to an array. It then runs a function to parse the rest of the post for images and correctly returns those images and adds them to the array of images for the post. However, the error is when it loops through all of these images to make the multiple og:image tags. Rather than append to the output string, it simply overwrites the string each time, and that is why the last image in my post is what was being used for the og:image tag.

    Note: One additional change I made: The featured image being retrieved was the ‘thumbnail’ version, which for me, was 150px x 150px. When I run this through the facebook debugger, it raises an error saying the images should be at least 200px on each side. I went ahead and changed the call to ‘wp_get_attachment_image_src()’ to use ‘medium’ instead of ‘thumbnail’. I don’t know enough about opengraph implementation in other platforms to know whether we should be using small thumbnails or offer larger images, so I’ll leave that up to the developer and/or other users to determine what it really should be set to. This change is reflected in my copy of the function below.

    Below is the entire fixed and working (for me) ‘nxs_ogtgCallback’ function which begins on line 796 of NextScripts_SNAP.php, version 2.6.3:

    function nxs_ogtgCallback($content){ global $post, $plgn_NS_SNAutoPoster;  if (!isset($plgn_NS_SNAutoPoster)) return; $options = $plgn_NS_SNAutoPoster->nxs_options;    $ogimgs = array();
    	wp_reset_postdata();
      if (stripos($content, 'og:title')!==false) $ogOut = "\r\n"; else {
        $title = preg_match( '/<title>(.*)<\/title>/', $content, $title_matches );
        if ($title !== false && count( $title_matches) == 2 ) $ogT ='<meta property="og:title" content="' . $title_matches[1] . '" />'."\r\n"; else {
          if (is_home() || is_front_page() )  $ogT = get_bloginfo( 'name' ); else $ogT = get_the_title();
          $ogT =  '<meta property="og:title" content="' . esc_attr( apply_filters( 'nxsog_title', $ogT ) ) . '" />'."\r\n";
        }
        $description = preg_match( '/<meta name="description" content="(.*)"/', $content, $description_matches );
        if ( $description !== false && count( $description_matches ) == 2 ){
        	 $ogD = '<meta property="og:description" content="' . $description_matches[1] . '" />'."\r\n";
        } else {
          if (is_singular()) {
            if(has_excerpt($post->ID))$ogD=strip_tags(nxs_snapCleanHTML(get_the_excerpt($post->ID)));else $ogD= str_replace("  ", ' ', str_replace("\r\n", ' ', trim(substr(strip_tags(nxs_snapCleanHTML(strip_shortcodes($post->post_content))), 0, 200))));
          } else $ogD = get_bloginfo('description');  $ogD = preg_replace('/\r\n|\r|\n/m','',$ogD);
          $ogD = '<meta property="og:description" content="'.esc_attr( apply_filters( 'nxsog_desc', $ogD ) ).'" />'."\r\n";
        }
        $ogSN = '<meta property="og:site_name" content="'.get_bloginfo('name').'" />'."\r\n";
        $ogLoc = strtolower(esc_attr(get_locale())); if (strlen($ogLoc)==2) $ogLoc .= "_".strtoupper($ogLoc);
        $ogLoc = '<meta property="og:locale" content="'.$ogLoc.'" />'."\r\n"; $iss = is_home();
        $ogType = is_singular()?'article':'website'; if($vidsFromPost == false) $ogType = '<meta property="og:type" content="'.esc_attr(apply_filters('nxsog_type', $ogType)).'" />'."\r\n";                  
    
        if (is_home() || is_front_page()) $ogUrl = get_bloginfo( 'url' ); else $ogUrl = 'http' . (is_ssl() ? 's' : '') . "://".$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $ogUrl = '<meta property="og:url" content="'.esc_url( apply_filters( 'nxsog_url', $ogUrl ) ) . '" />' . "\r\n";
    
        if (!is_home()) { /*
          $vidsFromPost = nsFindVidsInPost($post); if ($vidsFromPost !== false && is_singular()) {  echo '<meta property="og:video" content="http://www.youtube.com/v/'.$vidsFromPost[0].'" />'."\n";
          echo '<meta property="og:video:type" content="application/x-shockwave-flash" />'."\n";
          echo '<meta property="og:video:width" content="480" />'."\n";
          echo '<meta property="og:video:height" content="360" />'."\n";
          echo '<meta property="og:image" content="http://i2.ytimg.com/vi/'.$vidsFromPost[0].'/mqdefault.jpg" />'."\n";
          echo '<meta property="og:type" content="video" />'."\n";
        } */
          if (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
            $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
            $ogimgs[] = $thumbnail_src[0];
          }
          $imgsFromPost = nsFindImgsInPost($post, (int)$options['advFindOGImg']==1);
          if ($imgsFromPost !== false && is_singular() && is_array($ogimgs) && is_array($imgsFromPost))  $ogimgs = array_merge($ogimgs, $imgsFromPost);
        }       
    
        //## Add default image to the endof the array
        if ( count($ogimgs)<1 && isset($options['ogImgDef']) && $options['ogImgDef']!='') $ogimgs[] = $options['ogImgDef'];
        //## Output og:image tags
        $ogImgsOut = "";
        if (!empty($ogimgs) && is_array($ogimgs)) foreach ($ogimgs as $ogimage)  $ogImgsOut .= '<meta property="og:image" content="'.esc_url(apply_filters('ns_ogimage', $ogimage)).'" />'."\r\n";
        $ogOut  = "\r\n".$ogSN.$ogT.$ogD.$ogType.$ogUrl.$ogLoc.$ogImgsOut;
      } $content = str_ireplace('<!-- ## NXSOGTAGS ## -->', $ogOut, $content);
      return $content;
    }

    Hope this helps!

Viewing 1 replies (of 1 total)
  • The topic ‘Problem with Opengraph tags’ is closed to new replies.