Changing Link Attributes for Wp_Link_Pages

There is no filter for the URLs, and no filter for the complete output of wp_link_pages(). But you can get the output as string if you pass 'echo' => FALSE as argument.

There are four options:

  1. Write a modified copy of the function with the URLs you need. You will miss all further improvements which may happen in core code.
  2. Catch the output in a string and run a regex on that. Example:

    print preg_replace(
        '~(href="https://wordpress.stackexchange.com/questions/77843/)([^"]+)~',
        '\1\2?from=' . $post->post_name,
        wp_link_pages( array ( 'echo' => FALSE ) )
    );
    
  3. Use JavaScript to add the parameter.

  4. Do nothing. Do not offer the same content – separate sub pages – with different URLs (a search result could link to a sub page too), and make external search engines like Google happy.

Leave a Comment