Make WordPress Core

Opened 15 years ago

Closed 12 years ago

#9963 closed defect (bug) (invalid)

get_permalink() returns wrong link outside The Loop

Reported by: fmorel90's profile fmorel90 Owned by: ryan's profile ryan
Milestone: Priority: high
Severity: normal Version: 2.8
Component: Permalinks Keywords:
Focuses: Cc:

Description

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.

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.

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.

If I take out The Loop on my homepage, it returns the link to my latest post.

Change History (10)

#1 @fmorel90
15 years ago

  • Summary changed from get_permalink() returns wrong link after The Loop to get_permalink() returns wrong link outside The Loop

#2 @ryan
15 years ago

What version of 2.8? Some related bugs have been fixed recently. Could you try with the latest nightly or latest svn?

#3 @fmorel90
15 years ago

I'm using beta 2. I'm away from laptop right now, but I'll try an update tonight or tomorrow morning.

#4 @fmorel90
15 years ago

I just downloaded a nightly and retried several pages. I still have the same problem.

#5 @ryan
15 years ago

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.

#6 @ryan
15 years ago

  • Resolution set to invalid
  • Status changed from new to closed
esc_url( wp_logout_url( $_SERVER['REQUEST_URI'] ) )

You probably need something more like that. get_permalink() is not intended to be used like this.

#7 @fmorel90
15 years ago

Thanks, I'll try that tomorrow when I have time at home. I'm still new with Wordpress and PHP.

#8 @fmorel90
15 years ago

Thank you very much. That worked perfectly.

#9 @iLobster
12 years ago

  • Resolution invalid deleted
  • Severity changed from major to normal
  • Status changed from closed to reopened
  • Version changed from 2.8 to 3.4

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 here or relying on different combinations of $_SERVER which are not always uniform and reliable.

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

#10 @johnbillion
12 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed
  • Version changed from 3.4 to 2.8

iLobster, get_permalink() 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.

I had thought there was a ticket open suggesting a get_current_url() function, but it looks like I'm mistaken. The code is quite simple though:

$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
Note: See TracTickets for help on using tickets.