Formatting the Comments Area in WordPress. Formatting the comment area in WordPress Disabling the display of pings from the admin panel

The background to writing this article is typical: I decided to highlight my comments with a separate style... Fortunately, I have been shredding style.php for several days now and I know the basic rules of CSS syntax. And, of course, this innocent idea turned into a slight embarrassment, a search for a solution... In the end, everything worked out. And now I want to tell you what magical passes I used to decorate my own comments.

The point of all the manipulations described below is to highlight your comments and responses to readers. If you don’t go overboard with the styles, it will turn out quite elegant. Of course, this is not a vital necessity. Anyone who decided to view your comments will be able to do so by nickname. But the idea of ​​beauty is also important to many bloggers.

In addition, if we pay attention to how the author’s answers are formatted on many large and well-thought-out sites (for example, smashingmagazine), we will see a separate style for them. Obviously this will help improve usability.

It is especially important to use a separate style for author comments on blogs where tree-like comments with a fairly deep degree of nesting are allowed. Simply put, where the user can reply to comments, reply to replies, and so on.

Let's look at the comment styles that are already in use

Don’t be surprised that I suggest, as it were, starting from the end. But for users who do not feel completely comfortable editing WordPress themes, this is the most visual and obvious way.

So, select a post on your blog that has comments. Of course, we need your answers to be here as well. Having opened such a page, right-click on it and select “View page code” (in Google Chrome and Firefox browsers there is definitely such an option).

Don't be scared when you see a sheet of code. In fact, there is nothing complicated or secret here. Scroll down the page and you will notice that the general information about the post is displayed first, then the text with formatting. Below is finally the code related to comments. You can simply type Ctrl+F and enter in the window that appears commentlist and press Enter - you will immediately be “thrown” to the desired line, where the searched word will be brightly highlighted.

