2

I have a form that users fill out. When they finish, they are redirected to a 3rd party site to set up payment options. After completing the payment process, they are sent back to my site with a variety of $_POST variables.

My plugin needs to create a thank you page based on those POST variables. It's a simple little page with a title and a few paragraphs of text.

I do not want these pages to be stored in the database. They will be unique to each transaction.

I attached a function to init that parses the $_POST data (after sanitizing, of course.) That function creates two global variables: $my_title and $my_content I'm not sure how to turn those into a page for the user, though.

The payment system will send users to www.mysite.com/thank-you

So far, I have a filter on rewrite_rules_array to add my url:

function my_rewrite_rules( $rewrite_rules) {
    $rule = array('thank-you/?$' => 'index.php?formresult=thank-you');
    $rewrite_rules = $rule + $rewrite_rules;
    return $rewrite_rules
}

I filter query_vars to add mine:

function my_insert_qv($vars) {
    array_push($vars, 'formresult');
    return $vars;
}

Then, I have an action on template_redirect where I think the action should be. Maybe this is the wrong path.

function my_template_redirect() {
    gloval $wp;
    $qvs = $wp->query_vars;
    if (array_key_exists( 'laundry', $qvs && $qvs['laundry'] == 'thank-you' ) {
        global $wp_query;
        $wp_query->set( 'is_404', false );
        // ... now what?
    }
}

How can I show the user a page that loads the default page template from the active theme and shows my dynamic title and content?

3
  • Would you be okay with one page in the WordPress db? If so you could set up a single "thank you" page which uses a custom page template. Put the code in that template to pull in the args from the querystring and display your custom content.
    – Michelle
    Commented Sep 13, 2011 at 16:11
  • Yeah, that would work. I'm trying to do it all without touching the theme... if that's what has to happen, so be it. But, I'd like it to be one self-contained plugin.
    – Peter G
    Commented Sep 13, 2011 at 16:21
  • Ah, gotcha. My brain skipped right over the "My plugin needs to..." bit. :)
    – Michelle
    Commented Sep 13, 2011 at 16:54

1 Answer 1

1

Something like this should work, though I'm not sure how meta, title, etc.. will behave, you'll want to test everything thoroughly!

function my_template_redirect() {
    global $wp;
    $qvs = $wp->query_vars;
    if (array_key_exists( 'laundry', $qvs && $qvs['laundry'] == 'thank-you' ) {
        global $wp_query;
        $wp_query->set( 'is_404', false );
        // ... now what?

        $post = new stdClass();
        $post->ID= -99; // fake ID, hehe
        $post->post_content = 'some content';
        $post->post_excerpt = 'an excerpt';
        $post->post_status ='publish';
        $post->post_title = 'My fake page';
        $post->post_type = 'page';
        $wp_query->queried_object = $post;
        $wp_query->post = $post;
        $wp_query->found_posts = 1;
        $wp_query->post_count = 1;
        $wp_query->max_num_pages = 1;
        $wp_query->is_page = 1;
        $wp_query->is_404 = false;
        $wp_query->posts = array($post);
        $wp_query->page = 1;
        $wp_query->is_post = false;

    }
}
5
  • That looks like what I need, but it's not quite working for me. Looking at the debug info, I added lines for $post->post_name and $wp_query->queried_object_id. Now the result is it shows the blog index and gives the notice Trying to get property of non-object in .../wp-includes/post-template.php on line 485 It looks to be trying to look up the page with id = -99, which fails. Any ideas?
    – Peter G
    Commented Sep 13, 2011 at 21:08
  • I tried setting the fake page ID equal to 1. That cleared up the notice, but still just showing the blog index.
    – Peter G
    Commented Sep 13, 2011 at 21:15
  • @Peter G - sorry, don't know how to fix that, will have to do some tests myself later.
    – Milo
    Commented Sep 14, 2011 at 1:16
  • Thanks @Milo I've been trying to use it on all sorts of different hooks without luck. It seems like such a simple idea, but apparently not. I'd love an answer to this, but given time constraints I'll likely go the custom template route suggested above sometime tomorrow.
    – Peter G
    Commented Sep 14, 2011 at 2:08
  • The template method worked out for me and got the job done. I'm accepting this answer because it does show how to set up your own $post & $wp_query variables.
    – Peter G
    Commented Sep 17, 2011 at 17:59

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.