If you’re developing a WordPress theme, one of the most important page elements you’ll need to implement is the comments layout. As it so happens, it’s also one of the most frustrating components to develop. Getting the style right is difficult, and messing it up is incredibly easy.
The comments section of a website is a great place for readers to engage with one another and allows website owners to increase user interaction. With this in mind, we’ve put together this simple yet powerful code snippet generator that helps developers add a responsive comments section to their WordPress themes.
How Does the Comments Template and the Custom Callback Work?
By default, WordPress displays comments in a theme based on the code and configuration settings of the comments.php file in the theme’s directory.
To start things off, the comment template tag is wrapped in a conditional statement (using the have_comments() function) to determine whether there are any comments to loop over or not. If true, the template proceeds to display the total number of comments for the current post using the comments_number() function.
Next, the template specifies arguments, filters, and callbacks for the wp_list_comments() function. This function is used to display the entire comments list.
Finally, we have a conditional statement (combining the comments_open(), get_comments_number(), post_type_supports() functions) which determines whether or not comments are open for the post or not (i.e. does the post support comments). If true, the message Comments are closed. will be displayed on the screen.
The callback argument for the wp_list_comments() function is used to reference the function that will build the beginning of an individual comment. This means that the callback will make your custom function get called to display each comment bypassing WordPress’ internal functionality.
How Do I Use This WordPress Comments With CSS Snippet Generator Tool?
This comments with CSS code snippet generator is designed specifically to help you integrate a well-designed comments section into your WordPress theme. Since I’m using Bootstrap, I’ve pulled a few image styles over from bootstrap.css. Additionally, the generated code ensures that the comments section’s styling will default to the user’s theme’s font style.
To use the tool, all you have to do is enter your theme’s details and let the tool generate a theme-specific code snippet for you to use on your WordPress theme.
- Theme Slug: The theme’s slug should be in all lowercase letters, numbers, and/or underscores. It’s used to prefix any functions or variables that could potentially cause incompatibility issues with the WordPress core, themes, or plugins. Typically, the theme slug is the exact same string as the text domain with any underscores replaced by hyphens.
- Text Domain: The text domain is a unique key that will be used to identify translatable strings in your theme or plugin. It should be the same string as your theme’s name in all lowercase letters, numbers, and/or hyphens. In most cases, the text domain matches the theme’s directory name in your site’s root directory.
- Once you’ve entered both of the required fields, head over to the Code tab. You’ll see that the Functions.php, Comments.php, and Style.css areas are populated with custom code snippets for adding a pre-designed comments section to your WordPress site.
What Will the Comments Sections Look Like?
Here’s a preview of how the comments section will look like once you’ve added the code snippet to your WordPress theme:
And it looks great on mobile, too!
How Do I Add the Code Snippets to My Website?
The quickest way to add the generated code snippets to your site is by pasting them directly into your current theme’s functions.php, comments.php, and style.css files. You can edit them directly by navigating to Appearance > Editor from the WordPress admin panel.
Alternatively, you can download the theme’s functions.php, comments.php, and style.css files to your desktop via an FTP client. Next, open them up one by one in a text editor and paste the corresponding code snippets at the bottom of each of the files. Hit the Save button and re-upload all of the files to your theme’s root directory.
The look and feel of your theme’s comments section can potentially influence user engagement. Styling your comments section with a little bit of CSS can significantly improve its visual appeal and invite users to contribute comments to your blog. Hopefully, having this pre-configured coding base will save you some time and effort from having to code an elegant-looking comments section for your theme.
Do you have any questions about the Comments With CSS code snippet generator tool? Let us know in the comments section below!