chuckreynolds / WPFBOGP

WordPress Open Graph plugin development
wordpress.org/plugins/wp-facebook-open-graph-protocol/
13 stars 19 forks source link

wpfbogp_callback preg_replace fix #56

Closed Kevin-Hamilton closed 8 years ago

Kevin-Hamilton commented 9 years ago

fix og:title and og:description preg_replace calls in the output buffering callback

Kevin-Hamilton commented 9 years ago

The code as written was never running the replace, because the search string wasn't finding a match since you updated to self-close meta tags in commit d3937de

chuckreynolds commented 9 years ago

I still don't think it is after this change either...

Kevin-Hamilton commented 9 years ago

It's working for me in production. What do you see as the issue with the PR?

chuckreynolds commented 9 years ago

@Kevin-Hamilton mabye I'm testing it wrong but I don't see the filtering pulling any titles or descriptions put in place first by seo plugins. With your PR or not I'm not seeing it working; meaning I'm either testing it wrong or it's been broke for a while; which concerns me.

Kevin-Hamilton commented 9 years ago

Hm, I'm not sure where my comment from earlier went - I must have hit preview and not hit submit.

It turns out I was confused about where the og meta tags were coming from on my site. They were not coming from any SEO plugin, but rather from some functions in the theme. Below are the functions in question if you want to reproduce.

I updated the PR with some better RegExp that makes the self-closing optional, and deals with any whitespace between the attribute and the tag ending. It also fixes a "greedy dot-all" issue with the content attribute itself.

//Adding the Open Graph in the Language Attributes
function add_opengraph_doctype( $output ) {
        return $output . ' xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"';
    }
add_filter('language_attributes', 'add_opengraph_doctype');

//Lets add Open Graph Meta Info
function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) //if it is not a post or a page
        return;
        echo '<meta property="og:title" content="' . get_the_title() . '"/>';
        echo '<meta property="og:type" content="article"/>';
        echo '<meta property="og:url" content="' . get_permalink() . '"/>';
        echo '<meta property="og:site_name" content="' . get_option( 'blogname' ) . '"/>';
    if(!has_post_thumbnail( $post->ID )) { //the post does not have featured image, use a default image
        $default_image=get_option( 'siteurl' )."/images/Logo.png"; //replace this with a default image on your server or an image in your media library
        echo '<meta property="og:image" content="' . $default_image . '"/>';
    }
    else{
        $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
        echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
    }
    echo "\n";
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );