Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 921
choise Avatar asked Dec 23 '22 02:12

choise


1 Answers

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.

like image 135
TheDeadMedic Avatar answered Jan 11 '23 05:01

TheDeadMedic