views:

262

answers:

5

Hi,

so currently i'm using pods to create some individual pages for a log, filled with custom stuff.

now i want to use the comments-system for each of this pages e.g.:

mydomain.com/podpages/page1
mydomain.com/podpages/page2
mydomain.com/podpages/page3

this are not pages created with wordpress so simply adding <?php comments_template(); ?> is not working.

any ideas how to solve this problem? thanks in advance

please leave a comment if something is unclear :)

A: 

add

require('/path/to/wp-blog-header.php');

to include the wp files. this should give you all the functions/data you need.

Galen
yes i did that. but comments_template(); normally is only working inside a single blog or page post.
choise
true true, you might have to insert each page into your posts table and tweak the comments_table function to give it the id of the page
Galen
+8  A: 

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.

TheDeadMedic
so adding a page works, but the comment template dont gets displayed. i think wp(array('pagename' => $pagename)); still has no effect :(
choise
okay, dumping $wp says pagename is set, but comments still not showing up, also other things like permalink should work now, but they dont, or?
choise
'post_type' => 'post' is working better, but it would be 1000 times better to have them as pages
choise
Okay, stick with what I originally posted, but replace the `wp()` call with `$wp_query = new WP_Query(array('pagename' => $pagename))`. If that fails, globalise $wp_query just before with `global $wp_query;`
TheDeadMedic
no it says "comments are closed". getting closer. dumping the query gives me the right things. but still the global `$post` is not set, but i think its needed, isnt it? any idea ? :(
choise
Bingo! I think you need to add `'comment_status' => 'open'` to `wp_insert_post` argument array...
TheDeadMedic
no, i've already done that before. the strange thing is, `'post_type' => 'post'` works (not really , but better than page), an $post object is existing. changing it to page, $post is NULL.
choise
Each time you're running the test, are you deleting any old WP pages that were created? And have you logged in to WP to confirm that pages *are* being created, with comments open?
TheDeadMedic
shure i'm deleting them all. yes the pages are created and the comments are enabled. i also unchecked the comments checkbox, saved, checked it again, but nothing changed.
choise
I've nailed it - check revised answer. Sorry for all the hiccups!
TheDeadMedic
Updated again to fix comment redirect.
TheDeadMedic
oh damn. thats also not working. i think we are sooo close. should i mention that i'm using WordPress 2.7? but i think its not important in this case. using name instead of pagename also the if clause does not work and pages are generated and generated again. don't know how to thank you for your invested time, but at least i had no success. do you have another hint? =/ or can i post you a sample page or something?
choise
I would seriously advise upgrading to 2.9.2 - all the code I have provided has been tested in a 2.9.2 environment.
TheDeadMedic
A: 

Can you create pages in wordpress which display your log data? You might need a new template for this. WordPress will then have something to connect the comments to.

BillThor
A: 

Just supply the wordpress-comment-part with a new ID - start with something your usual posts will never reach (100.000+ is your pages i.e.)

I don't know exactly if in wordpress it's a function (saveComment i.e.), but if it is so, just use it in your page with he custom ID. You will nevertheless have to insert the Comments-form yourself.

And don't forget to modify the query that gets the news-entires that IDs over 100.000 are not entries.

Or you can write your own template that displays the standard-Worpress-stuff with IDs < 100.000, or else your pages.

Summed up, it should not be very difficult.

p.s.: If you just want to use the wordpress-login, then use any comment-system or make your own (it's an 1hour-thing) and authenticate / use the worpress-session.

ShoX
A: 

Do you need to use WordPress for this? If not, maybe something in this SO question helps: Unobtrusive, self-hosted comments function to put onto existing web pages

Pekka