Had a problem come up recently where folks wanted to keep engaging visitors on their website on a single post page — keep loading more posts afterwards when they kept scrolling.
I’ve heard of this before, and even seen some plugins accomplish it — for example, Infinite Post Transporter, by Tom Harrigan — wp.org / github — the codebase of which looks to be a modified version of Jetpack’s Infinite Scroll from about six years ago — (contemporary link).
So, I was curious to see how far I could go about getting close to what we needed just by playing with Jetpack’s own library, rather than duplicating a bunch of the code in a second plugin.
For anyone that wants to skip to the end and just get something to play with, here’s the gist that I’ve got the code shoved into for now.
First, a couple specifications we’re working with here:
- I want to make this work on single post pages, specifically of the
post
post_type. - I don’t want to modify Jetpack files, or deregister / replace them with customized versions of the files. Filters / actions only.
So, skimming through the Jetpack Infinite Scroll codebase, there’s a couple conditionals we’re gonna need to short out to get things triggering on single post pages.
The bulk of the per-page code comes from The_Neverending_Home_Page::action_template_redirect()
— this is the function that will register/enqueue Infinite Scroll’s scripts and styles, and set up footer actions to populate some javascript globals that specify state. However, for single post
s, we’ll need to override two points.
if ( ! current_theme_supports( 'infinite-scroll' ) || ! self::archive_supports_infinity() )
return;
By default, single post
s aren’t archives, and don’t work. So let’s hotwire it! Digging into The_Neverending_Home_Page::archive_supports_infinity()
we can see that the whole response to that call is simply passed through a filter — `infinite_scroll_archive_supported`
/**
* Allow plugins to filter what archives Infinite Scroll supports.
*
* @module infinite-scroll
*
* @since 2.0.0
*
* @param bool $supported Does the Archive page support Infinite Scroll.
* @param object self::get_settings() IS settings provided by theme.
*/
return (bool) apply_filters( 'infinite_scroll_archive_supported', $supported, self::get_settings() );
So to filter this, we’ll want to make sure we’re using the right conditionals and not filtering too many pages. In this case, is_singular( 'post' )
feels appropriate.
For brevity here, I’ll just be using anonymous functions, but in practice it’s likely far better to name your functions so other code can remove the filters if it becomes necessary.
add_filter( 'infinite_scroll_archive_supported', function ( $supported ) {
if ( is_singular( 'post' ) ) {
return true;
}
return $supported;
} );
Groovy, now we’ve got the function registering the scripts we care about. But there’s a second conditional later in the ::action_template_redirect()
method that we also get snagged on — ::is_last_batch()
.
// Make sure there are enough posts for IS
if ( self::is_last_batch() ) {
return;
}
Fortunately, like our last example, The_Neverending_Home_Page::is_last_batch()
is also filterable.
/**
* Override whether or not this is the last batch for a request
*
* @module infinite-scroll
*
* @since 4.8.0
*
* @param bool|null null Bool if value should be overridden, null to determine from query
* @param object self::wp_query() WP_Query object for current request
* @param object self::get_settings() Infinite Scroll settings
*/
$override = apply_filters( 'infinite_scroll_is_last_batch', null, self::wp_query(), self::get_settings() );
if ( is_bool( $override ) ) {
return $override;
}
So again, we can just override it as we had before, only this case returning false
:
add_filter( 'infinite_scroll_is_last_batch', function ( $is_last_batch ) {
if ( is_singular( 'post' ) ) {
return false; // Possibly retool later to confirm there are other posts.
}
return $is_last_batch;
} );
Great! So now we’ve got the scripts that do the work being output on our single posts page, but we’re not quite there yet! We need to change some of the variables being passed in to Jetpack’s Infinite Scroll. To modify those variables, we have — you guessed it — another filter.
The JS Settings are being output in The_Neverending_Home_Page::action_wp_footer_settings()
(rather than being added via wp_localize_script
), and here’s the filter we’ll be working off of:
/**
* Filter the Infinite Scroll JS settings outputted in the head.
*
* @module infinite-scroll
*
* @since 2.0.0
*
* @param array $js_settings Infinite Scroll JS settings.
*/
$js_settings = apply_filters( 'infinite_scroll_js_settings', $js_settings );
so we’ll start with a generic action like this, and start customizing:
add_filter( 'infinite_scroll_js_settings', function ( $js_settings ) {
if ( is_singular( 'post' ) ) {
$js_settings['foo'] = 'bar'; // any we need to make!
}
return $js_settings;
} );
We can verify this change is in place by loading up a single post page, and searching for foo
— confirming that it’s in the encoded string that’s being output to the page. So now we need to start looking at the javascript that runs and see what changes to inputs we may need to make.
First, we’ll need to make sure that the wrapper container we want to append our new posts to is the same as the ID that we use on archive pages. If it’s not, we just need to override that. In my case, I had to change that to main
on the single post pages — which can be done like so:
$js_settings['id'] = 'main';
If your wrapper id
is the same as archive pages, this can just be skipped. Here we can also override some other options — for example if we would rather change the verbiage on the load more posts button we could do
$js_settings['text'] = __( 'Read Next' );
or to switch from the button click, to autoloading on scroll we could do
$js_settings['type'] = 'scroll';
Now, if you tried what we have so far, you may notice that instead of getting new posts, you’ll likely see the same post loading over and over forever. That’s because of the parameters getting passed through as query_args
— if you pop open your browser window to examine infiniteScroll.settings
and look at the query_args
param, you’ll likely notice that we’ve gotten the infiniteScroll.settings.query_args.name
populated! This is getting passed directly to the ajax query, so when trying to pull up more posts, it’s restricting it to only the already queried post, as that’s from the query WordPress ran to generate the current url’s response.
So let’s just nuke it, and for good measure ensure we don’t inadvertently re-display the post in question.
$js_settings['query_args']['name'] = null;
$js_settings['query_args']['post__not_in'][] = get_the_ID();
Cool cool cool. So at this point, when you run the post, everything /should/ look about right, except for one small thing! You may see the url updating as you scroll to append /page/2/
to the path!
Unfortunately a lot of this functionality is hard-coded and I couldn’t find a good way to override it to update the url to — for example — the url of the post you’ve currently got in view, so I wound up doing the next best thing — nuking the functionality entirely.
/**
* Update address bar to reflect archive page URL for a given page number.
* Checks if URL is different to prevent pollution of browser history.
*/
Scroller.prototype.updateURL = function ( page ) {
// IE only supports pushState() in v10 and above, so don't bother if those conditions aren't met.
if ( ! window.history.pushState ) {
return;
}
var self = this,
pageSlug = self.origURL;
if ( -1 !== page ) {
pageSlug =
window.location.protocol +
'//' +
self.history.host +
self.history.path.replace( /%d/, page ) +
self.history.parameters;
}
if ( window.location.href != pageSlug ) {
history.pushState( null, null, pageSlug );
}
};
As the js relies on rewriting the url via calling .replace()
on a string, if we eat the token it searches for off the end, it’ll just wind up replacing the url with itself, and doesn’t look awkward. And so —
$js_settings['history']['path'] = str_replace( 'page/%d/', '', $js_settings['history']['path'] );
So, functionally this should get you most of the way there. The only other bit that I had added to my implementation was something that could trivially be done anywhere — Custom CSS in the Customizer, or theme files — but I wanted to hide Post Navigation links in my theme. So I just did a fun little trick of enqueueing my one line css tweak (with appropriate conditionals) like so:
add_action( 'template_redirect', function () {
if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_module_active( 'infinite-scroll' ) ) {
return;
}
if ( is_singular( 'post' ) ) {
// jisfsp = Jetpack Infinite Scroll for Single Posts
wp_register_style( 'jisfsp', null );
wp_enqueue_style( 'jisfsp' );
wp_add_inline_style( 'jisfsp', 'nav.post-navigation { display: none; }' );
}
} );
This way, if someone disables either Jetpack or infinite scroll, the conditional trips and it stops hiding post navigation links.
I hope this has been somewhat useful to someone. It’s probably not at the point where I’d be comfortable packaging it up as a plugin for end-user installation, but if you’ve got a passing understanding of code and how WordPress works, it shouldn’t be difficult to re-implement for a client. If there’s any other tweaks on Infinite Scroll that you wind up finding and would like to suggest, please feel free to leave a comment below, and it’ll hopefully be useful to others. Cheers!