0

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;
}
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.

  • Thanks @Milo for the answer. However, there was a little confusion(typo). My bad. deal.php and single-deal.php are the same pages. Please check the updated question. Thanks :) – PythonEnthusiast Dec 16 '18 at 17:21
  • I think you're confusing pages and templates. Your rewrite rule should point to index.php, and the pagename query var should point to a page slug. That page is then associated with a template. – Milo Dec 16 '18 at 18:18
  • How and where can I find page slug (associated to template single-deal.php)? – PythonEnthusiast Dec 16 '18 at 18:52
  • I tried echo get_page_template_slug($post->ID); in my template file (ie single-deal.php), but it shows nothing. – PythonEnthusiast Dec 16 '18 at 18:54
  • Templates don't have page slugs. You log in to admin interface, go to Pages, create a page, give it a slug. There is a meta box on the page edit screen that lets you select template, you choose your custom template. Now you use the slug of this newly created page in your rewrite rule. When the page loads, WordPress loads your template. – Milo Dec 16 '18 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.