Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using version 3.3.1 of wordpress but I'm following a tutorial that used version 2.7. After changing my header.php and comments.php code, I'm getting an infinite loop when I view the single post page with comments.

Here is the change I made in the <head> tags in header.php:

<?php if(is_singular()){ wp_enqueue_script('comment-reply');} ?>
    <?php wp_head(); ?>

Here is the full comments.php code:

<?php

if(!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME'])){
    die('Please do not load this page directly!');
} 
if(post_password_required()){
?>
    <p>This post is password protected.</p>
<?php
    return;
} ?>

<?php
//Display the comment loop
if(have_posts()) {
?>
    <h2><?php comments_number('Be the first to comment!', 'One Comment', '% Comments'); ?></h2>
    <ol id='comments_section'>
        <?php wp_list_comments(array('avatar_size'=>80, 'reply_text'=>'Reply to this comment.')); ?>
    </ol>
    <div><?php paginate_comments_links(); ?></div>
    <?php 
}else {
    //If no comments so far 

    if('open' == $post->comment_status) {
        ?>
    <p>Have your say!</p>
    <?php
    } else {
        ?>
    <p>Comments currently closed!</p>
        <?php
    }
}

if('open' == $post->comment_status) {
    ?>

    <div id='respond'>
        <h2><?php comment_form_title(); ?></h2>

        <div id='cancel-comment-reply'>
            <small><?php cancel_comment_reply_link(); ?></small>
        </div>

        <?php
        if(get_option('comment_registration') && !$user_ID){
            ?>
            <p>You must be logged in to comment</p>
            <?php
        } else {
            ?>
        <form action="<?php echo get_option('site_url'); ?>/wp-comments-post.php" method="post" id="commentform">

            <?php
            if($user_ID) {
                ?>
            <p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a></p>

            <?php
            } else {
                ?>
                <p>
                    <input type='text' name='author' id='author' value="<?php echo $comment_author;?>" />
                    <label for='author'>Name</label>
                </p>
                <p>
                    <input type='text' name='email' id='email' value="<?php echo $comment_author_email;?>" />
                    <label for='email'>Mail <?php if($req) echo '(Required field)';?></label>
                </p>
                <p>
                    <input type='text' name='url' id='url' value="<?php echo $comment_author_url; ?>" />
                    <label for='url'>Website</label>
                </p>
            <?php
            }
            ?>
            <div>
                <?php comment_id_fields(); ?>
                <input type="hidden" name="redirect_to" value="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'])?>" />
            </div>

            <p><textarea id='comment' name='comment' cols='50' rows='10'></textarea></p>

            <?php
            if(get_option('comment_moderation') == '1'){
                ?>
                <p>Comment moderation is enabled, no need to resubmit any comments posted.</p>
            <?php
            }
            ?>  

            <p><input type='text' name='submit' id='submit' value='Submit' /></p>
            <?php do_action('comment_form', $post->ID); ?>
        </form>
        <?php
        }
        ?>
    </div>
    <?php
}

The loop persists if I remove only the header.php code. Without changing comments.php, it looks like the single post and the comment form code between tags are both infinitely looping. If I remove the comment form code between the tags, the single post is still infinitely looping. The tutorial video I was watching did not show this infinite loop problem, yet I am using the exact code as the tutorial.

UPDATE:

the tutorial I was following is this one

single.php pastebin

share|improve this question
    
Can you test if the loop persists if you change only the comments, and if you change only the header? –  Tom J Nowell Apr 13 '12 at 9:29
2  
Also consider using codex.wordpress.org/Function_Reference/comment_form and please indicate what the infinite loop is printing, is it repeating the comments over and over again or the comments and the post or the post itself? –  Tom J Nowell Apr 13 '12 at 9:30
    
I've edited your question and reformatted your code to be more readable –  Tom J Nowell Apr 13 '12 at 9:46
    
The loop persists if I remove only the header.php code. Without changing comments.php, it looks like the single post and the comment form code between <div id='respond'> tags are both infinitely looping. If I remove the comment form code between the <div id='respond'> tags, the single post is still infinitely looping. The tutorial video I was watching did not show this infinite loop problem, yet I am using the exact code as the tutorial. –  user701510 Apr 13 '12 at 20:24
    
If you remove both the comment AND the header code and return it to how it was before, does it still loop? Also is it possible to see an example? E.g. link or screenshots? –  Tom J Nowell Apr 13 '12 at 21:03

1 Answer 1

up vote 1 down vote accepted

The article you're using is quite old, from ~2009. There are much better cleaner ways of doing commenting.

As a guide, follow this:

http://ottopress.com/2008/wordpress-2-7-comments-enhancements/

This is the definitive guide to implementing Comments post v2.7, and Otto is widely respected in the community.

Firstly, your comments code uses have_posts() to check if there are comments, instead use have_comments(), you're also building the comment form from scratch.

Instead I recommend you use this as your base for comments.php:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-content/themes/twentyten/comments.php

It's a good guide of how to do comments correctly with Modern WordPress. e.g. in that code the comment form is a 1 liner:

<?php comment_form(); ?>

A final word, when you include your footer, use get_footer();, and if you're ever including a PHP template, use get_template_part() instead of the include or require directives

share|improve this answer
1  
thank you, that solved the problem! –  user701510 Apr 13 '12 at 21:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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