redirect - Custom URL routes

admin2025-06-05  4

I'm a newbie in Wordpress, so excuse me if I ask a noob question.

I want to redirect some of the URLs to a specific page (single-deal.php). Now, these set of URLs (regex provided for these URLS) does not exist on my server. So, currently my WP returns 404 error. However, I still want to load single-deal.php and let it decide.

My single-deal.php fires a GET request to an API, and my processing will follow thereafter.

For eg:- If I have an url (for eg) /properties/1/ and it's permalink is not available in my WP settings, I want it load single-deal.php page, and let my API decide the further processing.

Here's what I've tried reading on multiple other questions:-

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'properties/([0-9]+)/?$',
        'single-deal.php?pagename=single-deal&property_id=$matches[1]',
        'top' );
}

add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
    $query_vars[] = 'property_id';
    return $query_vars;
}

I'm a newbie in Wordpress, so excuse me if I ask a noob question.

I want to redirect some of the URLs to a specific page (single-deal.php). Now, these set of URLs (regex provided for these URLS) does not exist on my server. So, currently my WP returns 404 error. However, I still want to load single-deal.php and let it decide.

My single-deal.php fires a GET request to an API, and my processing will follow thereafter.

For eg:- If I have an url (for eg) /properties/1/ and it's permalink is not available in my WP settings, I want it load single-deal.php page, and let my API decide the further processing.

Here's what I've tried reading on multiple other questions:-

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'properties/([0-9]+)/?$',
        'single-deal.php?pagename=single-deal&property_id=$matches[1]',
        'top' );
}

add_filter( 'query_vars', 'wpse26388_query_vars' );
function wpse26388_query_vars( $query_vars ){
    $query_vars[] = 'property_id';
    return $query_vars;
}
Share Improve this question edited Dec 16, 2018 at 17:19 Praful Bagai asked Dec 16, 2018 at 11:46 Praful BagaiPraful Bagai 1013 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

All internal rewrite rules must point to index.php. This isn't a theme file, it's the main bootstrap file in the root of your WordPress install. So, your rule should look like:

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'properties/([0-9]+)/?$',
        'index.php?pagename=single-deal&property_id=$matches[1]',
        'top'
    );
}

You can then assign the deal.php template to your single-deal page in the admin interface, or you can use the page_template filter if you want to apply conditional template loading, like for example only if your property_id query var is set.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749093449a316307.html

最新回复(0)