{"id":4205,"date":"2019-11-01T09:55:43","date_gmt":"2019-11-01T13:55:43","guid":{"rendered":"https:\/\/themespress.ca\/?p=4205"},"modified":"2021-11-18T06:42:14","modified_gmt":"2021-11-18T11:42:14","slug":"display-the-number-of-facebook-shares-for-a-page-or-post","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/display-the-number-of-facebook-shares-for-a-page-or-post\/","title":{"rendered":"Display the Number of Facebook Shares for a Page or Post"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p> With this tutorial we will show you how to display the number of share of a WordPress page or post on Facebook. The first step will be creating an API via <a href=\"https:\/\/developers.facebook.com\/?locale=en_US\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook developpers<\/a>.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n\n\t<div  class=\"wpb_single_image wpb_content_element vc_align_left wpb_content_element\">\n\t\t\n\t\t<figure class=\"wpb_wrapper vc_figure\">\n\t\t\t<div class=\"vc_single_image-wrapper   vc_box_border_grey\"><img decoding=\"async\" width=\"1320\" height=\"655\" src=\"https:\/\/themespress.ca\/wp-content\/uploads\/2019\/11\/facebook-application-1320x655-1.jpg\" class=\"vc_single_image-img attachment-full\" alt=\"\" title=\"facebook-application-1320x655\" srcset=\"https:\/\/themespress.ca\/wp-content\/uploads\/2019\/11\/facebook-application-1320x655-1.jpg 1320w, https:\/\/themespress.ca\/wp-content\/uploads\/2019\/11\/facebook-application-1320x655-1-600x298.jpg 600w, https:\/\/themespress.ca\/wp-content\/uploads\/2019\/11\/facebook-application-1320x655-1-300x149.jpg 300w, https:\/\/themespress.ca\/wp-content\/uploads\/2019\/11\/facebook-application-1320x655-1-1024x508.jpg 1024w, https:\/\/themespress.ca\/wp-content\/uploads\/2019\/11\/facebook-application-1320x655-1-768x381.jpg 768w\" sizes=\"(max-width: 1320px) 100vw, 1320px\" \/><\/div>\n\t\t<\/figure>\n\t<\/div>\n\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Once you&#8217;ve created your API, go into the left hand side menu under Settings &#8211;> General. This is where you&#8217;ll find the APP ID and your secret passkey. You can also fill out additional important information such as websites authorized to use this app among other useful information.<\/p>\n<p>Afterwards, you&#8217;ll need to add a function that will allow the API to count the number of Facebook shares of the WordPress post via its ID. You&#8217;ll need to add it into your functions.php file:<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-6548323\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-6548323\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">function wp_get_shares_facebook( $post_id ) {\n\t$cache_key = &#039;toni_share&#039; . $post_id;\n\t$access_token = &#039;APP_ID|APP_SECRET&#039;;\n\t$count = get_transient( $cache_key ); \/\/ try to get value from Wordpress cache\n \n\t\/\/ if no value in the cache\n\tif ( $count === false ) {\n\t\t$response = wp_remote_get( add_query_arg( array( \n\t\t\t&#039;id&#039;               =&gt; urlencode( get_permalink( $post_id ) ),\n\t\t\t&#039;access_token&#039;     =&gt; $access_token,\n\t\t\t&#039;fields&#039;           =&gt; &#039;engagement&#039;\n\t\t), &#039;https:\/\/graph.facebook.com\/v3.0\/&#039; ) );\n\t\t\n\t\t$body = json_decode( $response[&#039;body&#039;] );\n\t\t\/\/print_r($body);\n \n\t\t$count = intval( $body-&gt;engagement-&gt;share_count ); \n \n\t\tset_transient( $cache_key, $count, 3600 ); \/\/ store value in cache for a 1 hour\n\t}\n\treturn $count;\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>Next to $access_token = \u2018APP_ID|APP_SECRET\u2019, you&#8217;ll need to replace APP_ID by your API&#8217;s ID and then replace APP_SECRET by the secret passkey. <\/p>\n<p>Once your function is ready to go, we will display the result in desired area of your template; all you need to do is call the current post ID to display its number of Facebook shares. <\/p>\n\n\t\t<\/div>\n\t<\/div>\n<div id=\"ts-enlighterjs-container-1925298\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-1925298\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"standard\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">&lt;?php $id = get_the_ID(); echo wp_get_shares_facebook( $id ); ?&gt;<\/pre><\/div><\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial we will show you how to display the number of share of a Wordpress page or post on Facebook. The first step will be creating an API<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/display-the-number-of-facebook-shares-for-a-page-or-post\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":4941,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[341],"class_list":["post-4205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-social-medias"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/4205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=4205"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/4205\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/4941"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=4205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=4205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=4205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}