How To Get the URL of the page you are on with PHP (5 posts)

  1. backpages
    Posted 7 years ago #

    I've built a little custom "send to a friend" pop-up where someone can enter their name and the email of a friend and send them a message about a particular post. I want to automatically include a link to the page they are on in the email, but I'm having trouble figuring out how to grab the url for the page they are on. Is there a WordPress function to do this? I tried using the basic PHP function echo $_SERVER['PHP_SELF'] which usually returns the page you are on, but it just seems to return index in this case. Any help would be greatly appreciate. Thanks much!

  2. moshu
    Posted 7 years ago #

    There is a plugin for everythnig. (Almost.) Just needs a bit of search...

  3. backpages
    Posted 7 years ago #

    Thanks, but my client had specific needs that this plugin couldn't handle. I figured it out. <?php echo the_permalink() ?>

  4. mattifesto
    Posted 6 years ago #

    the_permalink() will only work for single posts, not pages with multiple posts or archives. To do that, use the PHP $_SERVER global.

    When I came to this topic, I was looking for how to make my sites return to the current page after a user logged in, instead of the admin page. Here is the solution to that specific question:


    This will create a log in or log out link (as appropriate) that will return the user to the current page.

    There's more discussion on the topic in this bug:


  5. Mark / t31os

    Posted 6 years ago #

    Yes it will work for lists of posts, providing they use a regular loop..

    the_permalink() is a loop function, it's made for use "in the loop".

    If you have a non-standard loop, you can just as easily use...

    echo get_permalink($ID); where $ID is a variable where you're storing the ID for the "current" page/post in your custom loop or you can again use this with a single entry, just as long as you pass the ID into the fucntion.

    For what's described above, get_permalink would be better suited.

    For what it's worth, using the request uri you'd need to add the HTTP address on to..

    echo "http://" . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];

    The request URI does not store the http address, just the query string etc...

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.