0

I am developing a e-commerce theme that requires an order page. I created a php file inside my theme folder named order-page.php and hope to access this page from the wordpress root url (example.com/order-page)

add_action('template_redirect', 'template_redirect');
function template_redirect(){
    $basename = basename($_SERVER['QUERY_STRING']);
    echo $basename;
    if($basename=='order-page'){
        loadCustomTemplate(TEMPLATEPATH."/$basename.php");
    }
}

function loadCustomTemplate($template) {
    global $wp_query;
    if(!file_exists($template)){
        return;
    }
    $wp_query->is_page = true;
    $wp_query->is_single = false;
    $wp_query->is_home = false;
    $wp_query->comments = false;
    // if we have a 404 status
    if ($wp_query->is_404) {
    // set status of 404 to false
        unset($wp_query->query["error"]);
        $wp_query->query_vars["error"]="";
        $wp_query->is_404=false;
    }
    // change the header to 200 OK
    //header("HTTP/1.1 200 OK");
    //load our template
    include($template);
    exit;
}

Problem#1: with the function listed above, I can only use example.com/?order-page to get what I want. If I use example.com/order-page, I will get url not found error.

Problem#2: when I have multiple parameters, this function doesn't work anymore. For example, if I have:

example.com/?order-page&product_id=001&type=digital

$basenamewill return order-page&product_id=001&type=digital

Using $query_array=explode('&',$basename) and get $query_array[0] is one possible solution. Is there a better way to do this?

1
  • 3
    you could add a query var and rewrite rule, but you're better off creating a page rather than "faking" one this way. I can immediately think of 10 different issues with this- menus, widgets, sitemap, caching, plugin integration, etc.. store the ID of the order page in an option so users can choose the page, and/or auto-insert the new page.
    – Milo
    Commented Nov 20, 2014 at 23:26

2 Answers 2

1

Problem#1:

You can create a customized php file for your page using a Page Template

Page Templates

WordPress looks for template files in the following order:

  1. Page Template — If the page has a custom template assigned, WordPress looks for that file and, if found, uses it.
  2. page-{slug}.php — If no custom template has been assigned, WordPress looks for and uses a specialized template that contains the page’s slug.
  3. page-{id}.php — If a specialized template that includes the page’s slug is not found, WordPress looks for and uses a specialized template named with the page’s ID.
  4. page.php — If a specialized template that includes the page’s ID is not found, WordPress looks for and uses the theme’s default page template.
  5. index.php — If no specific page templates are assigned or found, WordPress defaults back to using the theme’s index file to render pages.

In your scenario, your page template file would be page-order-page.php, wordpress would then load that file for when someone accesses (example.com/order-page).

Problem#2:

For problem 2, you would need to register query variables using the query_vars filter.

function wpse169050_register_query_vars( $vars ) {
    $vars[] = 'product_id';
    $vars[] = 'type';
    return $vars;
} 
add_filter( 'query_vars', 'wpse169050_register_query_vars' );

Your order url with the variables would then be

example.com/order-page/?product_id=001&type=digital

You can then get the value for both variables using get_query_var in your template file:

$product_id = get_query_var('product_id');
$type       = get_query_var('type');
0

You should take a look at the WordPress hierarchy page in the documentation. Here it's the relevant part:

page-{slug}.php 

If the page slug is recent-news, WordPress will look to use:

page-recent-news.php

http://codex.wordpress.org/Template_Hierarchy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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