Boundless comment php. Formatting the comment area in WordPress

Comments in PHP are similar to comments used in HTML. In PHP syntax, comments always begin with a special sequence of characters, and any 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 can view your site's source code. PHP comments differ 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 only useful for PHP programmers.

In case you forgot or didn't know how comments are made in HTML, see the example below.

php comment syntax: single line comment

Whereas html has only one type of comment, PHP has two types. The first type we will discuss is the single line comment. That is, a comment that tells the interpreter to ignore whatever happens on that 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!

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

php comment syntax: multiline comment

Like HTML comments, multiline comments in PHP can be used to comment on 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 crawl into the code or not to crawl? We've said before that the comment form is a bit complicated, so the creators have made it as easy as possible.

The function for calling comments 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 lot of php code. Because Since this function generates all ready-made html code, we will have to deal with it without the ability to somehow adjust or customize it using parameters. This can be a certain problem, given that we can't really change this html code. Don't like div classes? Don't want to use gravatars? Would you like to change the order of output lines?

You will have to put up with all this and hide unnecessary things with 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 comment functionality
  • built-in pagination functionality
  • built-in tree view functionality
  • css default classes for basic tasks

custom comment function

  • absolute control over everything
  • standard and custom css classes
  • the presence of plugins to add the desired functionality

The choice of method for generating comments depends on many factors. In general, we recommend using wp_list_comments and not fooling around. If you need to do something of your own, special and specific, with the desired functionality and capabilities, and this desire is backed up by knowledge, then using custom loops will make it all possible.

In addition, there is a good old method for displaying comments using the foreach loop, so we still have something to deal with.

wp_list_comments() or custom loop?

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

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

Although wp_list_comments doesn't look like a loop, it does the exact same thing, repeating requests and outputting comments over and over again until they run out, tagging them with the appropriate tags.

Usually, the finished html code of one comment looks something like this:

  1. Announcer's 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 comment text itself. If it suits you, then it’s good, you don’t need to do anything else.

In this case, you can only stretch your hands to the following available wp_list_comments options:

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

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

    Shamanim in functions.php

    To begin with, we 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 do not close the tag
  • , because WordPress will automatically generate closing tags depending on the markup of nested comments.

    Once placed, this function will be called when generating comments and allows you to make any necessary changes to yourself. The trick is that we have done everything we need, using the standard and rather limited settings wp_list_comments()

    And now let's shake the old days - together with wp_list_comments we will put a foreach loop

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

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

    Implementing 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 skim through and rewind. In general, we are certainly not afraid of vertical scrolling (as opposed to horizontal), but everything should have borders. A typical post with 60 comments can be up to 20k pixels tall!

    A great way to clean up the mess is to paginate the comments feed and display them page by page.

    After enabling pagination, don't forget to add functions to display links "Earlier" "Later" in the comments.php file

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

    And there is one catch here. There is a well-known SEO problem of duplicating content with page comments. This is due to the fact that 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 comments will be different on these pages. What worsens the overall picture even more is that each comment has its own unique link with the time of publication, which 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. Do not 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 about to become so, then it is of course 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 drastic approach by removing the post content from the comment pages and replacing it with a quote or a stripped down version of the post.

    Implementing nested comments

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

    Before the advent of such an opportunity, there was a style of responding to the selected author, using his name and the @ sign in the comment text, such as "@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 all you have to do is click on the appropriate “Reply” link and have your say. After the text of the comment is approved, it will appear immediately below the comment it was written to.

    If your template does not support this kind of commenting, there is an easy way to add this feature:

    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 just before wp_head()
    4. Add to comments.php just before the closing tag
    5. We check that