WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 22 months ago

#9963 closed defect (bug) (invalid)

get_permalink() returns wrong link outside The Loop

Reported by: fmorel90 Owned by: 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)

comment:1 fmorel905 years ago

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

comment:2 ryan5 years ago

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

comment:3 fmorel905 years ago

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

comment:4 fmorel905 years ago

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

comment:5 ryan5 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.

comment:6 ryan5 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.

comment:7 fmorel905 years ago

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

comment:8 fmorel905 years ago

Thank you very much. That worked perfectly.

comment:9 iLobster22 months 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

comment:10 johnbillion22 months 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.