Facebook dynamic Open Graph tags for image gallery

3.1k Views Asked by At

I have a Javascript image gallery and wanted to be able to share individual images on FB but found out that the Open Graph tags need to be static for FB to scrape, changing them on the fly with JS does not work.

I found this post Generating Facebook Open Graph meta tags dynamically and added some Javascript redirections.

Basically when a user clicks on my static FB Share button, title, description, image and redirect parameters are send to the page and the OG tags written. Than I do a JS self.location to the actual https://www.facebook.com/sharer/sharer.php?u=&t=

Once the image is posted on FB and the users clicks on the image they are taken back to the same page with another redirect pulled from the redirect parameter.

  • Everything works except that the image does not show up in the FB sharer dialog. Once posted on FB it shows fine though. Also in the Open Graph Object Debugger the image shows up fine, but not in the sharer dialog. Any Idea why?

  • I was also wondering about my dynamic OG template with the redirects, is there anything that could be improved? What about security etc?

    <?php
    
        // https://stackoverflow.com/questions/8431694/generating-facebook-open-graph-meta-tags-dynamically
    
        $params = array();
        if ( count($_GET) > 0) {
            $params = $_GET;
        } else {
            $params = $_POST;
        }
    
        // defaults
        if ( $params['locale'] == "" ) $params['locale'] = "en_US";
        if ( $params['type'] == "" ) $params['type'] = "article";
    
        if ( $params['title'] == "" ) $params['title'] = "My Awsome Website";
        if ( $params['description'] == "" ) $params['description'] = "The default description of my website";
        if ( $params['image'] == "" ) $params['image'] = "http://www.mywebsite.com/logo.png";
        if ( $params['redirect'] == "" ) $params['redirect'] = "http://www.mywebsite.com/";
    
        $query_str =    'locale='       . $params['locale'] . 
                        '&type='        . $params['type'] . 
                        '&title='       . $params['title'] . 
                        '&description=' . $params['description'] . 
                        '&image='       . $params['image'] . 
                        '&redirect='    . $params['redirect'];
    
        $fb_param_u = urlencode( 'http://www.mywebsite.com/share/facebook/?' . $query_str );
        $fb_param_t = rawurlencode( strip_tags( $params['title'] ) );
    
    ?>
    
    <!DOCTYPE html>
    <html lang="en-US" prefix="og: http://ogp.me/ns#">
    
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    
            <!-- Open Graph meta tags -->
            <meta property="og:site_name" content="My Website"/>
            <meta property="og:locale" content="<?php echo $params['locale']; ?>"/>
            <meta property="og:type" content="<?php echo $params['type']; ?>"/>
    
            <meta property="og:url" content="http://www.mywebsite.com/share/facebook/?<?= $query_str ?>" />
    
            <meta property="og:title" content="<?php echo $params['title']; ?>"/>
            <meta property="og:description" content="<?php echo $params['description']; ?>"/>
            <meta property="og:image" content="<?php echo $params['image']; ?>"/>
    
            <script type="text/javascript">
                var matches = document.referrer.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
                var domain_referrer = matches && matches[1];
    
                // when clicking through on FB share button
                if (domain_referrer == 'www.mywebsite.com') {
                    self.location = "https://www.facebook.com/sharer/sharer.php?u=<?= $fb_param_u ?>&t=<?= $fb_param_t ?>";
                } else {
                // redirect incomming links from FB
                    self.location = '<?php echo $params["redirect"]; ?>';
                }
            </script>   
        </head>
    
        <body>
            <div style="/*display:none*/">
                <img src="<?= $params['image'] ?>" />
            </div>
        </body>
    
    </html>
    
1

There are 1 best solutions below

0
On

Try this

<?php

$params = array();
if(count($_GET) > 0) {
    $params = $_GET;
} else {
    $params = $_POST;
}
// defaults
if($params['type'] == "") $params['type'] = "restaurant";
if($params['locale'] == "") $params['locale'] = "en_US";
if($params['title'] == "") $params['title'] = "default title";
if($params['image'] == "") $params['image'] = "thumb";
if($params['description'] == "") $params['description'] = "default description";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# MY_APP_NAME_SPACE: http://ogp.me/ns/fb/MY_APP_NAME_SPACE#">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

        <!-- Open Graph meta tags -->
        <meta property="fb:app_id" content="MY_APP_ID" />
        <meta property="og:site_name" content="meta site name"/>
        <meta property="og:url" content="http://URL/index.php?type=<?php echo $params['type']; ?>&locale=<?php echo $params['locale']; ?>&title=<?php echo $params['title']; ?>&image=<?php echo $params['image']; ?>&description=<?php echo $params['description']; ?>"/>
        <meta property="og:type" content="MY_APP_NAME_SPACE:<?php echo $params['type']; ?>"/>
        <meta property="og:locale" content="<?php echo $params['locale']; ?>"/>
        <meta property="og:title" content="<?php echo $params['title']; ?>"/>
        <meta property="og:image" content="http://URL/img/<?php echo $params['image']; ?>.png"/>
        <meta property="og:description" content="<?php echo $params['description']; ?>"/>

    </head>
</html>