Now let's look at how our comment styles are marked. For clarity, the code is from my theme. Of course, yours may look and be structured a little differently, but the main meaning of individual parts of the code should be clear:

  • COMMENTATOR'S NICK:

    COMMENT TEXT

    • YOUR NICKNAME:

      TEXT OF YOUR REPLY

      HERE IS THE DATA FOR THE "Reply" BUTTON
    • Actually, we only need to find comments with your nickname (for example, on this blog I write and respond under the nickname Katyaru). And just above the required line, which begins like this:

    • These are the class designations − comment byuser comment-author-admin bypostauthor odd alt depth-2 — we are interested in. What do they mean? — comment"comment style" byuser"registered user style" comment-author-admin"author-admin style" bypostauthor"post author's style" odd“even comment style” (or even “odd”) alt"response comment style" depth-2“comment nesting depth is 2″

      Attention: if you wisely changed your blog login from “admin” to something else, then the name of the style will look like this comment-author-YOUR LOGIN. It is the login to enter the admin area, and not the nickname you use to sign up!!!

      There are possible options here. Perhaps commenters on your blog should be registered users. Perhaps the author of the post is not you, but another registered user without admin rights. But there is only one admin on the blog - and it’s you! Therefore, you should pay attention to comment-author-admin(or comment-author-YOUR LOGIN) - it must be unique for all your comments.

      In general, the algorithm of actions is as follows:

      • We look at the comment code and find out how (by the presence of what style) your comments differ from the comments of all other users (just readers, registered users, authors, etc.)
      • Then we find this style in the style sheet and add it

      Adding a special style for blog admin comments

      Open the page with style sheets in the admin panel of the blog style.php - it’s better to first make a backup of the theme or, at least, copy all the code from this page and save it as a separate file in Notepad. Then you will be able to restore the styles without the errors we are preparing to make

      We find a description of the comment style using the search word commentlist. Let me remind you that the code looks great in different themes, but the meaning is the same everywhere. For example, among other things, I have the following lines:

      THERE CAN BE A LOT OF DIFFERENT THINGS HERE ol.commentlist li.alt() ol.commentlist li.bypostauthor p() ol.commentlist li.byuser () ol.commentlist li.comment-author-admin(ENTER YOUR STYLE) ol.commentlist li. comment() ol.commentlist li div.comment-author-admin (ENTER YOUR STYLE) THERE CAN BE A LOT OF DIFFERENT DIFFERENT OL.commentlist li ul.children li.bypostauthor() ol.commentlist li ul.children li.byuser() ol. commentlist li ul.children li.comment() ol.commentlist li ul.children li.comment-author-admin(ENTER YOUR STYLE) ol.commentlist li ul.children li.depth-2() ol.commentlist li ul.children li.depth-2 li.comment-author-admin(ENTER YOUR STYLE) ol.commentlist li ul.children li.depth-3() ol.commentlist li ul.children li.depth-3 li.comment-author-admin (ENTER YOUR STYLE) AND SO ON

      These empty curly braces () indicate that no special style has been specified. It is this void that we must fill with our code.

      Attention: first you need change in the code the word “admin” for your login!!! In all lines where the name of this style is used!!!

      Possible styles for admin comments

      We have already agreed that we will not make too much of the admin comments. You just need to emphasize them slightly. For example, I made a thick green stripe to the left of my answers.

      Comments in PHP are similar to the comments used in HTML. In PHP syntax, comments always begin with a special sequence of characters and all text that appears between these special characters will be ignored by the interpreter.

      In HTML, the main purpose of a comment is to serve as a note to developers who may view your site's source code. PHP comments are different in that they will not be displayed to visitors. The only way to view PHP comments is to open the file for editing. This makes PHP comments useful only to PHP programmers.

      In case you forgot or did not know how comments are made in HTML, then see the example below.

      PHP comment syntax: single line comment

      While html has only one type of comment, PHP has two types. The first type we will discuss is the one-line comment. That is, a comment that tells the interpreter to ignore everything that happens on this line to the right of the comments. To use this comment, use the characters "//" or "#" and all text to the right will be ignored by the PHP interpreter.

      Psst...You can"t see my PHP comments!"; // echo "nothing"; // echo "My name is Humperdinkle!"; # echo "I don"t do anything either"; ?>

      Result:

      Hello World! Psst...You can't see my PHP comments!

      Notice that several of our echo commands were not processed because we commented them out using special comment characters. This type of comment is often used to quickly write down complex and confusing code, or to temporarily remove a line of PHP code (for debugging).

      PHP comment syntax: multiline comment

      Like HTML comments, multiline comments in PHP can be used to comment large blocks of code or to write comments on multiple lines. Multiline comments in PHP start with "/*" and end with "*/". Anything between these characters will be ignored.

      Result.

      To get into the code or not to get into it? We've already said before that the comment form is a little complicated, and that's why the creators have simplified everything as much as possible.

      The comment call function is simple and has no parameters

      Looks simple, but does a lot. In fact, it creates a huge amount of html code and uses a bunch of php code. Because This function generates all the ready-made HTML code, then we will have to deal with it without the ability to somehow adjust or customize using parameters. This may be a problem, given that we cannot really change this html code. Don't like div classes? Don't want to use gravatars? Do you want to change the order in which the lines are displayed?

      You will have to come to terms with all this and hide unnecessary things using css. From this point of view, you will need to decide which preference path you will take:

      wp_list_comments

      • easy to use
      • built-in commenting functionality
      • built-in pagination functionality
      • built-in tree display functionality
      • default css classes for basic tasks

      custom comment function

      • absolute complete control over everything
      • standard and custom css classes
      • availability of plugins to add the necessary functionality

      The choice of comment generation method depends on many factors. In general, we recommend using wp_list_comments and not fooling yourself. If you need to do something of your own, special and specific, with the desired functionality and capabilities, and this desire is supported by knowledge, then the use of custom cycles will make it possible to implement it all.

      In addition, there is the good old method of displaying comments using a foreach loop, so we still have a lot to figure out.

      wp_list_comments() or custom loop?

      Prior to version 2.7, the comment output loop was based on database queries and was displayed in a somewhat tortuous but well-understood path.

      Then the comments were enriched with additional functionality - page navigation and tree nesting. These features were activated through the admin panel, and the foreach loop was replaced with the already shown wp_list_comments function. However, this cycle still remains operational, although for obvious reasons it does not support additional functionality.

      Although wp_list_comments does not look like a loop, it does everything the same, repeating queries and outputting comments over and over again until they end, marking them with the appropriate tags.

      Usually the finished HTML code for one comment looks something like this:

      1. Commentator name says:

        Thank you, you have a great site!

      As you can see, the comment output loop contains a gravatar image, several divs, many different CSS classes, a link to the author and the actual comment text. If this suits you, then that’s good, you don’t need to do anything else.

      In this case, you can only reach out to the following wp_list_comments options:

      • default avatar size is 32 pixels
      • css styles - using their names you can change the parameters to the desired ones
      • style for displaying the comment list style. By default, comments are displayed as unordered list tags
          but if you want to use an ordered list instead
            , then use this parameter "ol")); ?>
          1. type type is a string parameter responsible for the displayed comments. Available parameters are all, comment, trackback, pingback, pings. Pings means that both trackbacks and pingbacks will be output together. By default, the parameter is all, i.e. all comments are displayed.
          2. reply text reply_text - determines what will be written on the reply link to the comment, usually “Reply”
          3. authorization text login_text - displayed if you need to log in to the site to respond, usually “Log in to comment”
          4. callback is the name of a custom function for displaying comments.
          5. Additional options available

        This is not much, but in most cases it is enough for the minimum necessary modifications. If you need more, you will have to use a custom loop or interfere with the PHP code written in comments.php, or add the necessary functionality to functions.php.

        Shamanism in functions.php

        First, let's indicate in comments.php that we will use our custom function

        Now let's create our custom function in functions.php

      • id="li-comment-">
        ");?> %s says:"), get_comment_author_link()); ?>
        comment_approved == "0") : ?>
        $depth,"max_depth" => $args["max_depth"]))); ?>
        Please note that we are not closing the tag
      • , because WordPress will automatically create closing tags based on the markup of nested comments.

        Once posted, this function will be called when comments are generated and allows you to make any necessary edits. The trick is that we did everything we needed using the standard and fairly limited settings wp_list_comments()

        Now let’s go back to the old ways - together with wp_list_comments we’ll put a foreach loop

        This use will allow the template to be backwards compatible with older versions of WordPress.

        Now that we know how to set up a comment loop, let's modify it to suit our needs and apply some cool tricks.

        Implementing page-by-page comments

        No one knows in advance that the content on the site can attract hundreds of comments. What I'm getting at is that we are all superstars and success can come unexpectedly. A post with hundreds of comments can be insanely long to watch and rewind. In general, we are certainly not afraid of vertical scrolling (unlike horizontal scrolling), but everything should have borders. A typical post with 60 comments can be up to 20k pixels tall!

        A great way to keep things organized is to paginate your comment feed and display them page by page.

        Once you enable pagination, don't forget to add functions to your comments.php file to display "Before" and "Later" links.

        These links will automatically appear and break the sheet of comments when the specified number of comments per page is exceeded. If there are fewer comments than this number, the links will not be displayed.

        And there is one catch here. The well-known SEO problem of duplicating content with page-by-page comments arises. This happens because the same URLs are created for each comment page.

        • https://yoursite.com/ghost-dad-rocked/
        • https://yoursite.com/ghost-dad-rocked/comments-page-1/
        • https://yoursite.com/ghost-dad-rocked/comments-page-2/
        • https://yoursite.com/ghost-dad-rocked/comments-page-3/

        It is clear that only the comments on these pages will be different. What makes the overall picture even worse is that each comment has its own unique link with the time of publication, which also adds a bunch of duplicates. Therefore, the search bot will find the same content under a huge number of different links.

        To avoid duplication, there are several solutions:

        1. Don't use page comments
        2. Use meta tags to create canonical URLs for each post
        3. Use plugin

        If your site is super popular or is going to become so, then of course it is better to use canonical URLs. If heated discussions are not planned, then it is easier to use traditional comment sheets without pagination.

        SEO for Paged Comments takes an even more radical approach - removing the content of the post from the comment pages and replacing it with a quote or a shortened version of the post.

        Implementing nested comments

        Nested tree comments allow visitors to write a response to a selected existing comment, creating a rich discussion dialogue. After activating this feature in the settings, a “Reply” link will appear next to each comment, allowing you to speak on a specific comment.

        Before this possibility appeared, there was a style of responding to the selected author, using his name and the @ sign in the comment text, like “@Vasya you’re wrong!” With some transformation, this method has taken root in many applications on the Internet and still lives, for example, on Twitter. And in modern versions of WordPress such a mention does not need to be used.

        Now you just need to click on the appropriate “Reply” link and speak out. Once the comment text is approved, it will appear immediately below the comment it was written about.

        If your template doesn't support this type of commenting, there is an easy way to add it:

        1. We make backup copies of files, which in theory should already become a habit
        2. Enable nested comments in settings Options→Discussion Settings→Allow Tree Comments
        3. Add the following code to header.php immediately before wp_head()
        4. Add to comments.php just before the closing tag
        5. Let's check that