How the comments.php file in WordPress

How the comments.php file in WordPress

As is known to all people starting a blog with WordPress it is harder to create your own theme from scratch, however, many manage to manage and edit existing items. One of the most difficult to encode files and even edit the comments.php, with this tutorial I will explain the basics that should take your comments.php file so you need not create problems.

If you are not very skilled in coding themes for WordPress and you get better editing them, we will work with a framework called "Starkers" we had used before but now with its Spanish version translated by Jack Bach on its website TINNTA .

Download the version in Spanish Starkers "StarskersES"

Understanding the File Comments

Before doing anything with this file, we must first understand that it works and where to call.

The comments meet among others the following functions.

  1. Call the comments made in an entry
  2. Call the "Trackbacks" of an entry
  3. Displays the comment posting form

Understanding that works, we can understand from where to call. It is recommended to do so from the single.php file somewhere at the end of the publication of the post, eg:

  1
 2
 3
 4
 5
 6
 7
 = "entrada" ><!-- Comienza entrada --> < h1 > <? php the_title ( ) ; ?> </ h1 > < div class = "imagen-entrada" >< a href = "#" >< img src = "imagen.jpg" class = "imagen" /></ a ></ div > <?php the_content ( '' ) ; ?> <?php edit_post_link ( 'Editar Entrada' , '' , '.' ) ; ?> <?php comments_template ( ) ; ?> <!-- Inserta la pagina comments . php --> </ div ><!-- Termina entrada --> <Div class = "entry" ><!-- Start input -> <h1> <? Php the_title ();?> </ H1> <div class = "image-input"> <a href = "#" > <img src = "picture.jpg" class = "image" /> </ a> </ div> <? php the_content ('');?> <? php edit_post_link ('Edit Entry','', ' . ');?> <? php comments_template ();?> <! - Insert the comments page. php -> </ div ><!-- End Entry -> 

That would be a section of the file single.php and see what we call the file comments on line 6 just before closing div of the inputs (after publishing the content).

We call the file comments with the following line:

  1
  ; ?> <? Php comments_template ();?> 

Starting to edit the comments.php

Imagine the possible scenarios of your post.

1 - post a comment but expected moderation
2 - the comments are open and no one has published a
3 - Are the comments closed
4 - Are the comments open but you need to be logged in to post a
5 - Are the comments open and the user has logged into your meeting.

WordPress creators have given all these possibilities in the publication of comments on your posts, here we are to understand how to work with them.

