There are numerous plugins out there that enhance the "outta-the-box" WordPress comments form in various ways. But, if you are looking for very specific functionality, you may not have considered simply creating your own from scratch.

You've no doubt heard about all the negatives to customizing any files within the WordPress "core", as they would be overwritten by any upgrade you apply.

But, by creating your own comments form within your theme's loop-single.php or single.php, you can get around this issue.

High-level, we need three things:

  1. To modify the loop-single.php or single.php file and add our own comments form.
  2. In WordPress Admin -> Discussion, turn off allowing new comments (essentially hiding the default WordPress comments form)
  3. Create your own script for validating, then adding the comment to the WordPress comments table.

Creating the comments form:

<form action="/your_comments_processing_scipt.php" method="post">
    <input type="hidden" name="came_from" value="<?php echo $_SERVER['REDIRECT_URL']; ?>" />
    <input type="hidden" name="post_id" value="<?php echo $post->ID; ?>" />
    <div>
        <label for="comment-fullname">Name</label>
        <input type="text" name="fullname" id="comment-fullname" value="<?php echo $_REQUEST['fullname']; ?>" />
    </div>
    <div>
        <label for="comment-email">Email</label>
        <input type="email" name="email" id="comment-email" value="<?php echo $_REQUEST['email']; ?>" />               
    </div>
    <div>
        <label for="comment-text">Comment</label>
        <textarea name="comment" id="comment-text"><?php echo $_REQUEST['comment']; ?></textarea>               
    </div>
    <input type="submit" value="Submit" />
</form>

The Processing Script

<?php
// your validation goes here

require('wp-config.php');
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME);


$sql = "INSERT INTO wp_comments
        (
            comment_post_ID,
            comment_author,
            comment_author_email,
            comment_author_url,
            comment_author_IP,
            comment_date,
            comment_date_gmt,
            comment_content,
            comment_karma,
            comment_approved,
            comment_agent,
            comment_type,
            comment_parent,
            user_id,
            fbconnect
        )
        VALUES
        (
        {$_REQUEST['post_id']},
        '{$_REQUEST['fullname']}',       
        '{$_REQUEST['email']}',
        '',
        '{$_SERVER['REMOTE_ADDR']}',
        NOW(),
        UTC_TIMESTAMP(),
        '{$_REQUEST['comment']}',
        0,
        '0',
        '{$_SERVER['HTTP_USER_AGENT' ]}',
        '',
        0,
        0,
        0
        )";

$result = mysql_query($sql);
mysql_close($conn);
header('Location: ' . $_REQUEST['came_from']);
?>

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *