Many sites load more posts while the visitor scrolls or after a click on “Load more”. Since AdPresso 1.7.0, your placements also work in this loaded content. New posts get their ads just like the posts that were on the page from the start. This is part of the free version of AdPresso.
With the plugins listed below it works without any setup. Themes and plugins that load content with their own script need a small integration, which the rest of this page describes.
| Plugin or feature | Version | Setup |
|---|---|---|
| Jetpack Infinite Scroll | Free | None. AdPresso detects it. |
| Ajax Load More | Free | None. AdPresso detects it. |
| BuddyPress activity streams | Pro | None. |
| PeepSo activity streams | Pro | None. |
There is no setting for this in AdPresso. To check that it works, open a page with infinite scroll or a “Load more” button, load more posts, and look for your ads in the new posts.
The client scripts for dynamic content are only loaded when they are needed. The filter adpresso_dynamic_content_support switches them on for every page. It expects a boolean and defaults to false. Jetpack Infinite Scroll and Ajax Load More enable it on their own.
add_filter( 'adpresso_dynamic_content_support', '__return_true' );
After your script has added new content to the page, tell AdPresso about it. There are two functions.
| Function | Use it when |
|---|---|
AdPresso.content.scan( container ) | the new HTML is already in the DOM. AdPresso looks for placements inside the container and fills them. |
AdPresso.content.insert( container, html, { view: 'new' } ) | you have the HTML as a string. AdPresso inserts it into the container and fills the placements in it. |
fetch( url )
.then( ( response ) => response.text() )
.then( ( html ) => {
const container = document.querySelector( '#more-posts' );
container.insertAdjacentHTML( 'beforeend', html );
AdPresso.content.scan( container );
} );
Loaded content belongs to one of two views.
| View | Meaning | Single Appearance (Pro) |
|---|---|---|
continue (default) | The content extends what the visitor already sees, as in a stream or the next page of a list. | Ads that were already shown stay blocked. |
new | The content counts as a new page view, for example the next article loaded below the current one. | Starts over. |
continue, not the view of the page.Instead of calling the functions directly, you can dispatch the event adpresso:contentLoaded. Its detail carries root, the element that contains the new content, and view, which is 'continue' by default or 'new'.
document.dispatchEvent(
new CustomEvent( 'adpresso:contentLoaded', {
detail: { root: container, view: 'continue' },
} )
);
If your script may run before AdPresso has loaded, push the content into the queue window.adpressoContentQueue. AdPresso processes the entries as soon as it is ready.
window.adpressoContentQueue = window.adpressoContentQueue || [];
window.adpressoContentQueue.push( { root: container, view: 'continue' } );
If you render posts in your own AJAX or REST endpoint, wrap the rendering in adpresso_render_in_post_context(). The helper sets up the post context so that AdPresso can add its placements to the output. It is meant for REST and AJAX requests only. During a normal page view it returns an empty string and writes a “doing it wrong” notice to the debug log.
adpresso_render_in_post_context( $post, callable $callback, array $args = [] );
adpresso_render_in_post_context(
$post,
function () {
get_template_part( 'template-parts/content', 'single' );
},
[ 'output' => 'js' ]
);
Value of output | Result |
|---|---|
'js' (default) | All placements come back as placeholders, and the browser selects the ads. The response can be cached. |
'auto' | Each placement decides, as in a normal page view, whether it is rendered on the server or in the browser. Server-rendered ads are part of the returned HTML. The response is specific to this request and not suited for caching without further measures. |
How the two output paths differ is explained in The rendering pipeline. More hooks are listed under Filters.