When a comment is stored in the WordPress database, the ID of the post (or page) the comment relates to is also stored.
Trouble is, you're trying to save comments using WordPress, but for a page that it doesn't actually know about.
So, how about we create a WordPress page for each real page, but merely as a representation, so that your real pages and WordPress have a common ground for working with each other.
So, the plan here is to;
- Load WordPress in the background on each of the 'real' pages.
- See if a WordPress page representation already exists for the 'real' page
- If it doesn't, create it, then and there
- Trick WordPress into thinking we're actually viewing the representation
- Carry on using all of WP's functions and 'template tags' as you would normally
This code should be somewhere at the beginning of the template file used to render your 'real' pages;
include ('../path/to/wp-load.php');
// remove query string from request
$request = preg_replace('#\?.*$#', '', $_SERVER['REQUEST_URI']);
// try and get the page name from the URI
preg_match('#podpages/([a-z0-9_-]+)#', $matches);
if ($matches && isset($matches[1])) {
$pagename = $matches[1];
// try and find the WP representation page
$query = new WP_Query(array('pagename' => $pagename));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename
));
if (!$id)
do_something(); // something went wrong
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
}
UPDATE
I can't believe I was this stupid. Below should replace current code inside outer if
;
// try and find the WP representation page - post_type IS required
$query = new WP_Query(array('name' => $pagename, 'post_type' => 'page'));
if (!$query->have_posts()) {
// no WP page exists yet, so create one
$id = wp_insert_post(array(
'post_title' => $pagename,
'post_type' => 'page',
'post_status' => 'publish',
'post_name' => $pagename,
'post_author' => 1, // failsafe
'post_content' => 'wp_insert_post needs content to complete'
));
}
// this sets up the main WordPress query
// from now on, WordPress thinks you're viewing the representation page
// post_type is a must!
wp(array('name' => $pagename, 'post_type' => 'page'));
// set up post
the_post();
P.S I think using the query_var name
over pagename
is better suited - it queries the slug, rather than the slug 'path'.
You'll also need to either place an input inside the form with name redirect_to
and a value of the URL you'd like to redirect to, or, filter the redirect with a function hooked onto comment_post_redirect
, returning the correct URL.