Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created November 4, 2016 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaredatch/d72f042791c550886e5ad0baaf70e08f to your computer and use it in GitHub Desktop.
Save jaredatch/d72f042791c550886e5ad0baaf70e08f to your computer and use it in GitHub Desktop.
WordPress Facebook Comments Lazy Load
<?php
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="fb-comments">
<div id="fb-root"></div>
<div class="fb-comments" data-width="100%" data-href="<?php echo esc_url( get_permalink() ); ?>" data-numposts="5">dasdas</div>
</div>
<?php
<?php
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*/
function ja_global_enqueues() {
wp_enqueue_script(
'jquery-waypoint',
'https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.js',
array( 'jquery' ),
'4.0.1',
true
);
wp_enqueue_script(
'global',
get_template_directory_uri() . '/js/global.min.js',
array( 'jquery' ),
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'ja_global_enqueues' );
jQuery(function($){
// Lazy load Facebook comments when viewing blog posts
var $comments = $('#fb-comments');
if ( $comments.length > 0 ) {
$comments.waypoint({
handler: function(direction) {
console.log('Loading Facebook comments!');
var js = document.createElement('script');
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.8&appId=XX_YOUR_APP_ID_XX";
document.body.appendChild(js);
this.destroy();
},
offset: '100%'
})
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment