4

Is it possible to remove "nextpage" tag inside posts text depending of utm_campaign ?

Depending of where my visitors are comming from, I want to remove the

<!--nextpage--> 

of my post.

I am using this

if($_GET['utm_campaign']== 'nonextpagecampaign') {

directly in my template in order to display or not things depending of the campaign name but for the nextpage tag, it's not that easy.

6

You can use the_post hook to remove <!--nextpage-->. In this case:

add_action( 'the_post', 'campaign_remove_nextpage', 99);

function campaign_remove_nextpage ( $post ) {
    if ( ($_GET['utm_campaign']== 'nonextpagecampaign') && (false !== strpos( $post->post_content, '<!--nextpage-->' )) ) 
    {
        // Reset the global $pages:
        $GLOBALS['pages']     = [ $post->post_content ];

        // Reset the global $numpages:
        $GLOBALS['numpages']  = 0;

       // Reset the global $multipage:
        $GLOBALS['multipage'] = false;
    }
};

Read more about this issue

More in general, you may want to read this warning about SEO effects of using <!--nextpage-->.

0
4

We can use the content_paginationcodex filter to modify the paginated content without modifying the globals directly:

add_filter( 'content_pagination', function( $pages )
{
    // Target only the correct utm_campaign GET parameter 
    if( 'nonextpagecampaign' !== filter_input( INPUT_GET, 'utm_campaign', FILTER_SANITIZE_STRING ) )
        return $pages;

    // Remove the content pagination, only if it's already paginated
    if( count( $pages ) > 1 )
        $pages = [ join( '', $pages ) ];

    return $pages;
} );

where we return an array with the combined content when the correct utm_campaign GET paramter is detected. Hopefully you can adjust this further to your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.