What is nextpage tag?
<!--nextpage--> is an in-built tag of WordPress that allows you to split a post or page into multiple webpages, so that it has pagination. Using nextpage tag is quite simple, you just need to insert <!–nextpage–> into the post where you like the page to break. It’s an useful feature that allows you to break a long post or a page into two, three or more pages with the support of pagination for easy navigation. Well, it also has an undocumented benefit – helps in improving page impressions and doing so will allow the page to load new set of advertisements for the visitor. I had used this tag on many posts in Techglimpse. But now I wanted to remove <!–nextpage–> tags from all posts.
Why do I want to remove page breaks?
Yes, I wanted to do that, because I’m planning to introduce “Table of contents” to all my posts – which will allow the visitor to click on a specific heading that he or she is interested and scroll to that section instantly. Also, I coded the feature in such a way that it will automatically read all H2 tags from the post and display it as Table of Contents. It means, all those sections/paras under H2 should be loaded on the same page. Ok, I think you got my point? Great! I don’t want to split long posts into multiple pages. So I wanted to remove nextpage pagination tags from all the old posts and this tutorial will tell you how to do that.
Remove nextpage pagination tags from old posts
It’s not going to be easy to edit each individual post and remove page breaks manually. So we should write a hook to do that.
We’ll add a filter to functions.php file. The custom filter will alter and return the updated post. You will just need to copy the below code and paste it in functions.php file located in your theme folder (probably, the best way to do this is to add it in child theme).
function kill_pages($posts,$qry) { if ($qry->is_single()) { $posts[0]->post_content = preg_replace( '/<!--nextpage(.*?)?-->/', '', $posts[0]->post_content ); } return $posts; } add_filter('the_posts','kill_pages',1,2);
The above code will replace all nextpage tag in the post with an empty string and returns the updated one. Now, go ahead and test your post.
Liked it? Let me know your feedback below.
thanks, it’s helpful.