0

I am working on creating a comment system for an events plugin I am working on. I've got the main comments working fine with them saving to the database and displaying on the page. What I need help with now is displaying a form for replies when Reply is clicked below each message, then displaying the replies with the main comments. One issue I have is that I am on an older version of MySQL so I can't use a recursive query. Any help is appreciated. My code I have is below.

Form

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="commentform" class="commentform">
    <textarea name="comment" id="comment" class="commentbox" rows="5" cols="120" required></textarea><br />
    <input type="hidden" name="eventid" id="eventid" value="<?php echo $id; ?>" />
    <input type="hidden" name="userid" id="userid" value="<?php echo $userid; ?>" />
    <input type="hidden" name="action" value="commentform">
    <button class="commentbtn">Add Comment</button>
</form>

jQuery/AJAX

jQuery(function ($) {
    $('.commentform').submit(function () {
        event.preventDefault();
        var commentform = $('.commentform');
        $.ajax({
            url: commentform.attr('action'),
            data: commentform.serialize(), // form data
            type: commentform.attr('method'), // POST
            beforeSend: function (xhr) {
                commentform.find('button').text('Processing...');
            },
            success: function (data) {
                commentform.find('button').text('Add Comment');
                $(".commentform")[0].reset();
                $('#response').html(data); // insert data
            }
        });
        return false;
    }); 
});

PHP Function

add_action('wp_ajax_commentform', 'comment_submit');
add_action('wp_ajax_nopriv_commentform', 'comment_submit');
function comment_submit() {
    global $wpdb;
    date_default_timezone_set("America/New_York");
    $date = date('Y-m-d H:i:s');
    $inserttable = $wpdb->prefix . "event_comments";
    $eventid = $_POST["eventid"];
    $userid = $_POST["userid"];
    $comment = $_POST["comment"];

    $data = array(
        'eventid' => $eventid,
        'userid' => $userid,
        'content' => $comment,
        'datesaved' => $date,
    );

    $wpdb->insert($inserttable, $data);
}

Display Comments

$commentctr = $wpdb->get_var("SELECT count(*) FROM " . $wpdb->prefix . "cmc_communitybb_event_comments where eventid=$eventid");

echo "<h2>Comments</h2>";
echo "<h3>$commentctr Total Comments</h3>";

$commentsql = $wpdb->get_results("SELECT comm.id as id, comm.eventid as eventid, comm.userid as userid, comm.content as comment, comm.datesaved as date, users.meta_value as fname, users2.meta_value as lname
                                FROM " . $wpdb->prefix . "event_comments as comm
                                inner join " . $wpdb->prefix . "usermeta as users on comm.userid = users.user_id and users.meta_key = 'first_name'
                                inner join " . $wpdb->prefix . "usermeta as users2 on comm.userid = users2.user_id and users2.meta_key = 'last_name'
                                where comm.eventid=$eventid and comm.parentid is null order by comm.datesaved asc");

foreach ($commentsql as $comment) {
    $commid = $comment->id;
    $date = date_create($comment->date);
    $fordate = date_format($date, "m/d/Y");
    $fortime = date_format($date, "g:i a");
    ?>
    <div id="commentdiv<?php echo $commid; ?>" class="commentdiv">
        <b>Comment by: </b><?php echo $comment->fname . " " . $comment->lname; ?><br />
        <b>Date/Time: </b><?php echo $fordate . " - " . $fortime; ?><br />
        <?php  echo $comment->comment . "<br>"; ?>
        <a href="javascript:void(0);" id="replylink<?php echo $commid; ?>" class="replylink" onclick="showreplybox(<?php echo $commid; ?>)">REPLY</a>
    </div>
    <hr class="commenthr" />
    <?php
}
2
  • WordPress already has a robust built-in comments system. Is there a reason you're not just using that? Nov 1 at 1:35
  • Because this is for a custom events plugin I am creating, not for pages or posts created in WordPress. Is there an easy way to use the comment system from WordPress? I haven't been able to find a way to do this. Nov 1 at 12:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.