Using WordPress ‘content_pagination’ PHP filter

The content_pagination WordPress PHP filter allows you to modify the “pages” created after splitting the post content based on the presence of <!-- nextpage --> tags.

Usage

add_filter('content_pagination', 'my_custom_content_pagination', 10, 2);

function my_custom_content_pagination($pages, $post) {
  // your custom code here
  return $pages;
}

Parameters

  • $pages (string[]): An array of “pages” from the post content split by <!-- nextpage --> tags.
  • $post (WP_Post): The current post object.

More information

See WordPress Developer Resources: content_pagination

Examples

Add a custom header to each page

This example adds a custom header to each “page” of the post content.

function my_custom_content_pagination($pages, $post) {
  $header = '<h2>Custom Header</h2>';

  foreach ($pages as &$page) {
    $page = $header . $page;
  }

  return $pages;
}
add_filter('content_pagination', 'my_custom_content_pagination', 10, 2);

Remove empty pages

This example removes empty pages from the array.

function my_custom_content_pagination($pages, $post) {
  $pages = array_filter($pages, 'trim');

  return $pages;
}
add_filter('content_pagination', 'my_custom_content_pagination', 10, 2);

Reverse the order of pages

This example reverses the order of the pages.

function my_custom_content_pagination($pages, $post) {
  $pages = array_reverse($pages);

  return $pages;
}
add_filter('content_pagination', 'my_custom_content_pagination', 10, 2);

Add page numbers to each page

This example adds a page number to each “page” of the post content.

function my_custom_content_pagination($pages, $post) {
  foreach ($pages as $index => &$page) {
    $pageNumber = $index + 1;
    $page = "<span>Page {$pageNumber}</span>" . $page;
  }

  return $pages;
}
add_filter('content_pagination', 'my_custom_content_pagination', 10, 2);

Limit the number of pages

This example limits the number of pages to a maximum of 3.

function my_custom_content_pagination($pages, $post) {
  $pages = array_slice($pages, 0, 3);

  return $pages;
}
add_filter('content_pagination', 'my_custom_content_pagination', 10, 2);