Forum

Thread tagged as: Problem, Docs

Facebook OG / Twitter Cards using variables

Hi,

I'm having issues implementing this solution: https://solutions.grabaperch.com/integrations/facebook-and-twitter-sharing

Adding content via the Page Details tab in the perch back end works fine. It's the passing of variables in the head that is not working.

I've basically copied and pasted the code from Rachel's example and get the following output:

<meta property="og:site_name" content="" />
<meta property="og:url" content="" />
<meta property="og:title" content="Home Page" />
<meta property="og:description" content="Rid your life of clutter with &#039;Declutter Me&#039; the London based service that offers hands on practical help organising your life." />

<meta property="og:image" content="" />


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


<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="" />
<meta name="twitter:title" content="Home Page" />
<meta name="twitter:description" content="Rid your life of clutter with &#039;Declutter Me&#039; the London based service that offers hands on practical help organising your life." />

<meta name="twitter:url" content="" />

The docs page: https://docs.grabaperch.com/docs/templates/passing-variables-into-templates/

...states that variable can only be passed when using templates which are parsed at runtime – such as content rendered with perch_content_custom() rather than the precompiled output from perch_content().

Could this be the culprit as to why the variables are not appearing in the output?

Thanks

Adam Menczykowski

Adam Menczykowski 1 points

  • 7 years ago

Here is the php code:

    <?php 
    $domain        = 'https://'.$_SERVER["HTTP_HOST"];
    $url           = $domain.$_SERVER["REQUEST_URI"];
    $sitename      = "Declutter Me";
    $twittername   = "@SalDeclutterMe";
    $sharing_image = '/img/Declutter_Me.gif';

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

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

Simon Clay 127 points

Perhaps try perch_page_attributes_extend :

<?php 
    $domain        = 'https://'.$_SERVER["HTTP_HOST"];
    $url           = $domain.$_SERVER["REQUEST_URI"];
    $sitename      = "Declutter Me";
    $twittername   = "@SalDeclutterMe";
    $sharing_image = '/img/Declutter_Me.gif';

    perch_page_attributes_extend(array(
            'domain' => $domain,
            'sitename' => $sitename,
            'url' => $url,
            'sharing_image' => $sharing_image,
            'twittername' => $twittername
        ));

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

Just tried that and it broke the site at that point in the output for some reason. Thanks though

It seems that using perch_page_attributes_extend at all breaks the page.

Any ideas why that would be?

Thanks :-)

Simon Clay

Simon Clay 127 points

Yes, not sure. Over to Drew and Rachel.

Like you, I was finding that PerchSystem::set_var wasn't passing any content into page attributes, but perch_page_attributes_extend worked for me.

Can you share the full code please simon?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you running 2.7.4?

2.7.3 so I'll upgrade and see

Simon Clay

Simon Clay 127 points

It wasn't for twitter card, but same principle I think.

<?php 

        //  get the current product info
        $product_details = perch_content_custom('Product Details', array(
             'page' => '/our-range/products.php',
             'filter' => 'slug',
             'match' => 'eq',
             'skip-template' => 'true',
             'return-html'   => 'true',
        ));

        //  set the variable
        $product_description = $product_details['0']['summary'];

        //  send it to Page Attributes  
        perch_page_attributes_extend(array(
            'description' => $product_description
        ));

        //  call Page Attributes
        perch_page_attributes(); 
    ?>

I see that perch_page_attributes_extend is new to 2.7.4 - just backing up now ready to upgrade.

:-)

Updating perch has fixed it. Since perch_page_attributes_extend() wasn't a feature until 2.7.4 that explains it. Thanks for your helpful alternative Simon.

Simon Clay

Simon Clay 127 points

Great :)