Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Last active May 21, 2021 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgestephanis/42b194dd5b58ad465865eeabdde2bb05 to your computer and use it in GitHub Desktop.
Save georgestephanis/42b194dd5b58ad465865eeabdde2bb05 to your computer and use it in GitHub Desktop.
WORK IN PROGRESS
<?php
// Plugin name: Jetpack Infinite Scroll for Single Posts
/**
* Infinite scroll needs to be filtered to function on non-archive pages.
*/
function jisfsp_archive_supported( $supported, $settings ) {
if ( is_singular( 'post' ) ) {
return true;
}
return $supported;
}
add_filter( 'infinite_scroll_archive_supported', 'jisfsp_archive_supported', 10, 2 );
/**
* A filter to the infinite scroll settings so we can shove in arbitrary data.
*/
function jisfsp_js_settings( $js_settings ) {
if ( is_singular( 'post' ) ) {
$js_settings['id'] = 'main';
$js_settings['text'] = __( 'Read Next' );
$js_settings['type'] = 'scroll';
// mess with $js_settings['query_args'] here!
$js_settings['query_args']['name'] = null;
$js_settings['query_args']['post__not_in'][] = get_the_ID();
// Nip off the page change so the url rewriting won't misparse it.
$js_settings['history']['path'] = str_replace( 'page/%d/', '', $js_settings['history']['path'] );
}
return $js_settings;
}
add_filter( 'infinite_scroll_js_settings', 'jisfsp_js_settings' );
/**
* Generally infinite scroll won't load if we're on the last page of an archive page.
*
* As we're trying to get it to work on single pages,
*/
function jisfsp_is_last_batch( $is_last_batch, $query, $settings ) {
if ( is_singular( 'post' ) ) {
return false; // Replace with a check for next post being set
}
return $is_last_batch;
}
add_filter( 'infinite_scroll_is_last_batch', 'jisfsp_is_last_batch', 10, 3 );
/**
* An arbitrary conditional that runs before pages render for any remaining misc bits.
*
* As this action runs off WordPress Core actions instead of Infinite Scroll actions we
* need some extra conditionals to make sure we're not running if IS is disabled.
*/
function jisfsp_all_the_single_posts() {
if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'infinite-scroll' ) ) {
return;
}
$settings = The_Neverending_Home_Page::get_settings();
if ( is_singular( 'post' ) ) {
wp_register_style( 'jisfsp', null );
wp_enqueue_style( 'jisfsp' );
wp_add_inline_style( 'jisfsp', 'nav.post-navigation { display: none; }' );
}
}
add_action( 'template_redirect', 'jisfsp_all_the_single_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment