0

Have a function I use to print Open Graph tags in the header of each page I also have a plugin I use to post to Facebook which also puts in it's own set of basic OG Tags. Facebook doesn't like if a page as more than one og:url tag. The plugin only works on single pages so I want to not print my og:url tag when it is on a single post page. I want to test for is_home and is_tag and if those are true the function will print the og:url tag. The other problem is the home page has a completely different url than the tag page. I tried doing the conditional inside the meta tag but Facebook vomits if the og:url line even exists.

Here is the code I currently have and everything except the conditional for the og:url tag is working:

    function wpc_fb_opengraph() {
$wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(),     'large');
?>
<?php echo "\n<!-- Start of OG Tags from DougTheme 2013 -->\n"; ?>
<meta property="og:image" content="<?php if (function_exists('catch_that_image')) {echo catch_that_image(); }?>" />
<meta property="fb:admins" content="XXXXX"/>
<meta property="fb:app_id" content="XXXXXXXXXX">
<meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
<meta property="og:description" content="<?php if ( is_singular() ) { echo strip_tags(get_the_excerpt()); } else { echo "comments about everything"; } ?>" />
<meta property="og:title" content="<?php if ( is_singular()) { echo esc_attr( get_the_title() ); } elseif (function_exists('is_tag') && is_tag()) {single_tag_title('Tag Archive for &quot;'); echo '&quot; - '; echo get_bloginfo('name');} else { echo get_bloginfo('name'); } ?>">
<meta property="og:type" content="<?php if ( is_singular() ) { echo "article"; } else { echo "website";} ?>">
        if (is_home()) {
        <meta property="og:url" content="<?php bloginfo('url'); ?>" />
<?php } //end if home

elseif ( is_tag() ) {
    <meta property="og:url" content="<?php echo $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];  ?>">
<?php }      


<?php echo "\n<!-- End of OG Tags from DougTheme 2013 -->\n"; ?>
<?php }

add_action('wp_head', 'wpc_fb_opengraph');

I get a syntax error in either of the lines testing for the page. I'm stumped.

0

From what I understand, you need help framing your if logic? And the php tags usage dont look right either.

Does this help -

<?php 
if(!is_single()){
    if(is_home() || is_front_page()){ // not sure if you have set a static page as your front page
        echo '<meta property="og:url" content="'.bloginfo('url'); .'" />';
    }elseif(is_tag()){
        echo '<meta property="og:url" content="'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; .'">';
        }
}
?>

=================== This is the code in entirety ========================

    function wpc_fb_opengraph() {
$wpc_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(),     'large');
?>
<?php echo "\n<!-- Start of OG Tags from DougTheme 2013 -->\n"; ?>
<meta property="og:image" content="<?php if (function_exists('catch_that_image')) {echo catch_that_image(); }?>" />
<meta property="fb:admins" content="XXXXX"/>
<meta property="fb:app_id" content="XXXXXXXXXX">
<meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
<meta property="og:description" content="<?php if ( is_singular() ) { echo strip_tags(get_the_excerpt()); } else { echo "comments about everything"; } ?>" />
<meta property="og:title" content="<?php if ( is_singular()) { echo esc_attr( get_the_title() ); } elseif (function_exists('is_tag') && is_tag()) {single_tag_title('Tag Archive for &quot;'); echo '&quot; - '; echo get_bloginfo('name');} else { echo get_bloginfo('name'); } ?>">
<meta property="og:type" content="<?php if ( is_singular() ) { echo "article"; } else { echo "website";} ?>">
if(!is_single()){
    if(is_home() || is_front_page()){ // not sure if you have set a static page as your front page
        echo '<meta property="og:url" content="'.bloginfo('url'); .'" />';
    }elseif(is_tag()){
        echo '<meta property="og:url" content="'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; .'">';
        }
}    

<?php echo "\n<!-- End of OG Tags from DougTheme 2013 -->\n"; ?>
<?php }    

add_action('wp_head', 'wpc_fb_opengraph');
  • When I put that code into my function I got a syntax error on the first meta property line for an unexpected '.' – Doug B. Feb 10 '14 at 18:26
  • How do I merge that logic code into my function - that is the problem I'm having. The conditionals inside the meta property lines work fine but when I try to put the conditional outside of that statement I get syntax errors. – Doug B. Feb 10 '14 at 18:38
  • It is printing the if else line at the top of my page so at least no syntax error. Here is a link to my page that I want this to work on. is.gd/s2ufPe – Doug B. Feb 11 '14 at 9:09
  • Go it to work by making some minor changes. Thanks for the leading me to the solution. I appreciate it. – Doug B. Feb 11 '14 at 22:16
0

Here is the corrected code to solve my issue.

    <?php if(!is_single()){
if(is_home() || is_front_page()){ // not sure if you have set a static page as your front page
    echo '<meta property="og:url" content="'.get_bloginfo('url').'" />';
}elseif(is_tag()){
    echo '<meta property="og:url" content="http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"].' ">';
    }

} ?>

I added this block to my function and it does what I need it to do - write the url of the page depending if it was the home page or a tag page.

The final issue was getting syntax errors with the data inside the echo statements so I used a PHP code checker to knock those down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.