Facebook counter not working, Please help me to fix.
-
Hi,
Facebook counter does not work, It displaying 0.
Please help me to fix.Thanks
-
I’m working on it
Hello,
Any response on this?My Facebook counter shows ZERO too…
@davidrgz I’m working on it. Feel free to help me in https://github.com/claudiosmweb/social-count-plus/ or in https://bit.ly/1HhfCkO
I was able to implement a workaround on my WordPress installation, albeit hardcoded. A Facebook app is required for this to work. Please note that there might be an issue with Facebook API v2.4 which results in the necessary data not being returned (only “id” and “name” are returned), so until they fix this, you need to make use of an app that was created prior to 2.4, so creating a new Facebook app right now will NOT work at this point. You can see API version in the Dashboard at https://developers.facebook.com
I’ve created this simple tool in PHP to create a long-lived access token which should never expire (app in development mode) or in 2 months (approved app in public/production mode). It requires the PHP Facebook SDK which you can get from https://github.com/facebook/facebook-php-sdk-v4/archive/4.0-dev.zip
Upload the script and the SDK files to a server. You may modify the authentication scopes as desired. In the Facebook app settings, configure the “Valid OAuth redirect URIs” (under Settings > Advanced) to point to the URL of the PHP file. So for example, if you access the script at https://localhost/tokenhelper.php, then that should also be your redirect URL. These URLs must match exactly.
<?php session_start(); function get_value($key) { if (isset($_POST[$key])) { return $_POST[$key]; } if (isset($_SESSION[$key])) { return $_SESSION[$key]; } return ''; } function set_value($key, $value) { $_SESSION[$key] = $value; return $value; } ?> <html> <head> <title>Obtain Access Token</title> </head> <body> <?php define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook-php-sdk-v4-4.0-dev/src/Facebook/'); require __DIR__ . '/facebook-php-sdk-v4-4.0-dev/autoload.php'; use Facebook\Entities\AccessToken; use Facebook\FacebookSession; use Facebook\FacebookRedirectLoginHelper; use Facebook\FacebookRequest; $redirectUrl = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; if (isset($_POST['get_tokens'])) { $appId = set_value('app_id', $_POST['app_id']); $appSecret = set_value('app_secret', $_POST['app_secret']); FacebookSession::setDefaultApplication($appId, $appSecret); $helper = new FacebookRedirectLoginHelper($redirectUrl); $userToken = trim($_POST['user_token']); if (strlen($userToken) == 0) { // Obtain a new user token $scopes = array('manage_pages'); $loginUrl = $helper->getLoginUrl($scopes); echo "Login at <a href=\"$loginUrl\" target=\"_blank\">$loginUrl</a> to obtain a user access token.<br /><br />"; } } if (isset($_GET['code']) && strlen(trim($_GET['code'])) > 0) { $appId = get_value('app_id'); $appSecret = get_value('app_secret'); FacebookSession::setDefaultApplication($appId, $appSecret); $helper = new FacebookRedirectLoginHelper($redirectUrl); $session = $helper->getSessionFromRedirect(); // User access token obtained $accessToken = $session->getAccessToken(); $longLivedAccessToken = $accessToken->extend(); $request = new FacebookRequest($session, 'GET', '/me/accounts?fields=name,access_token,perms'); $pageList = $request->execute()->getGraphObject()->asArray(); set_value('user_token', (string) $accessToken); set_value('long_user_token', (string) $longLivedAccessToken); set_value('page_tokens', print_r($pageList, true)); header('Location: token.php'); } ?> <form method="post" action="token.php"> App ID <br /><input type="text" name="app_id" value="<?php echo get_value('app_id') ?>" /><br /><br /> App Secret <br /><input type="text" name="app_secret" value="<?php echo get_value('app_secret') ?>" /><br /><br /> User Token <br /><input type="text" name="user_token" value="<?php echo get_value('user_token') ?>" size="50" /><br /><br /> Long-term User Token <br /><input type="text" name="long_user_token" value="<?php echo get_value('long_user_token') ?>" size="50" /> <br /><br /> <textarea rows="10" cols="80"><?php echo get_value('page_tokens') ?></textarea><br /><br /> <input type="submit" name="get_tokens" value="Get Tokens" /> </form> </body> </html>
Once you get the results, you will see the list of pages that you have access to along with their corresponding access tokens and IDs in the textarea field.
To verify your access tokens, paste them to the Access Token debugger at https://developers.facebook.com/tools/debug/access_token
This will show you the expiration time, allowed scopes and other information.Once that’s complete, you’ll need to edit the plugin files. Go to your WordPress Dashboard > Plugins > Editor. Select the “Social Count Plus” plugin from the dropdown and edit the social-count-plus/includes/counters/class-social-count-plus-facebook-counter.php file (If you don’t see it right away, click on the social-count-plus/includes/social-count-plus-functions.php file first).
Look for the line:
protected $api_url = 'https://graph.facebook.com/';
Change it to
protected $api_url = 'https://graph.facebook.com/v2.3/';
Look for the line:
$this->connection = wp_remote_get( $this->api_url . $settings['facebook_id'], $params );
and change it to:
$this->connection = wp_remote_get( $this->api_url . $settings['facebook_id'] . '?access_token=<access_token_here>', $params );
where <access_token_here> is the access token that you obtained for the Facebook page.Save the file and run a System report (WordPress Dashboard > Settings > Social Count Plus > System Status > Get System Report), and you will see the results for the Facebook request (assuming Facebook is enabled and a numeric Facebook ID) is set.
Hope this helps until there’s a proper fix from the plugin author. ??
Thank you @intricateware !
It worked, only in my case I had to also change
if ( isset( $_data['likes'] ) ) { $count = intval( $_data['likes'] );
to
if ( isset( $_data['country_page_likes'] ) ) { $count = intval( $_data['country_page_likes'] );
I hade the exact same issue.
Following is the steps i followed :
1. I created a facebook app from [Please note you have to login with the exact same user who is the owner of the fb page]. You will need to register.
Go to -> https://developers.facebook.com/apps/
– Select the website option once the screen for “Add a new App” is presented.
– Set the name for your app and click on “Create a new app id”.
– Choose Category as “Apps for Pages” and click on “Create App Id”.2. Generated a temperarory request token from ->
https://developers.facebook.com/tools/explorer/
– Go to above link and select application as “<Your app name created in step 1>”
– Click on “Get Token” Drop down.
– From Drop down click “Get Access Token” .
– Select “user_likes” from User Data Permissions and from extended permisssions select “manage_pages”.
– Click Get Access Token.
– Click “Okay” on the next two screens.
– Click the “Get Access Token” again and select your Page listed in the drop down. The token will change in access token section. Copy the same, it will be required in the next step.3. Extended the temp token [Created above in last step] by replacing the appropriate values after viewing the app created in step 1 [View the app from same https://developers.facebook.com/apps/%5D. Click on Show button to view the app secret.
https://graph.facebook.com/oauth/access_token?client_id=<app Id>&redirect_uri=telecomfundamentals.com&client_secret=<app secret>&grant_type=fb_exchange_token&fb_exchange_token=<temp token>&redirect_uri=<your website>&
– Copy the access token createed on the screen. It should also show some value like -> &expires=5183999
4. Logout from facebook and try to access the graph api and check if Likes can be fetched in offline mode. To do this put the following link in browser URL.
https://graph.facebook.com/<page id>?fields=likes&access_token=<New Token Copied from Step 3>.
5. If step 4 works fine you are all set. Put the following link in the SocialCountPlus Plugin Settings.
Facebook Page ID = <FB page Id>?fields=likes&access_token=<token from step 3>
– Click update and reload your website the count should load properly.
I have successfully implement this steps at https://telecomfundamentals.com/.
Br,
SatheeshSatheesh- you da man. Awesome!
Awesome indeed, it worked like a charm for me.
For others that might miss it, there is a “telecomfundamentals.com” left in the string in step 3 that should be replaced by the site url.Thanks a million Satheeshgecp !!
You’re a magician!
Awesome !!!!I have an easy fix use fql query instead of getting with id.
Go to wp-content/plugins/social-count-plus/includes/counters/class-social-count-plus-facebook-counter.php
Try to replace functionpublic function get_total( $settings, $cache ) { }
with below :-
public function get_total( $settings, $cache ) { if ( $this->is_available( $settings ) ) { $params = array( 'sslverify' => false, 'timeout' => 60 ); $this->connection = wp_remote_get( 'https://graph.facebook.com/fql?q=SELECT%20like_count,%20total_count,%20share_count,%20click_count,%20comment_count%20FROM%20link_stat%20WHERE%20url%20=%20%22'.$settings['facebook_id'].'%22', $params ); if ( is_wp_error( $this->connection ) ) { $this->total = ( isset( $cache[ $this->id ] ) ) ? $cache[ $this->id ] : 0; } else { $_data = json_decode( $this->connection['body'], true ); if ( isset( $_data['data'][0]['total_count'] ) ) { $count = intval( $_data['data'][0]['total_count'] ); $this->total = $count; } else { $this->total = ( isset( $cache[ $this->id ] ) ) ? $cache[ $this->id ] : 0; } } } return $this->total; }
Now enter in admin facebook if Full url of your page like:-
https://www.facebook.com/pages/Tutsnare/757936347627025
Now Change the link of view
Go to wp-content/plugins/social-count-plus/includes/class-social-count-plus-view.php
Try to remove facebook.com from function and change with belowprotected static function get_facebook_counter( $settings, $total, $color ) { return self::get_view_li( 'facebook', $settings['facebook_id'], $total, __( 'likes', 'social-count-plus' ), $color, $settings ); }
Hi,
I too utilized this plugin a few days back. My twitter is working well, but not Facebook. So I find a solution to it. Just change your
class-social-count-plus-facebook-counter.php
located in
wp-content/plugins/social-count-plus/includes/counters/
With the below code.if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } /** * Social Count Plus Facebook Counter. * * @package Social_Count_Plus/Facebook_Counter * @category Counter * @author Claudio Sanches */ class Social_Count_Plus_Facebook_Counter extends Social_Count_Plus_Counter { /** * Counter ID. * * @var string */ public $id = 'facebook'; /** * API URL. * * @var string */ protected $api_url = 'https://api.facebook.com/restserver.php?method=facebook.fql.query&query=SELECT fan_count FROM page WHERE page_id='; /** * Test the counter is available. * * @param array $settings Plugin settings. * * @return bool */ public function is_available( $settings ) { return ( isset( $settings['facebook_active'] ) && isset( $settings['facebook_id'] ) && ! empty( $settings['facebook_id'] ) ); } /** * Get the total. * * @param array $settings Plugin settings. * @param array $cache Counter cache. * * @return int */ public function get_total( $settings, $cache ) { if ( $this->is_available( $settings ) ) { $params = array( 'sslverify' => false, 'timeout' => 60 ); //echo html_entity_decode ($this->api_url) . $settings['facebook_id']; //$this->connection = wp_remote_get( html_entity_decode ($this->api_url) . $settings['facebook_id'] ); $this->connection = @simplexml_load_file( html_entity_decode ($this->api_url) . $settings['facebook_id'] ); if ( is_wp_error( $this->connection ) ) { $this->total = ( isset( $cache[ $this->id ] ) ) ? $cache[ $this->id ] : 15; } else { $_data = $this->connection; if ( isset( $_data->page->fan_count ) ) { $count = (string)$_data->page->fan_count; $this->total = $count; } else { $this->total = ( isset( $cache[ $this->id ] ) ) ? $cache[ $this->id ] : 15; } } } return $this->total; } }
The missing thing is that it only updating counts on every time saving option from admin panel.In short counts are not auto-updating.
Thanks Yashpal, you rock! I’ve switched to your solution because the usage is closer to what was originally intended, but Satheesh did a good job too ??
Claudio Sanches will you update the plugin or shall we try one of this tweaks?
@kaskarino, the work around provided by satheeshgecp doesn’t require any change in the plugin, no tweaks at all. It just explains the way to get a new Facebook ID that will work seamlessly in the plugin.
So the plugin is perfect as it is. It’s just the process the get the Facebook ID that became a bit more tricky.Thanks jshare for your valuable comment.
I think the plugin has to be updated according to the graph api. That would be best solution.
Inspite of doing manual work by everyone.Thanks,
Yashpal k
- The topic ‘Facebook counter not working, Please help me to fix.’ is closed to new replies.