How to create a dynamic page based on form data with a plugin?

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?

1 Answer
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;

    }
}

Leave a Comment