I want to display the comment form of a specific page on a sidebar.

It will look like this roughly.

enter image description here

enter image description here

I tried modifying from comments.php and putting it on sidebar.php.

But nothing shows up. Help please?

<?php $aspirasi = get_post(2); ?> 
<?php if ( 'open' == $aspirasi->comment_status ) : ?>
            <div id="respond">
                <h3><?php comment_form_title( __('Post a Comment', 'newtheme'), __('Post a Reply to %s', 'newtheme') ); ?></h3>

                <div class="formcontainer">  

                    <form id="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post">

<?php if ( $user_ID ) : ?>
                        <p id="login"><?php printf(__('<span class="loggedin">Logged in as <a href="%1$s" title="Logged in as %2$s">%2$s</a>.</span> <span class="logout"><a href="%3$s" title="Log out of this account">Log out?</a></span>', 'newtheme'),
                            get_option('siteurl') . '/wp-admin/profile.php',
                            wp_specialchars($user_identity, true),
                            wp_logout_url(get_permalink()) ) ?></p>

<?php else : ?>

                        <p id="comment-notes"><?php _e('Your email is <em>never</em> published nor shared.', 'newtheme') ?> <?php if ($req) _e('Required fields are marked <span class="required">*</span>', 'newtheme') ?></p>

          <div id="form-section-author" class="form-section">
                            <div class="form-label"><label for="author"><?php _e('Name', 'newtheme') ?></label> <?php if ($req) _e('<span class="required">*</span>', 'newtheme') ?></div>
                            <div class="form-input"><input id="author" name="author" type="text" value="<?php echo $comment_author ?>" size="30" maxlength="20" tabindex="3" /></div>
          </div><!-- #form-section-author .form-section -->

          <div id="form-section-email" class="form-section">
                            <div class="form-label"><label for="email"><?php _e('Email', 'newtheme') ?></label> <?php if ($req) _e('<span class="required">*</span>', 'newtheme') ?></div>
                            <div class="form-input"><input id="email" name="email" type="text" value="<?php echo $comment_author_email ?>" size="30" maxlength="50" tabindex="4" /></div>
          </div><!-- #form-section-email .form-section -->

          <div id="form-section-url" class="form-section">
                            <div class="form-label"><label for="url"><?php _e('Website', 'newtheme') ?></label></div>
                            <div class="form-input"><input id="url" name="url" type="text" value="<?php echo $comment_author_url ?>" size="30" maxlength="50" tabindex="5" /></div>
          </div><!-- #form-section-url .form-section -->

<?php endif /* if ( $user_ID ) */ ?>

          <div id="form-section-comment" class="form-section">
                            <div class="form-label"><label for="comment"><?php _e('Comment', 'newtheme') ?></label></div>
                            <div class="form-textarea"><textarea id="comment" name="comment" cols="45" rows="8" tabindex="6"></textarea></div>
          </div><!-- #form-section-comment .form-section -->

          <div id="form-allowed-tags" class="form-section">
              <p><span><?php _e('You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:', 'newtheme') ?></span> <code><?php echo allowed_tags(); ?></code></p>

<?php do_action('comment_form', $aspirasi->ID); ?>

                        <div class="form-submit"><input id="submit" name="submit" type="submit" value="<?php _e('Post Comment', 'newtheme') ?>" tabindex="7" /><input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /></div>
                     </form><!-- #commentform -->
                </div><!-- .formcontainer -->
            </div><!-- #respond -->
<?php endif /* if ( 'open' == $post->comment_status ) */ ?>
        </div><!-- #comments -->

Don't build your own form and don't modify comments.php - it's purpose is little bit different and I don't think you should include it inside sidebar.

Just put comment_form function call in your sidebar template. It takes 2 parameters: args and post_id.

So you can do it like this:

$args = array(
    ... // your custom arguments (you can leave it empty
comment_form( $args, $post_id );

Where should you place this code? Somewhere in your sidebar template. (sidebar.php or something like that, but it depends on your theme).

| improve this answer | |
  • It's just a query for further knowledge, why it is discouraging to modify or even to build a new comment form [within methodology]? – Mayeenul Islam Jul 26 '13 at 13:25
  • 2
    WordPress can change the way it processess comment forms. If you'll build your custom form, then it won't work after such change. On the other hand there is WordPress function to do this, and it will still work. If there is WP function to do something, you always should use it to be as compatible with WP as you can. Otherwise you can cause many problems after WP updates or break some plugins (because your custom form won't use default WP filters). – Krzysiek Dróżdż Jul 26 '13 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.