Skip to content

Instantly share code, notes, and snippets.

@i-like-robots
Created August 21, 2012 10:01

Revisions

  1. i-like-robots revised this gist Sep 24, 2012. 2 changed files with 81 additions and 40 deletions.
    9 changes: 0 additions & 9 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -11,12 +11,3 @@ function comment_form_hidden_fields()
    wp_nonce_field( 'unfiltered-html-comment_' . get_the_ID(), '_wp_unfiltered_html_comment', false );
    }
    }

    /**
    * Get current (active) user
    */
    function get_active_user()
    {
    global $current_user;
    return $current_user;
    }
    112 changes: 81 additions & 31 deletions wordpress-custom-comment-form.php
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,86 @@
    <?php if ( comments_open() ) : ?>
    <?php
    $comments = get_comments( array(
    'post_id' => get_the_ID(),
    'status' => 'approve'
    ) );
    ?>

    <form action="<?php echo site_url('/wp-comments-post.php') ?>" method="post" id="comments">
    <ul class="form-collection">
    <?php if ( is_user_logged_in() ) : ?>
    <li>
    <span class="field-hint">Logged in as <?php echo get_active_user()->user_login ?></span>
    <div id="comments">

    <h3><?php echo count($comments) ?> Comments</h3>

    <?php WPTheme::comment_form_unapproved() ?>

    <?php if ( count($comments) ) : ?>

    <ol class="base comment-list">
    <?php foreach ( $comments as $comment ) : ?>
    <li id="comment-<?php echo $comment->comment_post_ID ?>">
    <div class="comment">
    <h6 class="comment-title"><?php echo $comment->comment_author ?> said:</h6>
    <p class="comment-date">
    Posted on <time datetime="<?php echo $comment->comment_date ?>" itemprop="datePublished"><?php echo date('d.m.Y', strtotime($comment->comment_date) ) ?></time>
    at <?php echo date('H:i', strtotime($comment->comment_date) ) ?>
    </p>
    <div class="comment-body">
    <?php echo $comment->comment_content ?>
    </div>
    </div>
    </li>
    <?php else : ?>
    <?php endforeach ?>
    </ol>

    <?php else : ?>

    <p>
    There are no responses to this article, why not be the first?
    </p>

    <?php endif ?>

    <?php if ( comments_open() ) : ?>

    <form action="<?php echo site_url('/wp-comments-post.php') ?>" method="post">
    <ul class="form-collection">
    <?php if ( is_user_logged_in() ) : ?>
    <li>
    <span class="field-hint">Logged in as <?php echo get_user_option('user_nicename') ?></span>
    </li>
    <?php else : ?>
    <li>
    <label for="comment-author" class="field-label">Name</label>
    <input type="text" name="author" id="comment-author" required />
    </li>
    <li>
    <label for="comment-email" class="field-label">Email</label>
    <input type="email" name="email" id="comment-email" required />
    </li>
    <?php endif ?>
    <li>
    <label for="comment-author" class="field-label">Name</label>
    <input type="text" name="author" id="comment-author" />
    <label for="comment-body" class="field-label">Comment</label>
    <textarea name="comment" id="comment-body" required></textarea>
    </li>
    <li>
    <label for="comment-email" class="field-label">Email</label>
    <input type="email" name="email" id="comment-email" />
    <?php if ( ! is_user_logged_in() ) : ?>
    <li>
    <label for="comment-query" class="field-label">20 - 15 + 7 =</label>
    <input type="text" name="result" id-"comment-query" required />
    </li>
    <?php endif ?>
    <li class="button-field">
    <button type="submit">Post comment</button>
    <?php comment_form_hidden_fields() ?>
    </li>
    <?php endif ?>
    <li>
    <label for="comment-body" class="field-label">Comment</label>
    <textarea name="comment" id="comment-body"></textarea>
    </li>
    <li class="button-field">
    <input type="submit" name="submit" value="Post comment" />
    <?php comment_form_hidden_fields() ?>
    </li>
    </ul>
    </form>

    <?php else : ?>

    <p class="nocomments">
    Comments are closed for this article.
    </p>

    <?php endif ?>
    </ul>
    </form>

    <?php else : ?>

    <hr />

    <p class="nocomments">
    Comments are closed for this article.
    </p>

    <?php endif ?>

    </div>
  2. i-like-robots revised this gist Aug 21, 2012. 2 changed files with 25 additions and 10 deletions.
    22 changes: 22 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php
    /**
    * Comment form hidden fields
    */
    function comment_form_hidden_fields()
    {
    comment_id_fields();

    if ( current_user_can( 'unfiltered_html' ) )
    {
    wp_nonce_field( 'unfiltered-html-comment_' . get_the_ID(), '_wp_unfiltered_html_comment', false );
    }
    }

    /**
    * Get current (active) user
    */
    function get_active_user()
    {
    global $current_user;
    return $current_user;
    }
    13 changes: 3 additions & 10 deletions wordpress-custom-comment-form.php
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@

    <form action="<?php echo site_url('/wp-comments-post.php') ?>" method="post" id="comments">
    <ul class="form-collection">
    <?php if ( is_user_logged_in() ) : global $current_user; ?>
    <?php if ( is_user_logged_in() ) : ?>
    <li>
    <span class="field-hint">Logged in as <?php echo $current_user->user_login ?></span>
    <span class="field-hint">Logged in as <?php echo get_active_user()->user_login ?></span>
    </li>
    <?php else : ?>
    <li>
    @@ -22,14 +22,7 @@
    </li>
    <li class="button-field">
    <input type="submit" name="submit" value="Post comment" />
    <?php
    comment_id_fields();

    if ( current_user_can( 'unfiltered_html' ) )
    {
    wp_nonce_field( 'unfiltered-html-comment_' . get_the_ID(), '_wp_unfiltered_html_comment', false );
    }
    ?>
    <?php comment_form_hidden_fields() ?>
    </li>
    </ul>
    </form>
  3. i-like-robots created this gist Aug 21, 2012.
    43 changes: 43 additions & 0 deletions wordpress-custom-comment-form.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    <?php if ( comments_open() ) : ?>

    <form action="<?php echo site_url('/wp-comments-post.php') ?>" method="post" id="comments">
    <ul class="form-collection">
    <?php if ( is_user_logged_in() ) : global $current_user; ?>
    <li>
    <span class="field-hint">Logged in as <?php echo $current_user->user_login ?></span>
    </li>
    <?php else : ?>
    <li>
    <label for="comment-author" class="field-label">Name</label>
    <input type="text" name="author" id="comment-author" />
    </li>
    <li>
    <label for="comment-email" class="field-label">Email</label>
    <input type="email" name="email" id="comment-email" />
    </li>
    <?php endif ?>
    <li>
    <label for="comment-body" class="field-label">Comment</label>
    <textarea name="comment" id="comment-body"></textarea>
    </li>
    <li class="button-field">
    <input type="submit" name="submit" value="Post comment" />
    <?php
    comment_id_fields();

    if ( current_user_can( 'unfiltered_html' ) )
    {
    wp_nonce_field( 'unfiltered-html-comment_' . get_the_ID(), '_wp_unfiltered_html_comment', false );
    }
    ?>
    </li>
    </ul>
    </form>

    <?php else : ?>

    <p class="nocomments">
    Comments are closed for this article.
    </p>

    <?php endif ?>