WordPress Trac: Ticket #9963: get_permalink() returns wrong link outside The Loophttps://core.trac.wordpress.org/ticket/9963
<p>
I'm using wp_logout_url(get_permalink()) in a sidebar widget to let users logout and return to the current page.
When viewing a single post, get_permalink() returns the posts' correct link.
</p>
<p>
However, on my home page, on archive pages, and on search results pages, get_permalink() returns the link of the last post in The Loop used to display posts in the page. I've checked both my theme and the default theme to see if I'd screwed up The Loop, but the default theme does this as well.
</p>
<p>
I am unsure if it worked correctly in 2.7. It's been a few months since I played with WordPress and I just started again with 2.8 a few days ago. However, I'm sure I would have noticed this before, so I'm putting this as a bug in 2.8.
</p>
<p>
If I take out The Loop on my homepage, it returns the link to my latest post.
</p>
en-usWordPress Trachttps://core.trac.wordpress.org/chrome/site/your_project_logo.pnghttps://core.trac.wordpress.org/ticket/9963
Trac 1.0.1fmorel90Thu, 28 May 2009 19:03:08 GMTsummary changedhttps://core.trac.wordpress.org/ticket/9963#comment:1
https://core.trac.wordpress.org/ticket/9963#comment:1
<ul>
<li><strong>summary</strong>
changed from <em>get_permalink() returns wrong link after The Loop</em> to <em>get_permalink() returns wrong link outside The Loop</em>
</li>
</ul>
TicketryanThu, 28 May 2009 19:47:12 GMThttps://core.trac.wordpress.org/ticket/9963#comment:2
https://core.trac.wordpress.org/ticket/9963#comment:2
<p>
What version of 2.8? Some related bugs have been fixed recently. Could you try with the latest nightly or latest svn?
</p>
Ticketfmorel90Thu, 28 May 2009 22:10:22 GMThttps://core.trac.wordpress.org/ticket/9963#comment:3
https://core.trac.wordpress.org/ticket/9963#comment:3
<p>
I'm using beta 2. I'm away from laptop right now, but I'll try an update tonight or tomorrow morning.
</p>
Ticketfmorel90Fri, 29 May 2009 01:44:55 GMThttps://core.trac.wordpress.org/ticket/9963#comment:4
https://core.trac.wordpress.org/ticket/9963#comment:4
<p>
I just downloaded a nightly and retried several pages. I still have the same problem.
</p>
TicketryanFri, 29 May 2009 02:48:23 GMThttps://core.trac.wordpress.org/ticket/9963#comment:5
https://core.trac.wordpress.org/ticket/9963#comment:5
<p>
get_permalink() does not create links to the current page. It only creates links to posts. On a page with multiple posts, returning a link to the last post is about the only thing it can do in this context.
</p>
TicketryanFri, 29 May 2009 02:52:53 GMTstatus changed; resolution sethttps://core.trac.wordpress.org/ticket/9963#comment:6
https://core.trac.wordpress.org/ticket/9963#comment:6
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>invalid</em>
</li>
</ul>
<pre class="wiki">esc_url( wp_logout_url( $_SERVER['REQUEST_URI'] ) )
</pre><p>
You probably need something more like that. get_permalink() is not intended to be used like this.
</p>
Ticketfmorel90Fri, 29 May 2009 14:37:05 GMThttps://core.trac.wordpress.org/ticket/9963#comment:7
https://core.trac.wordpress.org/ticket/9963#comment:7
<p>
Thanks, I'll try that tomorrow when I have time at home. I'm still new with Wordpress and PHP.
</p>
Ticketfmorel90Sat, 30 May 2009 19:17:13 GMThttps://core.trac.wordpress.org/ticket/9963#comment:8
https://core.trac.wordpress.org/ticket/9963#comment:8
<p>
Thank you very much. That worked perfectly.
</p>
TicketiLobsterThu, 28 Jun 2012 08:52:04 GMTstatus, version, severity changed; resolution deletedhttps://core.trac.wordpress.org/ticket/9963#comment:9
https://core.trac.wordpress.org/ticket/9963#comment:9
<ul>
<li><strong>status</strong>
changed from <em>closed</em> to <em>reopened</em>
</li>
<li><strong>version</strong>
changed from <em>2.8</em> to <em>3.4</em>
</li>
<li><strong>resolution</strong>
<em>invalid</em> deleted
</li>
<li><strong>severity</strong>
changed from <em>major</em> to <em>normal</em>
</li>
</ul>
<p>
ryan: your solution may work for the specific presented problem, but I think there needs to be a uniform way to pull the current page's permalink, or more basically ID (with the id you can access the permalink) outside the loop without running custom queries such as <a class="ext-link" href="http://wordpress.org/support/topic/how-to-get-page-id-using-php?replies=5"><span class="icon">​</span>here</a> or relying on different combinations of $_SERVER which are not always uniform and reliable.
</p>
<p>
My test case is a self submitting form where I prefer not to leave the action attribute empty. As the page ID is processed in the beginning of each page, it would make sense to copy it to another veritable that doesn't get overridden by the loop
</p>
TicketjohnbillionThu, 28 Jun 2012 09:03:37 GMTstatus, version changed; resolution sethttps://core.trac.wordpress.org/ticket/9963#comment:10
https://core.trac.wordpress.org/ticket/9963#comment:10
<ul>
<li><strong>status</strong>
changed from <em>reopened</em> to <em>closed</em>
</li>
<li><strong>version</strong>
changed from <em>3.4</em> to <em>2.8</em>
</li>
<li><strong>resolution</strong>
set to <em>invalid</em>
</li>
</ul>
<p>
iLobster, <tt>get_permalink()</tt> is not the function you should be using in this case. What you're really asking for is a function which returns the current URL.
</p>
<p>
I had thought there was a ticket open suggesting a <tt>get_current_url()</tt> function, but it looks like I'm mistaken. The code is quite simple though:
</p>
<pre class="wiki">$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
</pre>
Ticket