fun with FB share of dynamic URL's

Started by drewactual, June 29, 2020, 11:46:52 AM

Previous topic - Next topic

drewactual

the simpler a share can be is more likely to be shared... that is the starting point and motivation here.  So if you use FB developer tools to create a share button, there is no way to make it share the title of the page (thread) and that threads URL; it functions with static pages, not dynamic pages... you gotta get clippy with their code which uses data collected from their OG: standards...

so...

i created a file a long while back to include that replaces the OG: data for each board and display view... it provides images specific to that board, and descriptions ect... i've recently changed it to attempt to dynamically collect the threads name and url so a person that simply hits the 'share' button doesn't have to hit anything else but the 'post' on the FB pop-up... like i said, trying to simplify it best i can...

the problem i'm encountering - and this is hacky which i don't like- is to retread the '$context[canonical_url] definition for both the page URL and the link (data href) provided to FB's sharer function...

it works nicely... but... it kicks back an error in the log for using undefined index 'canonical_url'... it populates it just fine, but kicks the error in the log...

the code i'm using now is in a separate included file on my index.template, which again checks the URL to change the OG appropriately.. it's:

if(( isset($_REQUEST['board']) && $_REQUEST['board'] == '5' ) || ( isset($board) && $board == '5' )) {
  echo '
    <meta name="keywords" content="...stuff...">
    <meta name="description" content="...stuff..." />
    <meta property="og:url" content="', $context['canonical_url'],'" />
    <meta property="og:type" content="website" />
    <meta property="og:title" content="', $context['page_title_html_safe'],'" />
    <meta property="og:description" content="...stuff..." />
    <meta property="og:image" itemprop="image" content="...image..." />
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:site" content="...handle..." />
    <meta name="twitter:title" content="...stuff..." />
    <meta name="twitter:description" content="...stuff..." />
    <meta name="twitter:image" content="...stuff..." />
    <link itemprop="thumbnailUrl" href="...stuff...">
   '; }


that is the first entry- followed by a list of 'elseifs' and then an 'else'. 
curiously, the line "og:title" DOES NOT kick an error, only "og:URL" does. 

how is this to be defeated?

should i simply check if the item is set using something like:

if (isset($context['canonical_url'])) {
     $output.=$context['canonical_url'];}

^BEFORE any of the 'else' or 'elseifs' or just once at the start of the file? or, is that the wrong approach altogether to make certain canonical_url is defined?

help please.... thanks!

drewactual

solved.... tentatively... we'll see...

added:

if (!empty($context['canonical_url']))
echo '
<meta property="og:url" content="' . $context['canonical_url'] . '" />';

above the big list of echo's... and it seems to have worked.

i just don't get how page_title doesn't kick an error and canonical_url does?


Advertisement: