Plugins & Themes

HTTP to HTTPS: recovering Facebook Likes

2 Replies ·

  1. Hi,

    I recently move my Wordpress site to HTTPS. I’d like to move Facebook to still fetch the HTTP version so it displays the likes (otherwise, after the move, count goes to zero). I’m not using a widget: I coded the Facebook button myself quite a while ago. There are various solutions online, but I’m looking to fix this myself, without using yet another plugin. If anyone went through this or has an idea as to what I should modify (or what filter I should introduce in my functions.php file, I’d appreciate. Such a possible filter is describe at the end of this post: https://really-simple-ssl.com/knowledge-base/how-to-recover-facebook-likes-after-moving-to-httpsssl/

    And here’s the full code for my Facebook button (I have anonymised it, somehow):

    /***Facebook Open Graph Protocol- START (en plus de ça j'ai modifié le header en le copiant du dossier du thème Hybrid dans le dossier du thème Aphelis: le header d'Aphelis (child theme) override le header du thème parent)
    */
    
    /* ici une fonction pour indiquer à Facebook de trouver la première image sur la page permanente d'un billet, avec une image par défaut s'il ne trouve pas*/
    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
    
      if(empty($first_img)){ //Defines a default image
        $first_img = "http://mywebsite.net/wp-content/themes/mytheme/images/fb_quote.png";
      }
      return $first_img;
    }
    /*fin de la fonction pour l'image*/
    function add_facebook_open_graph_tags() {
        if (is_single() || is_page()) {
        global $post;
        
        ?>
    <meta property="og:title" content="<?php the_title(); ?>"/>
    <meta property="og:type" content="article"/>
    <meta property="og:image" content="<?php echo catch_that_image() ?>"/>
    <meta property="og:url" content="<?php the_permalink(); ?>"/>
     <?php
     function og_meta_desc() {
     global $post;
     $meta = strip_tags($post->post_content);
     $meta = str_replace(array("n", "r", "t"), ' ', $meta);
     $meta = substr($meta, 0, 200);
     echo "<meta property="og:description" content="$meta"/>";
     }
     og_meta_desc();
     ?>
    <meta property="og:site_name" content="<?php echo get_bloginfo('name'); ?>"/>
    <meta property="fb:admins" content="00000000000000"/>
    <meta property="fb:app_id" content="00000000000000" />
        <?php }
    }
    add_action('wp_head', 'add_facebook_open_graph_tags', 99);
    /**Facebook Open Graph Protocol- STOP
    */
    
    /**Facebook JavaScriptSDK- START *** attention d'indiquer le bon URL de l'emplacement du fichier channel.html qui doit être placé dans le root folder de l'installation
    */
    function add_facebook_sdk() {
        if (is_single() || is_page()) { ?>
    <div id="fb-root"></div>
    <div id="fb-root">
      <!-- you must include this div for the JS SDK to load properly -->
    </div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '00000000000000', // App ID
          channelUrl : '//mywebsite.net/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });
    
        // Additional initialization code here
      };
    
      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         ref.parentNode.insertBefore(js, ref);
       }(document));
    </script>
        <?php }
    }
    add_action('hybrid_before_html', 'add_facebook_sdk');
    //Facebook JavaScriptSDK- STOP
    
    //Facebook JavaScript SDK: début script PHP pour mis en cache du fichier channel.html qui optimise le chargement du boutton FB - j'en exclus la page des Random Post dont le ID est 0000 autrement casse
    add_action( 'wp_head', 'my_facebook_head' );
    
    function my_facebook_head() {
    
        if ( 0000 == get_queried_object_id() )
            return;
    
        $cache_expire = 60*60*24*365;
        header("Pragma: public");
        header("Cache-Control: max-age=".$cache_expire);
        header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$cache_expire) . ' GMT'); ?>
    
    <script src="//connect.facebook.net/en_US/all.js"></script>
    
    <?php }
    //Facebook JavaScript SDK: FIN script PHP
    
    //ADD iFrame FACEBOOK LIKE BUTTON TO BOTTOM OF EVERY POST IN HOME SINGLE AND RANDOM-POST PAGE *** ATTENTION pour le bon app ID number *** ausi 0000 est le page ID de mes posts random
    add_action( 'hybrid_after_entry', 'my_facebook_link', 11 );
    
    function my_facebook_link() {
    
        if ( is_home() || is_singular( 'post' ) || 0000 === get_queried_object_id() ) {
    
            $social = '

    '; $social .= '<iframe src="//www.facebook.com/plugins/like.php?href=' . get_permalink() . '&send=false&layout=button_count&width=140&show_faces=false&action=recommend&colorscheme=dark&font&height=21&appId=00000000000000" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:140px; height:21px;" allowTransparency="true"></iframe>'; $social .= '

    '; echo $social; } }
  2. Justin Tadlock

    Using a plugin really is the best method for handling social sharing. Plugin authors have to study and familiarize themselves with the APIs to make a great plugin.

    The filter described in that post is on a hook in the Really Simple SSL plugin, so it wouldn’t really apply to anything you’re doing unless you’re using that plugin.

    Here’s what Facebook has to say on the subject of “moving” URLs (changing to https falls under this): https://developers.facebook.com/docs/plugins/faqs#moving-urls

    Click on the question named “How do I move a page to a different URL?” It describes the steps you have to take to continue using the old URL as the canonical source for likes.

  3. Philippe

    8 months later… thanks Justin. Going with a plugin makes total sense, since I’m not a developer and I can’t quite (or hardly) keep up with all the development APIs are going through. Happy summer to you.

  4. Justin Tadlock

    Perhaps the biggest thing from your code is going to be this:

    <meta property="og:url" content="<?php the_permalink(); ?>"/>
    

    You’ll need to change the URL there (at least for posts prior to going to https). Something along the lines of:

    <?php $url = str_replace( 'https://', 'http://', get_permalink() ); ?>
    
    <meta property="og:url" content="<?php echo esc_url( $url ); ?>"/>
    
  5. Justin Tadlock

    Well, something went very wrong with this thread. Somehow, replies got out of order. Must be because it was an old thread on the old system. 🙁