We started first of all with a few lines as you said in the comments php, should not be changed unless you know what you do. In Starkers lines are:

  1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 if ( ! empty ( $_SERVER [ 'SCRIPT_FILENAME' ] ) && 'comments.php' == basename ( $_SERVER [ 'SCRIPT_FILENAME' ] ) ) die ( 'Por favor, no abras esta página directamente. ¡Gracias!' ) ; if ( ! empty ( $post -> post_password ) ) { // Si hay una contraseña if ( $_COOKIE [ 'wp-postpass_' . COOKIEHASH ] != $post -> post_password ) { // Y no coincide con la cookie ?> < p > Esta entrada está protegida con contraseña . Introduce la contraseña para ver los comentarios .</ p > <?php return ; } } /* Esta variable es para alternar el fondo de los comentarios */ $oddcomment = 'class="alt" ' ; ?> <!-- Puedes empezar a modificar aquí . --> <? Php / / Do not delete these lines if (! Empty ($ _SERVER ['SCRIPT_FILENAME']) & & 'comments.php' == basename ($ _SERVER ['SCRIPT_FILENAME'])) die ('Please do not open this page directly. Thanks! ') if (! empty ($ post -> post_password)) (/ / If the password if ($ _COOKIE [' wp-postpass_ '. COOKIEHASH]! = $ post -> post_password) ( / / Y does not match the cookie?> <p> This post is password protected. Enter the password to view comments. </ p> <? php return;)) / * This variable is to switch the bottom of the comments * / $ oddcomment = 'class = "alt"';?> <! - You may change here. -> 

As it says not modify it, here we skyandstars case and do not touch.

Starkers In what follows is a PHP tag conditional "If", do not touch, left as is like this. Then follows with a third-order title (h3) that indicates the number of comments made in your post.

  1
 2
 ( $comments ) : ?> < h3 id = "comments" > <? php comments_number ( 'Sin Respuestas' , 'Una Respuesta' , '% Respuestas' ) ; ?> a & #8220;<?php the_title(); ?> & #8221;</h3> <? Php if ($ comments):?> <H3 id = "comments"> <? Php comments_number ('No Responses', 'One Response', '% Responses');?> A & # 8220; <? Php the_title ();?> & # 8221; </ h3> 

* Here the only thing I recommend you modify the degree of the title (h1, h2 or h3).

We fall and we find an ordered list (ol), which does nothing but display a remark made by a visitor of your website, the sorted list is as follows:

  1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
	 <?php foreach ( $comments as $comment ) : ?> < li <?php echo $oddcomment ; ?> id = "comment-<?php comment_ID() ?>" > <?php echo get_avatar ( $comment , 32 ) ; ?> < cite > <? php comment_author_link ( ) ?> </ cite > Dice :: <?php if ( $comment -> comment_approved == '0' ) : ?> < p > Tu comentario está pendiente de moderación .</ p > <?php endif ; ?> < p >< a href = "#comment-<?php comment_ID() ?>" title = "" > <? php comment_date ( 'F jS, Y' ) ?> a las <?php comment_time ( ) ?> </ a > <?php edit_comment_link ( 'editar' , '&nbsp;&nbsp;' , '' ) ; ?> </ p > <?php comment_text ( ) ?> </ li > <?php /* Cambia cualquier otro comentario (trakbacks) a una clase distinta */ $oddcomment = ( empty ( $oddcomment ) ) ? 'class="alt" ' : '' ; ?> <?php endforeach ; /* fin de los comentarios */ ?> </ ol > <Ol> <? Php foreach ($ comments as $ comment):?> <Li <? Php echo $ oddcomment;?> Id = "comment-<? Php comment_ID ()?>"> <? Get_avatar php echo ($ comment, 32);?> <cite> <? php comment_author_link ()?> </ cite> Says: <? php if ($ comment -> comment_approved == '0 ')?> <p> Your comment is awaiting moderation. </ p> <? php endif;?> <p> <a href = "# comment-<? php comment_ID ()?>" title = ""> <? php comment_date ('F JS, Y ')?> at <? php comment_time ()?> </ a> <? php edit_comment_link (' edit ',' ','');?> </ p> <? php comment_text () ?> </ li> <? php / * Changes every other comment (trakbacks) to a different class * / $ oddcomment = (empty ($ oddcomment))? 'class = "alt"':'';?> <? endforeach php / * end of comment * /?> </ ol> 

Let us understand this section of code line by line:

Line 1 (ol): Open an HTML element that will unite all the comments made in your post, ie after the publication of the comments, they will look like this:

  1
 2
 3
 4
 5
  <ol>
 <li> a comment from a user named x with your avatar and other <li>
 <li> Another comment from another user named with your avatar and other </ li>
 <li> Another comment from a user named Carlos with your avatar and other </ li>
 </ Ol> 

Understanding this, we can make some modifications to this code and do it this way:

  1
 2
 3
 4
 <div id="comentarios"> <div class="comentario-individual"> a comment from a user named x with your avatar and other </ div> <div class="comentario-individual"> Another comment from another user named and your avatar and other </ div> </ div> 

Line 3: This section of PHP code it does is open a single comment, something like the LOOP that we used in the index which publishes the entries but in this case used for posting a comment.

Has the following:

  1
 2
 3
  ( $comments as $comment ) : ?> <? Php foreach ($ comments as $ comment):?>
 Here would be the overall appearance of your individual comments
	 ; /* fin de los comentarios */ ?> <? Endforeach php / * End of comment * /?> 

* These lines do not make any changes

Line 5 (li): Open an HTML element to an individual entry, in this case an ordered list item previously opened, with the modifications made in line 1, here we can do the following:

New line 5 and 13 respectively:

  1
 2
  = "comentario-individual" <?php echo $oddcomment ; ?> id = "comment-<?php comment_ID() ?>" > <Div class = "comment-individual" <? Php echo $ oddcomment;?> Id = "comment-<? Php comment_ID ()?>>
		 </ Div> 

Line 6: Call the user's avatar to public comment, in this case will be of a size of 32px x 32px (assuming equal values for the width and height.

If we make some changes to the appearance of the avatars of users, such as adding a class and that they are 80px x 80px, we can do this by:

  1
  = "avatar" > <? php echo get_avatar ( $comment , 80 ) ; ?> </ div > <Div class = "avatar"> <? Get_avatar php echo ($ comment, 80);?> </ Div> 

7 Line: This line it does is call the link of the person posting the comment.
Lines 8, 9 and 10: These lines open one of the first situations discussed at the beginning of this section (but is expected to post a Comment moderation). Only modifies the message you want to give your users, such as the following:

  1
 2
 3
  ( $comment -> comment_approved == '0' ) : ?> <? Php if ($ comment -> comment_approved == '0 ')?>
			 En este blog se necesita que un administrador acepte tu comentario , por lo tanto , el mismo espera moderación .</ p > <P> In this blog you need an administrator to approve your comment, therefore, the same expected moderation. </ P>
			 ; ?> <? Php endif;?> 

Line 11: This line shows data for the publication of the comments: ID, date, time and a link to edit them.
Line 12: This line displays the contents as such, ie, the message was published.
Lines 15, 16, 17 and 18: What do these lines is to alternate the style shown with your comments (you can see an example here where it alternates skyandstars funds the comments). My recommendation if you're new to making themes for WordPress is that DO NOT use this function.

Now it is the most complicated of this file, it is because we find many IF, ELSE, ENDIF.

We went in and found Starkers following:

  1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
  : // esto se muestra si aún no hay comenarios ?> <?php if ( 'open' == $post -> comment_status ) : ?> <!-- Si los comentarios están abiertos , pero no hay comentarios . --> <?php else : // los comentarios están cerrados ?> <!-- Si los comentarios están cerrados --> < p > Los comentarios están cerrados .</ p > <?php endif ; ?> <?php endif ; ?> <? Php else: / / this is displayed if there is still no comment will?> <? Php if ('open' == $ post -> comment_status):?> <! - If comments are open, but no comments . -> <? php else: / / comments are closed?> <! - If comments are closed -> <p> The comments are closed. </ p> <? php endif;?> <? php endif;?> 

Lines 1, 2, 3 and 4: These lines change the second situation described in this section (the comments are open and no one has published one) and in this case nothing is displayed if no comments but you can add some text on these lines For example:

New lines 1, 2, 3 and 4:

  1
 2
 3
 4
 5
  : // esto se muestra si aún no hay comenarios ?> <?php if ( 'open' == $post -> comment_status ) : ?> < p > Por ahora no hay comentarios en la entrada , pero tú puedes ser el primero </ p > <!-- Si los comentarios están abiertos , pero no hay comentarios . --> <? Php else: / / this is displayed if there is still no comment will?> <? Php if ('open' == $ post -> comment_status):?> <P> By now there are no comments at the entrance, but you can be the first </ p> <! - If comments are open, but no comments. -> 


Lines 6, 7 and 8: They raise the third situation (comments are closed), you can modify the text displayed in this section:

  1
 2
 3
	  : // los comentarios están cerrados ?> <!-- Si los comentarios están cerrados --> < p > Disculpa pero en esta entrada están los comentarios cerrados .</ p > <? Php else: / / comments are closed?> <! - If comments are closed -> <p> Sorry but the comments this entry are closed. </ P> 

Line 10: The closure of the "status" of the commentary (if there is none and if they are closed).
Line 11: A general closure of the second situation referred.

My advice is to not change the general structure of these codes PHP, just modify the text to appear as they did here.

We went more in the Starkers and find the following line:

  1
  ( 'open' == $post -> comment_status ) : ?> <? Php if ('open' == $ post -> comment_status):?> 

What that means is that the comments are open and proceed to the opening of the form of publication of comments.

Then find the following:

  1
 2
 3
 ( get_option ( 'comment_registration' ) && ! $user_ID ) : ?> < p > Tu debes estar < a href = "<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>" > conectado </ a > para escribir un comentario .</ p > <?php else : ?> <? Php if (get_option ('comment_registration') & &! $ User_id)?> <P> You must be <a href = "<? Php echo get_option ('siteurl');?> / Wp-login.php? redirect_to = <? php echo urlencode (get_permalink ());?> "> online </ a> to post a comment. </ p> <? php else:?> 

The above lines you do is open the fourth situation related (comments are open but need to be registered to post one), no need to make any changes to these lines.

Now below is open comment posting form with the following line:

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

Do not modify

Then we have the following code block:

  1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 ( $user_ID ) : ?> < p > Conectado como < a href = "<?php echo get_option('siteurl'); ?>/wp-admin/profile.php" > <? php echo $user_identity ; ?> </ a >. < a href = "<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title = "Salir de esta cuenta" > Deconectarme & raquo ;</ a ></ p > <?php else : ?> < input type = "text" name = "author" id = "author" value = "<?php echo $comment_author ; ?>" size = "22" tabindex = "1" <?php if ( $req ) echo "aria-required='true'" ; ?> /> < label for = "author" > Nombre <?php if ( $req ) echo "(necesario)" ; ?> </ label > < input type = "text" name = "email" id = "email" value = "<?php echo $comment_author_email ; ?>" size = "22" tabindex = "2" <?php if ( $req ) echo "aria-required='true'" ; ?> /> < label for = "email" > Email ( no se publicará ) <?php if ( $req ) echo "(necesario)" ; ?> </ label > < input type = "text" name = "url" id = "url" value = "<?php echo $comment_author_url ; ?>" size = "22" tabindex = "3" /> < label for = "url" > Página web </ label > <?php endif ; ?> <? Php if ($ user_id)?> <P> Logged in as <a href = "<? Php echo get_option ('siteurl');?> / Wp-admin/profile.php"> <? Php echo $ user_identity ;?> </ a>. <a href = "<? php echo get_option ('siteurl');?> / wp-login.php? action = logout" title = "Out of this account"> Deconectarme & raquo; </ a> </ p> <? php else:?> <input type = "text" name = "author" id = "author" value = "<? php echo $ comment_author;?>" size = "22" tabindex = "1" <? php if ($ req) echo "aria-required = 'true'";?> /> <label for = "author"> Name <? php if ($ req) echo "(required) "?> </ label> <input type =" text "name =" email "id =" email "value =" <? php echo $ comment_author_email;?> "size =" 22 "tabindex =" 2 "<? php if ($ req) echo "aria-required = 'true'";?> /> <label for = "email"> Email (not published) <? php if ($ req) echo "(required)"; ?> </ label> <input type = "text" name = "url" id = "url" value = "<? php echo $ comment_author_url;?>" size = "22" tabindex = "3" /> <label for = "url"> Website </ label> <? php endif;?> 

Do not change the structure of the form fields!

Lines 1, 2, 3 and 4: These lines open the situation before number 5 (the comments are open and the user has logged into your meeting), what it does is show the user name with a link to your profile and link to leave the meeting.

The rest of the lines they do is show the form fields that users are not in your user session.

Close the form of posting comments with the following lines:

  1
 2
 3
 4
 5
 6
 7
 8
 9
 strong > XHTML :</ strong > Puedes utilizar estas etiquetas : < code > <? php echo allowed_tags ( ) ; ?> </ code ></ p > < textarea name = "comment" id = "comment" cols = "100%" rows = "10" tabindex = "4" ></ textarea > < input name = "submit" type = "submit" id = "submit" tabindex = "5" value = "Enviar Comentario" /> < input type = "hidden" name = "comment_post_ID" value = "<?php echo $id ; ?>" /> <?php do_action ( 'comment_form' , $post -> ID ) ; ?> </ form > <P> <strong> XHTML: </ strong> You can use these tags: <code> <? Php echo allowed_tags ();?> </ Code> </ p> <textarea name = "comment" id = "comment" cols = "100%" rows = "10" tabindex = "4"> </ textarea> <input name = "submit" type = "submit" id = "submit" tabindex = "5" value = "Submit Comment" / > <input type = "hidden" name = "comment_post_ID" value = "<? php echo $ id;?>" /> <? php do_action ('comment_form', $ post -> ID);?> </ form> 

What these lines is to show the form fields that will be common to all users (those who logged many as those who do not).

The closure of the file comments is as follows:

  1
 2
 3
  ; // Si es necesario registro y no está conectado ?> <? Php endif / / If necessary registration and not online?>

 ; // Si borras esto el cielo caerá sobre tu cabeza ?> <? Php endif / / If you delete this the sky will fall on your head?> 

I think the comments PHP these lines speak for themselves.

The important thing summarizing all this mess a bit of code is NOT modify the overall structure in PHP, I mean to eliminate the IF, ELSE, ENDIF limit yourself to only add HTML tags (divs, h1, h2, etc) where you think necessary . If you have any further questions on the comments.php file you can leave it here or on the forum and try to help.

Michael is the creator and editor of skyandstars.net which publishes guides and tutorials for WordPress, CuteNews, PHP, HTML and more. Are a current college student.

Mijael166

Maybe one of these entries may interest you

25 Comments on "How the comments.php file in WordPress"

  1. stage72

    October 27, 2009

    it is difficult if I did but still I do not believe that she would make jajaja bye and thank you for your time

  2. marten

    October 27, 2009

    hello.
    contact you feel this way. I am writing on behalf of an advertising company based in Berlin. You could get in touch with the head of advertising for your blog?

    thank you very much
    Marta P.

  3. Michael

    October 27, 2009

    Response to Comment stage72 : Glad you got yours: P

    Response to Comment by marta : Hey marta, I am the owner and manager of this website .. you can leave an email to webmaster@skyandstars.net or add us to the same MSN.Saludos.

  4. Jack Bach

    October 29, 2009

    Michael Ei!

    Thanks for the reference! I'm glad that was useful to you Starkers.

    See you soon:)

  5. Mijael166

    October 29, 2009

    Hey Jack the truth is that if Spanish is a useful tool, it saves work to translate all the tema.Gracias.

  6. nefer

    December 2, 2009

    I finally managed to fit the wordpress web, still not 100% but either is fine, I'm so happy XD

  7. Mijael166

    December 2, 2009

    Response to Comment nefer : I'm glad Nefer: P soon as you finish, as I told you on there .. Once you make the first the rest will come easy.

  8. nefer

    December 3, 2009

    yes yes is super easy, too complicated but I will jajaja qeu now I change all the sites to wp xd

  9. ShairNash

    January 6, 2010

    I have a super problems when I put an entry with password does not appear to me the comments after entering the password but if you see the login T_T

  10. Mijael166

    January 6, 2010

    Let's see if I understand the problem. The entrance is protected by password and can not see anything, but when you write the password input is displayed but not the comments, is that what happens?

  11. ShairNash

    January 6, 2010

    if! that happens to me more q and code reviewed as I find the problem

  12. ShairNash

    January 6, 2010

    and fix it! oO and no other entry or as I like and that if you served the other does not delete it so q apologize for any inconvenience n_n

    • Mijael166

      January 6, 2010

      Dale:) who knows that's what happened ... Saludos!

      • pablo

        July 29, 2010

        're drunk?

        • Mijael166

          July 29, 2010

          Hi Paul, or Peter, please I beg you to make conscious use of the comments, remember they are only for questions or suggestions, Thanks!

  13. Gasi_isea

    March 27, 2010

    Michael Friend ...

    Luckily i got your blog, I have a Problemón! my blog in the settings section - discussion of wordpress DISAPPEARED COMMAND LINE THAT INDICATES THAT IF YOU ARE NOT REGISTERED NO COMMENT ... I have not gotten a solution to that ...! to complete the file comments.php reempelace the THEMES section and raizel comment.php the blog and can not see That line of command to disable! nose and check where else ... HELP!

    • Mijael166

      March 27, 2010

      Hello! Replied to your message you sent me from the contact section. What I suggest is to download wordpress and upload again all the folders and files to your server again, all but wp-content if you do that again you have that option in your WordPress installation

      • pedro

        July 28, 2010

        I want to comment as you, as you did, please leave your item downloads!

        • Mijael166

          July 28, 2010

          Sorry, this item is not available for download, I can do is a little tutorial on how to place comments in response to your blog with WordPress

  14. Nico

    April 29, 2010

    hello, two queries, build a blog, and use the item has this characteristic: each entry in the top left is a rectangle with the date of entry and exit down the other with a number that is the link to comment entry. you can change that number and put "comments" or easier (I guess) to make each entry is the feedback form provided below (for more comments have not yet)

  15. Mijael166

    April 29, 2010

    If you delete the file ... single.php of your theme and add the linking index file comments.php file in the loop where you want to leave the comments section.

  16. ShairNash

    May 15, 2010

    is possible to make the author's name has a link to the profile of the same if you use any plugin to customize profiles?
    giving me hope to understand xD

  17. Michael

    May 16, 2010

    Mmm have to use some plugin for user profiles. If you do it without the plugin and link to the author will leave the entries posted.

  18. ShairNash

    May 16, 2010

    q q profiler makes use will see user profiles but qiero q to give the name of the user click the send to that profile but have not succeeded yet T_T

  19. MaxorChuck

    May 17, 2010

    Thanks for your tutorial helps a lot on how to understand the comments.php thanks

Leave a comment

Name (Required)

Email (required but not published)

Web

Comment (Required)

* Your E-mail is used to relate it to your account in Gravatar