0

I'm wondering if there is any way to change the actual link itself for wp_link_pages. So right now, I have a simple code to display the pagination for a post, it looks like this:

<?php wp_link_pages( array( 'before' => '<div class="link-pages">' . __('Pages:', 'muimedia'), 'after' => '</div>',) ); ?>

It displays the Post pagination like so:

Pages: 1 2 3

Page one links two page one, page two links to page 2 etc. Now, I would like to add an attribute called fromwhere=news" to the pagination permalinks. So page 1, 2, and 3 pagination links would look like so.. mysite.com/somepost/post?fromwhere=news" instead of just mysite.com/somepost/post

I really hope this is possible, and if anyone could help me out, it would really mean a lot to me! And happy new years!!! :)

1 Answer 1

4

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=")([^"]+)~',
        '\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.
3
  • Thanks for the reply, I agree that your answers are great starting points, but I honestly have no idea how to make your answers work unfortunately . Regarding number 2, how would I make this work? I know how to do this with custom fields, that's not hard. But the complicated part is adding the ?fromwhere=news" to the end of the URL. Do you think you could help me out with an answer to add the fromwhere to the end of the pagination links via your number 2 answer (or maybe even number 3)? It would really mean a lot. :) Commented Jan 1, 2013 at 1:55
  • @user1936894 I had an error in my first explanation: You don’t need an output buffer. And I added an example showing how to change the URLs with a regex.
    – fuxia
    Commented Jan 1, 2013 at 2:17
  • your updated answer (number 2) worked like a charm, thanks a lot!!! :) Commented Jan 1, 2013 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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