Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YOURLS WP plugin doesn't handle the case of being called from inside the Loop properly. #500

Closed
ozh opened this issue Apr 6, 2013 · 0 comments

Comments

@ozh
Copy link
Member

ozh commented Apr 6, 2013

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 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.
@ozh ozh closed this as completed Apr 6, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant