patric-boehner
8/1/2016 - 5:29 AM

Adding Facebook Comments to Genesis Single Posts

Adding Facebook Comments to Genesis Single Posts

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

//* Single blog post template
//**********************************************


//* After Entry
//**********************

//* Facebook Comments
get_template_part( '/inc/partials/entry', 'comments' );
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

/*
* Add to theme/inc/functions
*/

//**********************************************
//* Add Facebook SDK validation JS to header
//**********************************************
// https://developers.facebook.com/docs/plugins/comments

add_action('genesis_before','pb_facebook_validation');
function pb_facebook_validation(){

	// Display on single posts but not singular custom post type
	if ( is_single() && !is_singular( array( 'cpt_slug' ) ) ) {

		$output='<div id="fb-root"></div>
		<div id="fb-root"></div>
		<script>(function(d, s, id) {
		  var js, fjs = d.getElementsByTagName(s)[0];
		  if (d.getElementById(id)) return;
		  js = d.createElement(s); js.id = id;
		  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7&appId=xxxxxxxxxxx";
		  fjs.parentNode.insertBefore(js, fjs);
		}(document, "script", "facebook-jssdk"));</script>';
		echo $output;
		echo '<!--Facebook Validation-->';

	}
}


//* Facebook Comment Moderators
//**********************************************
// Using a Facebook App ID

add_action( 'wp_head', 'pb_facebook_comment_moderators' );
function pb_facebook_comment_moderators() {
// http://findmyfbid.com/

	$app_id		 = 'xxxxxxxxxxxxxx';

   if ( is_single() || is_attachment() ) {
      echo '<meta property="fb:app_id" content="'.$app_id.'" />';
      echo '<!--Facebook Comment Moderation-->';
   }
}
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

/*
* Add to theme/inc/partials
*/

//**********************************************
//* Facebook Comments
//**********************************************

add_action( 'genesis_after_entry', 'pb_facebook_comments' );
remove_action( 'genesis_after_entry', 'genesis_get_comments_template' );
function pb_facebook_comments() {

	$number_of_posts = '5';

	echo '<div class="entry-comments" id="comments">';
	echo '<div class="fb-comments" id="respond" data-href="' . pb_production_url( get_permalink() ) . '" data-numposts="' .$number_of_posts . '" data-width="100%"></div>';
	echo '</div>';

}


//* Production URL
//**********************************************
/* Helper function to always reference live URLs
 * http://www.billerickson.net/code/adding-facebook-comments/
 */
function pb_production_url( $url ) {
	$production_url = 'http://example.com';

	$dev_urls = array(
		'http://example.dev',
		'http://staging.patrickboehner.com/example',
	);
	return str_replace( $dev_urls, $production_url, $url );
}