• Hi
    i use this code to add custom endpoint for page, but in wordpress 5.5 not working and remove_action(‘template_redirect’, ‘redirect_canonical’); return 404.

        // custom variables will only be available on fix page.
        $pageslug = 'fix';
    
        // get the actual request and parse it down
        if (isset($wp->request) && strpos($wp->request, '/')) {
            $requests = explode('/', $wp->request);
    
            // array_shift takes out the first path what is the page slug/name.
            // and make sure it's the popular page.
            if ($pageslug == array_shift($requests)) {
                // set pagename to make sure we have found a page with the name.
                $wp->set_query_var('pagename', $pageslug);
    
                // wordpress automatically try to check if it's an attachment when a parameter is present
                // after pageslug, so we set it false as we do the parsing ourselves.
                $wp->set_query_var('attachment', false);
    
                // we understand this is a valid page, and let WordPress understand there's no error.
                $wp->set_query_var('error', false);
    
                $count = count($requests);
    
                if ($count > 0) {
                    // stop the canonical redirection, if a url request isn't same as get_permalink( ) or
                    // the_permalink( ), then wordpress redirects it to the actual page
                    // and removes all parameter after slug name.
                    remove_action('template_redirect', 'redirect_canonical');
    
                    foreach ($requests as $i => $request) {
                        // this should be the key, and next one will be the value
                        if ($i == 0 || $i % 2 == 0) {
                            if (isset($requests[$i + 1]))
                                $val = $requests[$i + 1];
                            else
                                $val = '';
                            $wp->set_query_var('fix', $val);
                        }
                    }
                }
            }
        }
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    If you’ve suppressed the canonical redirect and your request goes 404, there’s something wrong with the resulting query. Find out what the SQL is that WP constructed through the “posts_request” filter. Something in the query criteria is causing nothing to be found. It ought to be apparent what that is. What that is will be a good clue to what needs to be done to resolve the problem.

Viewing 1 replies (of 1 total)
  • The topic ‘WordPress Page Add Custom Endpoint’ is closed to new replies.