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:
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")); ?>
- 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.
- reply text reply_text - defining what will be written on the reply link to the comment, usually "Reply"
- authorization text login_text - displayed if the answer requires authorization on the site, usually "Login to comment"
- callback is the name of a custom function for displaying comments.
- 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
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:
- Do not use page comments
- Use meta tags to create canonical urls for each post
- 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:
- We make backup copies of files, which, in theory, should already become a habit
- Enable nested comments in settings Options→Discussion Settings→Allow Tree Comments
- Add the following code to header.php just before wp_head()
- Add to comments.php just before the closing tag
- We check that
- Verify that the comment form is enclosed in with attribute id="respond"
- Add a comment cancel link right before the comment form
- In comments.php we replace the old cycle with the updated one
Well, actually everything 🙂 Traditionally, there are a trillion different ways to customize and redo it to your needs, but I think you get the point. Just don't forget to set the styles for nested comments
Commentlist li.depth-1() .commentlist li.depth-2() .commentlist li.depth-3()
Sharing comments, pingbacks and trackbacks
When displaying responses to your post, an effective way to reduce clutter is to display comments and pings separately. Not only will this not interrupt the flow of discussions, but it will also allow you to focus more attention on the comments.
This is one of the oldest tricks and there are many ways to implement it, here's an example of a simple split by using the wp_list_comments() options:
Comments
Pingbacks/Trackbacks
True, this code has a drawback - the titles will be displayed even if there are no comments or pings. It would be better to use a slightly improved version:
Comments
Trackbacks/Pingbacks
During the display of pingbacks, a link is displayed with a piece of text around it, taken from the referring site. This snippet can be of various sizes and is difficult to control, because it is not known in advance what will be displayed there. Fortunately, it is possible to clean up the display of this text, displaying only a neat list of links. To do this, add to functions.php
Function cleanPings($comment, $args, $depth) ( $GLOBALS["comment"] = $comment; echo "
- ".comment_author_link()."
";}And then add the required callback parameter to the comment function call
Destroying pingbacks and trackbacks
You can't prevent people from linking to your site, but you can choose not to display trackbacks and pingbacks to them. Initially, as well as much else on the Internet, pingbacks were invented for good purposes - informing that someone referred to you. And now someone else continues to use and display them. But the trackback technology has completely discredited itself due to massive spam, so it would be better to turn them off.
Disable display of pings from the admin panel
To do this, in the Discussion settings, uncheck the option Allow notifications from other blogs. This completely disables the functionality of pingbacks and trackbacks, however, for a single selected entry, you can enable this feature in the settings.
Exclude pings from comments
Use a parameter for the wp_list_comments() function, allowing only the output of comments
Delete wp-trackback.php
This is a very simple and effective way - physical deletion of the wp-trackback.php file from the WordPress root directory. This is an absolutely safe method, but the file is restored after the WordPress upgrade and will need to be deleted again.
Disable with plugins
This can be done with a plugin
The plugin itself is very simple and consists of only one file, so its contents can be simply copied and pasted into the functions.php file
Class DisableTrackbacks ( function DisableTrackbacks() (add_action("pings_open", array(&$this, "pings_open"));) function pings_open($open) (return ("1" == get_query_var("tb")) ? FALSE : $open;) ) add_action("plugins_loaded", create_function("", "global $DisableTrackbacks; $DisableTrackbacks = new DisableTrackbacks();"));
Once placed, the DisableTrackbacks function destroys all trackbacks. And the pingbacks remain.
Control comments directly in the database
WordPress offers several ways to control whether a post is open for commenting or not. In the admin panel, you can completely prohibit leaving comments on future entries on the entire site. Also, in a single post in the editor, you can choose in the Discussions block whether comments will be open to it or not.
But right above these methods is a plugin that allows you to hide all old comments after a specified period, sending them for moderation. And without any plug-ins, you can enable/disable commenting with simple queries to the database.
Allow pings globally for all users
UPDATE wp_posts SET ping_status = "open";
Globally disable pings for all users
UPDATE wp_posts SET ping_status = "closed";
Globally disable pings before a specific post date
UPDATE wp_posts SET ping_status = "closed" WHERE post_date< "2009-09-09" AND post_status = "publish";
Such requests allow periodic cleaning. And here is an example of requests for a global permission or prohibition of commenting
UPDATE wp_posts SET comment_status = "open", ping_status = "open" WHERE comment_status = "closed" AND post_status = "publish";
The prehistory of writing this article is typical: I decided to separate my comments with a separate style ... Fortunately, I have been shredding style.php for more than a day and I know the basic rules of CSS syntax. And, of course, this innocent idea turned into a small embarrassment, the search for a solution ... In the final, everything was successful. And now I want to tell you what magical passes I used to decorate my own comments.
The meaning of all the manipulations that are described below is to highlight your comments and answers to readers. If you do not overdo it with styles, it will turn out quite elegant. Of course, this is not a vital necessity. Anyone who decides to view your comments will be able to do this by nickname. But the idea of beauty is also important to many bloggers.
In addition, if you pay attention to how the author's answers are designed on many large and thoughtful 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 see the comment styles that are already in use
Do not be surprised that I suggest, as it were, to start from the end. But for users who are not quite comfortable with editing WordPress themes, this is the most obvious and obvious way.
So, select a post on your blog that has comments. Of course, your answers need 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 opportunity).
Don't be alarmed when you see a sheet of code. In fact, there is nothing complicated and secret here. Scroll down the page and notice that the general information about the post is displayed first, then its text with formatting. Below, finally, will be the code related to the comments. You can just type Ctrl+F , in the window that appears, enter commentlist and press Enter - you will immediately be “transferred” to the desired line, where the search word will be brightly highlighted.
Now let's see how the styles of our comments are marked up. For clarity, the code is from my theme. Of course, it may look and be structured a little differently for you, but the main meaning of the individual parts of the code should be clear:
COMMENT TEXT
Actually, we only need to find comments with your nickname (for example, on this blog I write and answer under the nickname Katyaru). And just above the desired line, which begins like this:
Thank you, you have a great site!