admin管理员组

文章数量:1122846

I'm trying to create a subpost location - like /embed or /feed - for photo galleries.

// this defines the location logic
if (!function_exists('is_gallery')) {
function is_gallery() {
    $uri = $_SERVER['REQUEST_URI'];
    $parts = array_values(array_filter(explode('/',$uri)));
        if(end($parts) == 'gallery') {
        return true;
        }
    }
}

// this loads the template
add_action('template_include','gallery_page');
function gallery_page($template) {
    if(is_gallery()) {
        status_header(201);
            global $wp_query;
            $wp_query->is_404  = false;
            $wp_query->is_singular  = true;
        $new_template = locate_template(array('gallery.php'));
        if ('' != $new_template) {return $new_template;}
    }
return $template;
}

So whatever the template outputs is available at example/post-name/gallery

It works fine like this, but notice the 201 (could be 203 as well) http status header. If I use the appropriate 200, it no longer works, as wordpress does some random 302 redirect, usually to the parent url but not always.

What I'm trying to do is prevent that redirection. It's possibly triggered by wp_safe_redirect() in wp-includes/pluggable.php but from there I couldn't figure out a filter to stop it.

Disabling canonical redirects – with add_filter('redirect_canonical', '__return_false'); or it's 'remove...' variations – doesn't help either.

And neither does setting true some random values of wp_query, but maybe there's something I'm missing.

So what I want is to stop that redirection or somehow have this work with a proper status of 200.

本文标签: wp queryHow to prevent random 302 canonicalish redirect on custom template