Forum

Thread tagged as: Problem, Blog

Trouble with social sharing in blog

Hi guys,

So I'm trying to set up the facebook and twitter card sharing. I've tried following the solution docs on it and it is working great for the pages but I can't see any of the tags in the header when I view source and I'm not sure exactly what I'm doing wrong.

<?php 
    # get the post
    $result = perch_blog_custom(array(
        'section' => 'comic-pages',
        'filter' => 'postSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'template'   => 'blog/post_comic_page.html',
        'skip-template' => true,
        'return-html'   => true,
        )); 

    # set up the variables
    $title       = $post['0']['postTitle'];
    $description = strip_tags($post['0']['excerpt']);
    if (isset($post[0]['fbimage'])) {
        $fbimage = $post[0]['fbimage'];
    } else {
        $fbimage = '';
    }
    # use the variables in the array value 
    perch_page_attributes_extend(array(
        'description'    => $description,
        'og_description' => $description,
        'og_title'       => $title,
        'og_type'        => 'article',
        'sharing_image'  => $fbimage,
        'og_author'      => 'https://www.facebook.com/myfbname',
    ));
    # display the post
    echo $result['html'];
 ?>

Here is the header code...

<?php 
        $domain        = 'https://'.$_SERVER["HTTP_HOST"];
        $url           = $domain.$_SERVER["REQUEST_URI"];
        $sitename      = "Heart of Millyera";
        $twittername   = "@HeartOfMillyera";
        $sharing_image = '/images/social_tile_fallback_img.jpg';

        PerchSystem::set_var('domain',$domain);
        PerchSystem::set_var('url',$url);
        PerchSystem::set_var('sharing_image',$sharing_image);
        PerchSystem::set_var('twittername',$twittername);

        perch_page_attributes(array(        
          'template' => 'default.html'    
        ));
    ?>

Jana Hoffmann

Jana Hoffmann 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm going to run this up here and see. There's an alarmingly high number of people having problems with this, so it must be something we're doing wrong.

No problems. Happy to share my git repo with you if that helps at all, just let me know. :)

Hi Drew, sorry to bug you but is there any progress with this issue?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, Jana, I'll see if I can look at this tomorrow.

Sorry to bug you again Drew but the site I'm using this on is due to go live in a week, is there any word on this?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Sorry, Jana - I'll get something together for you when I'm back in the office on Tuesday.

no problem, thanks!

How'd you go Drew?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Literally on it now! I'm implementing it on my own blog to try and find the issue.

Great! Thanks so much.

Hey Drew how did you go?

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's all working flawlessly for me. Can I just double check you're calling perch_page_attributes_extend() before you use perch_page_attributes() ?

Oh I wasn't but I still can't get it to work. I have a global header and I have moved the extend and related to above the perch layout but It's still not working for me. :(

Do I need to pass variables into the global header template? Sorry my php is pretty beginner.

Here is my new extend.

Sorry for the delay in getting back to you. Australian/UK time difference makes it a bit hard :p

<?php include('../perch/runtime.php');

    # get the post
    $result = perch_blog_custom(array(
        'section' => 'comic-pages',
        'filter' => 'postSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'template'   => 'blog/post_comic_page.html',
        'skip-template' => true,
        'return-html'   => true,
        )); 

    # set up the variables
    $title       = $post['0']['postTitle'];
    $description = strip_tags($post['0']['excerpt']);
    if (isset($post[0]['fbimage'])) {
        $fbimage = $post[0]['fbimage'];
    } else {
        $fbimage = '';
    }
    # use the variables in the array value 
    perch_page_attributes_extend(array(
        'description'    => $description,
        'og_description' => $description,
        'og_title'       => $title,
        'og_type'        => 'article',
        'sharing_image'  => $fbimage,
        'og_author'      => 'https://www.facebook.com/myfbname',
    ));

    perch_layout('global.header', array(
        'title'           => perch_blog_post_field(perch_get('s'), 'postTitle', true) ,
    ));
?>

Header code is the same as above.

Drew McLellan

Drew McLellan 2638 points
Perch Support

And then global.header is calling perch_page_attributes()? Can you show me that part?

You mean this yes?

This part works fine on my normal pages, but doesn't display at all on the blog posts

    <?php 
        $domain        = 'https://'.$_SERVER["HTTP_HOST"];
        $url           = $domain.$_SERVER["REQUEST_URI"];
        $sitename      = "Heart of Millyera";
        $twittername   = "@HeartOfMillyera";
        $sharing_image = '/images/social_tile_fallback_img.jpg';

        PerchSystem::set_var('domain',$domain);
        PerchSystem::set_var('url',$url);
        PerchSystem::set_var('sharing_image',$sharing_image);
        PerchSystem::set_var('twittername',$twittername);

        perch_page_attributes(array(        
          'template' => 'default.html'    
        ));
    ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, it looks like you're overwriting what you've already set.

On the blog page, change this:

perch_layout('global.header', array(
        'title'           => perch_blog_post_field(perch_get('s'), 'postTitle', true) ,
    ));

to this:

perch_layout('global.header', array(
        'title'           => perch_blog_post_field(perch_get('s'), 'postTitle', true) ,
        'blog' => true,
    ));

That will set 'blog' as a layout var that we can then test for.

So, in global.header change this:

        $domain        = 'https://'.$_SERVER["HTTP_HOST"];
        $url           = $domain.$_SERVER["REQUEST_URI"];
        $sitename      = "Heart of Millyera";
        $twittername   = "@HeartOfMillyera";
        $sharing_image = '/images/social_tile_fallback_img.jpg';

        PerchSystem::set_var('domain',$domain);
        PerchSystem::set_var('url',$url);
        PerchSystem::set_var('sharing_image',$sharing_image);
        PerchSystem::set_var('twittername',$twittername);

to this:

    if (!perch_layout_var('blog', true)) {
        $domain        = 'https://'.$_SERVER["HTTP_HOST"];
        $url           = $domain.$_SERVER["REQUEST_URI"];
        $sitename      = "Heart of Millyera";
        $twittername   = "@HeartOfMillyera";
        $sharing_image = '/images/social_tile_fallback_img.jpg';

        PerchSystem::set_var('domain',$domain);
        PerchSystem::set_var('url',$url);
        PerchSystem::set_var('sharing_image',$sharing_image);
        PerchSystem::set_var('twittername',$twittername);
    }

This will only reset those variables if it's not a blog page.

I've done those changes but I still don't get any of the page attributes loading into the header. I added some comments and it looks like even though I'm setting blog to true, the header doesn't think I'm setting blog to true. :(

    <?php 

        if (!perch_layout_var('blog', true)) {
            $domain        = 'https://'.$_SERVER["HTTP_HOST"];
            $url           = $domain.$_SERVER["REQUEST_URI"];
            $sitename      = "Heart of Millyera";
            $twittername   = "@HeartOfMillyera";
            $sharing_image = '/images/social_tile_fallback_img.jpg';

            PerchSystem::set_var('domain',$domain);
            PerchSystem::set_var('url',$url);
            PerchSystem::set_var('sharing_image',$sharing_image);
            PerchSystem::set_var('twittername',$twittername);
            echo "<!-- yay blog -->";
        } else {
            echo "<!-- no blog for you -->";
        }

        perch_page_attributes(array(        
          'template' => 'default.html'    
        ));
    ?>


<?php include('../perch/runtime.php'); # get the post $result = perch_blog_custom(array( 'section' => 'comic-pages', 'filter' => 'postSlug', 'match' => 'eq', 'value' => perch_get('s'), 'template' => 'blog/post_comic_page.html', 'skip-template' => true, 'return-html' => true, )); # set up the variables $title = $post['0']['postTitle']; $description = strip_tags($post['0']['excerpt']); if (isset($post[0]['fbimage'])) { $fbimage = $post[0]['fbimage']; } else { $fbimage = ''; } # use the variables in the array value perch_page_attributes_extend(array( 'description' => $description, 'og_description' => $description, 'og_title' => $title, 'og_type' => 'article', 'sharing_image' => $fbimage, 'og_author' => 'https://www.facebook.com/myfbname', )); perch_layout('global.header', array( 'title' => perch_blog_post_field(perch_get('s'), 'postTitle', true) , 'blog' => true, )); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Those comments you've added are the wrong way around. <!-- no blog for you --> will show when you're on a blog page.

Ok so I had a friend who is more experienced with PHP but not perch try and help me out. We kinda got it to work but it seems pretty hacky. Is there a working git example online somewhere?

We had to add an index.php page and a blog/post.php into the admin, add page titles, images etc to those pages and then the blog meta tags would show up(not the ones on the page equivalent), but the site title doesn't show up on either index.php or post.php and none of the images are working for post.php pages. :( Site title whatever, I can just hard code, but I do really need to get the images working and I'm not sure why they aren't when the descriptions are.

post.php

