Earlier today I was trying to change the “type” for JetPack’s Infinite Scroll behavior to use “click” only for the front page. For all other archives I wanted JetPack to use the standard “scroll” type (you know, the namesake for Infinite Scrolling).
I think the infinite_scroll_has_footer_widgets filter is the right choice, but is_front_page() isn’t yet available when the filter runs
— Brian Richards (@rzen) May 21, 2016
(In a hurry? The solution is at the very bottom of the post.)
JetPack has a bunch of filters available, and the developers even had the foresight to provide a conditional to toggle this behavior based on whether or not the site footer is displaying widgets. Each of the JetPack Infinite Scroll Settings is explained extremely well in the documentation. The setting designed for what I wanted to do is called footer_widgets
.
This setting expects a boolean and, if true (footer widgets are present), JetPack will automatically convert the event trigger to “click” in order to allow visitors to see the footer. My first thought was that I could throw is_front_page()
right here in the settings definition. My immediate second thought was, “no, that won’t work.”
You see, when the theme support for infinite scroll is registered we’re on the after_setup_theme
hook, which is much too early for WordPress to know which template is being loaded.
Digging in, I found a filter mentioned in the documentation named infinite_scroll_has_footer_widgets
designed expressly for more complex logic. This is perfect, I thought. Surely this could be as easy as hooking the is_front_page()
callback to the filter (i.e. add_filter( 'infinite_scroll_has_footer_widgets', 'is_front_page' )
). Nope; this filter is also evaluated before the template conditionals know where we are on the site. Foiled again!
Ah ha! There’s a filter named infinite_scroll_settings
we can override! Nope, this is run too early, too.
Finally, Ben Gillbanks (@BinaryMoon) came to my rescue:
ah – I see. That's a pain. How about filtering at the js level then – https://t.co/ibPhEMpLNu
— Ben ? (@BinaryMoon) May 21, 2016
He was exactly right. The filter infinite_scroll_js_settings
, designed for altering the settings just before they’re passed into JavaScript, works because its parent function is hooked to wp_footer
.
Thanks Ben!
If you’ve got a more appropriate filter or an even better solution, I’d love to hear it!
Leave a Reply