0

So I'm still new to Wordpress and development in general and trying to add a leading zero to post pagination numbers. The code I came up with is as follows:

function leading_zero_wp_link_pages_link( $i ) {
$i = zeroise($i, 2);
return $i;
}

add_filter( 'wp_link_pages_link',  'leading_zero_link_pages_link' );

which of course isn't working. I know I need to modify the '$i' integer variable as specified in the codex and I'm almost certainly misunderstanding how hooks/filters work and any help would be great!

0

wp_link_pages_link filters the entire HTML link (<a href... etc.), which is why what you've got isn't working. There doesn't appear to be a filter for just the page number, $i, so you'll need a workaround.

Since the callback function for the wp_link_pages_link filter passes the $i, we could use that to find the number inside the HTML and replace it.

The simple version would be to simply replace $i inside $link:

function wpse_287783_zeroize_page_numbers( $link, $i ) {
    $zeroised = zeroise( $i, 2 );

    $link = str_replace( $i, $zeroised, $link );

    return $link;
}
add_filter( 'wp_link_pages_link', 'wpse_287783_zeroize_page_numbers', 10, 2 );

There are two related problems with this solution:

  1. This will also affect the page number in the URL, which we don't want to do, because this is purely cosmetic.
  2. If the URL includes any other numbers this will affect those too. This could include date archives where months or years that have the page number in them.

So what we need to do is only replace the number inside the HTML tag. To do that we can use a regular expression:

function wpse_287783_zeroize_page_numbers( $link, $i ) {
    $zeroised = zeroise( $i, 2 );

    $link = preg_replace( '/>(\D*)(\d*)(\D*)</', '>${1}' . $zeroised . '${3}<', $link );

    return $link;
}
add_filter( 'wp_link_pages_link', 'wpse_287783_zeroize_page_numbers', 10, 2 );

Now someone might be able to provide a better regular expression, since I'm rubbish at them, but what '/>(\D*)(\d*)(\D*)</' does is get characters between > and <, which will be the stuff inside the tag, and return 3 things: Text before any digits, any digits, and text after the digits. The text before and after is so it still only affects digits if link_before, link_after or pagelink from wp_link_pages() have any other text in them.

With preg_replace() we are replacing just the 2nd thing returned, the digits, with the zeroised version of the page number.

  • Thanks Jacob that makes a lot of sense. I'll be honest, I didn't quite expect it to get that convoluted though! Now it's still not working, but I have a feeling that's because I'm building on top of FoundationPress and I'm pretty sure it's messing around with the pagination so I'm gonna see if I can work out what it's doing. – andersonslookout Dec 5 '17 at 15:18

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.