Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!!! :)

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.
share|improve this answer
    
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. :) –  user1936894 Jan 1 '13 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. –  toscho Jan 1 '13 at 2:17
    
your updated answer (number 2) worked like a charm, thanks a lot!!! :) –  user1936894 Jan 1 '13 at 2:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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