WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Modifying "next_post_link"

[Resolved] Modifying "next_post_link"

  • Hi there. I want to add some tracking to my “next_post_link” and my “next_previous_link” so that I can see whether people are actually using the links.

    I’m thinking the easiest way to do it would be to add a “?next” or “?previous” to the URLs so that they’d show up in Google Analytics.

    But.. looks like the code to make the URLs is waaaaaay buried in “link-template.php” and i don’t really want to mess with it. Can anyone help with a simple function that could make this happen?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Michael

    @alchymyth

    Forum Moderator

    you could possibly add filters; in functions.php of the theme:

    example:

    add_filter('next_post_link', 'mod_next_post_link');
    function mod_next_post_link($link) {
    $link = str_replace('" rel="next">', '?next" rel="next">', $link);
    return $link;
    }
    add_filter('previous_post_link', 'mod_previous_post_link');
    function mod_previous_post_link($link) {
    $link = str_replace('" rel="prev">', '?previous" rel="prev">', $link);
    return $link;
    }

    Solid, thanks!

    Where in that code would I add my text? ie, if I wanted it to say “?APPLES” after the “next” URL, where does it go in there?

    Michael

    @alchymyth

    Forum Moderator

    in this line:

    $link = str_replace('" rel="next">', '?next" rel="next">', $link);

    you could replace ?next with ?APPLES

    Ah! got it, sorry, should have been obvious, thanks.

    Thanks Nickaster.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Modifying "next_post_link"’ is closed to new replies.
Skip to toolbar