开发者

Wordpress - use comment-system outside of pages and posts

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 :)


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.


add

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

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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