You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please review the original issue and especially its comments. Comments here on closed issues will be ignored. Thanks.
Original description
The WordPress wp_get_shortlink() function has an inherent assumption that, if you pass in no ID or a zero ID, then it checks the current post and uses its ID to determine which post it should return a shortlink for. This is the case in a lot of WordPress "Loop" functions, where zero means "current post".
Thus, using a bare call to wp_get_shortlink() on, say, the main index template page (or any page where there's more than one post in the Loop) will fail to return the proper shortlink.
The problem lies in this code:
if( !$id ) {
global $wp_query;
$id = $wp_query->get_queried_object_id();
}
That's not the right way to get the ID for the current post in the Loop. That will work fine on single post pages only, anything that queries multiple posts will not have it return the proper ID for the current post.
To handle this correctly, the plugin needs to also get the $context parameter from that filter (which it is not currently getting) and then it should implement this code:
global $wp_query;
$post_id = 0;
if ( 'query' == $context && is_single() ) {
$post_id = $wp_query->get_queried_object_id();
} elseif ( 'post' == $context ) {
$post = get_post($id); // note, $id is passed in from the filter arguments
$post_id = $post->ID;
}
When the $context is "post", then the user is requesting the shortlink for a specific post. And in that case, it should get the current post ID from the loop, then return a shortlink to that single post page.
When the $context is "query", then the user is requesting the shortlink for the current page, whatever it is. That can be the home index page, or an archives page, or whatever. As you see, this needs special handling for the is_single case, because then you are returning the same shortlink as for the "post" $context.
This will also allow the plugin to be able to generate shortlinks for non-single-post pages, including the home page, BTW. Just check for that "query" $context type.
The text was updated successfully, but these errors were encountered:
This is a COPY of Issue 500: YOURLS WP plugin doesn't handle the case of being called from inside the Loop properly., filed on Google Code before the project was moved on Github.
Please review the original issue and especially its comments. Comments here on closed issues will be ignored. Thanks.
Original description
The text was updated successfully, but these errors were encountered: