$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>redirect - Custom URL routes|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

redirect - Custom URL routes

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far