<?php include('../perch/runtime.php');

    # get the post
    $result = perch_blog_custom(array(
        'section' => 'comic-pages',
        'filter' => 'postSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'template'   => 'blog/post_comic_page.html',
        'skip-template' => true,
        'return-html'   => true,
        )); 

    # set up the variables
    $title       = $result['0']['postTitle'];
    $description = substr($result[0]['postDescRaw'], 0, 150) . '...';
    if (isset($result[0]['perch_teaserImage'])) {
        $fbimage = $result[0]['perch_teaserImage'];
    } else {
        $fbimage = '';
    }
    # use the variables in the array value 
    perch_page_attributes_extend(array(
        'description'    => $description,
        'og_description' => $description,
        'og_title'       => $title,
        'og_type'        => 'article',
        'sharing_image'  => $fbimage,
        'og_author'      => 'https://www.facebook.com/heartofmillyera',
    ));

    perch_layout('global.header', array(
        'title'           => perch_blog_post_field(perch_get('s'), 'postTitle', true) ,
    ));
?>

global.header.php

<?php
    $domain        = 'https://'.$_SERVER["HTTP_HOST"];
    $url           = $domain.$_SERVER["REQUEST_URI"];
    $sitename      = "Heart of Millyera";
    $twittername   = "@HeartOfMillyera";
    $sharing_image = '/images/social_tile_fallback_img.jpg';

    PerchSystem::set_var('domain',$domain);
    PerchSystem::set_var('url',$url);
    PerchSystem::set_var('sharing_image',$sharing_image);
    PerchSystem::set_var('twittername',$twittername);

    perch_page_attributes(array(        
        'template' => 'default.html'    
    ));
?>

facebook.html

<meta property="og:site_name" content="<perch:pages id="sitename" type="hidden" />" />
<meta property="og:url" content="<perch:pages id="url" type="hidden" />" />
<meta property="og:title" content="<perch:pages id="og_title" label="Social title" type="text" escape="true" help="Title for this document with no branding or site name" divider-before="Facebook Open Graph Tags" />" />
<meta property="og:description" content="<perch:pages id="og_description" label="Social description" type="textarea" size="s" escape="true" />" />
<perch:if exists="og_image">
<meta property="og:image" content="<perch:pages id="domain" type="hidden" /><perch:pages id="og_image" label="Image when shared" help="Should be at least 1200x630" type="image" width="1200" />" />
<perch:else />
<meta property="og:image" content="<perch:pages id="domain" type="hidden" /><perch:pages id="sharing_image" type="hidden" />" />
</perch:if>
<perch:if exists="og_type">
<meta property="og:type" content="<perch:pages id="og_type" label="Facebook type" type="select" options="article,book,profile,website,video,music" allowempty="true" />" />
</perch:if>
<perch:if exists="og_author">
<meta property="article:author" content="<perch:pages id="og_author" type="hidden" />" />
</perch:if>

View source output...

<meta name="description" content="Jess and I have been working on these pages for almost a year now and I put a lot of pressure on myself to make the right design decisions in these fi..." />
<!-- <meta name="keywords" content="" /> -->
<meta name="robots" content="" />
<meta property="og:site_name" content="Heart of Millyera" />
<meta property="og:url" content="https://heartofmillyera.com/blog/2015-09-05-page-4" />
<meta property="og:title" content="Page 4" />
<meta property="og:description" content="Jess and I have been working on these pages for almost a year now and I put a lot of pressure on myself to make the right design decisions in these fi..." />

<meta property="og:image" content="https://heartofmillyera.com/perch/resources/socialtilefallbackimg-1-w1200.jpg" />


<meta property="og:type" content="article" />


<meta property="article:author" content="https://www.facebook.com/heartofmillyera" />

<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="@HeartOfMillyera" />
<meta name="twitter:title" content="Page 4" />
<meta name="twitter:description" content="Jess and I have been working on these pages for almost a year now and I put a lot of pressure on myself to make the right design decisions in these fi..." />

<meta property="twitter:image" content="https://heartofmillyera.com/perch/resources/socialtilefallbackimg-1-w1200.jpg" />

<meta name="twitter:url" content="https://heartofmillyera.com/blog/2015-09-05-page-4" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

We've just released Blog 5.0 to make this simpler. It has a dedicated section for entering Facebook and Twitter meta data, and a simple function to output it all in the head of your page.

Blog: https://grabaperch.com/add-ons/apps/blog

New page function: https://docs.grabaperch.com/addons/blog/page-functions/perch-blog-post-meta/