How to Change the Message Shown Before the Comments Field in WordPress

Customizing the message in the "Leave a Reply" section of a WordPress post


How to Change the Message Shown Before the Comments Field in WordPress

by Christopher Heng, thesitewizard.com

I was asked by a visitor how he could change the message that appears just before the comments field in his WordPress blog.

Assumptions

How to Change the Text that Appears Before the Comments / Reply Field

If you use the one of default themes that come with WordPress, there is typically a comment field at the bottom of each post with some instructions or message that appears before it. For example, the Twenty Sixteen theme has a section with the title "Leave a Reply" and a brief paragraph that says "Your email address will not be published. Required fields are marked *".

While you can of course search the theme files in the "wp-content/themes/" folder, and alter the text directly, your changes will be overwritten the next time the theme updates. And you should not simply prevent the theme from updating, since it may contain crucial security fixes.

The alternative is to create what is known as a "child theme". The latter only contains the changes you want to make to the main theme (called the "parent theme") and nothing else. The parent theme contains the rest of the files needed to make your blog appear the way it does. When WordPress updates itself, it will only update the parent theme, leaving your child theme untouched. This way, you get the best of both worlds: an up-to-date theme, and your changes untouched.

A child theme is fairly easy to make, particularly if all you want is to modify the instructions for the comments field.

  1. Log into your Wordpress blog and find out the name of the theme you are currently using.

  2. Connect to your website with your FTP software, and navigate to the wp-content/themes directory. That is, doubleclick to get into the wp-content folder. Then look for a sub-directory called themes and doubleclick to open it.

  3. Look for the name of your theme in that directory. For example, if you are using the Twenty Twenty theme, the folder is called "twentytwenty".

  4. Create a new folder in that same directory and give it the name of your original theme with "-child" affixed to it. For example, if your current theme is stored in a directory called "twentytwenty", make a new directory with the name "twentytwenty-child".

    You can actually call the directory anything you like. However, the WordPress developers recommend that you stick to the naming convention of calling your child theme by the original name with a "-child" suffix. It will also help you in the future, so that you do not mistake that folder to be one with a self-contained theme, and accidentally delete the parent.

  5. Note down both the name of the parent theme's folder as well as the child theme's folder. You will need both.

    Leave your FTP software running, and connected to your website. You will need it again later.

  6. Open a plain text editor on your computer. For example, if you are currently using Windows, click the Start menu and type "notepad". Click the "Notepad" entry that appears at the top of the menu to run it. Those who are using Mac OS X should do the equivalent to start TextEdit.

  7. Copy the text in the box below and paste it into your editor. I suggest that you do not manually type it, but copy and paste as I said (to avoid typing errors). To copy and paste, just drag your mouse over the text in the box to highlight it, then click the right mouse button to bring up the browser's context menu. Click "Copy" in that menu. Then switch to your editor and paste it the usual way. For Notepad, you can paste it by clicking "Edit" in the menu bar, followed by "Paste" in the drop down menu that appears.

    /*
    Theme Name: Twenty Sixteen Child
    Description: Twenty Sixteen Child Theme
    Template: twentysixteen
    */

    Copy and paste everything above, including the line that says "/*" and the line that says "*/". Don't worry that it currently doesn't reflect your actual theme names. You will change it in the next step.

  8. Now, in your editor, change the line "Template: twentysixteen" to refer to your parent theme's directory. For example, if your parent theme is in a directory called "twentytwenty", change it to "Template: twentytwenty".

    Remember that this is the directory name. It is not the human-readable name that you see in your WordPress interface. The name must be exactly the folder name you saw earlier.

  9. Change the "Theme Name" line to refer to the name of your theme, but with the word "Child" appended. For example, if your theme is called "Twenty Twenty", change the line to say "Theme Name: Twenty Twenty Child". This name will appear in your WordPress user interface, so don't write anything that will confuse you later. For example, don't be too clever and call it by some fancy new name, or years down the line, you may forget which parent theme this one is linked to. Keep the name of the original theme and add "Child" to it.

  10. The "Description" line is optional, and you can change it to whatever you want. You can even delete it if you wish.

  11. Save the file with the name "style.css". Note that you must use this name, and no other, or WordPress won't recognize it. If you are using Notepad, click "File" on the menu bar, followed by "Save As..." in the menu that appears. Then type "style.css", including the quotation marks. You must include the quotation marks if you use Notepad, otherwise the editor will change your filename to "style.css.txt" behind your back. And if you did not configure Windows to show you the file extensions, you will not be able to see that your file has now the wrong name.

  12. Close your text editor. That is, exit the program.

  13. In your FTP software, change to the child theme's folder. For example, if the folder is called "twentytwenty-child", doubleclick to open it in your FTP program.

  14. Upload the "style.css" file that you have just created to that folder.

    Again, leave the FTP program open for now. You will need it again after the next few steps.

  15. Open your plain text editor again. You should see a blank page. If you did not close it earlier as I suggested, and it still contains the "style.css" content, please close it first, so that you do not accidentally modify the wrong file. We are starting a new blank document here.

  16. Copy and paste the following lines into your editor. Do not re-type it or you may make mistakes. (For example, your browser may have folded my long lines into multiple lines, so if you manually type it, you may incorrectly break lines.)

    <?php
    function tsw_change_comments_field_message ( $comment_form_settings )
    {
      $comment_form_settings['comment_notes_before'] =
        '<p class="comment-notes"><span id="email-notes">Your email address will not be published. Required fields are marked <span class="required">*</span></p>' ;
      return $comment_form_settings ;
    }
    function tsw_enqueue_styles ( )
    {
      wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    }
    add_action( 'wp_enqueue_scripts', 'tsw_enqueue_styles' );
    add_filter( 'comment_form_defaults', 'tsw_change_comments_field_message' );
    ?>

    In addition, don't worry that it doesn't say what you want it to say. For now, just copy and paste it.

  17. The text that will be shown under the "Leave a Reply" heading in the Twenty Sixteen theme (which was the theme I used for the above example, since it was what my visitor had) is given in this line:

    '<p class="comment-notes"><span id="email-notes">Your email address will not be published. Required fields are marked <span class="required">*</span></p>' ;

    This is actually the default text used by that theme. Modify it so that it says what you want it to say. If you are not familiar with the PHP programming language, notice that the text to be changed is contained within single quotation marks. Unless you know what you're doing (eg, you know how to write PHP programs), only modify the things contained within the quotation marks.

    If you want single quotation marks (such as apostrophes) to appear in your instructions, precede each one with a backslash (ie, the "\" character), otherwise the PHP interpreter won't know whether you are terminating the quote or inserting an apostrophe. If you are not sure what I mean in my previous sentence, don't use any apostrophes or single quotation marks in your modification (eg, rephrase your sentence so that apostrophes are avoided). If you're afraid you will break something, stick to changing these words only: "Your email address will not be published. Required fields are marked <span class="required">*</span>"

  18. When you are done, save the file as "functions.php". Again, if you are using Notepad, make sure you include the quotation marks around the name, otherwise it will silently save it as "functions.php.txt" instead. Note that you should not use any other name. It must be literally "functions.php", small (ie, lowercase) letters and all, or WordPress will not recognize it.

  19. Upload this file to the child theme's directory (where you previously transferred the "style.css" file). There should now be two files in that folder: "functions.php" and "style.css".

  20. Disconnect from your site in your FTP program and exit it. Quit your text editor as well. The remainder steps have to be done within WordPress itself.

  21. Log into your WordPress blog. Go to the Themes section, that is, the part where you normally choose a theme. You can usually do this by selecting "Appearance" in the left column, followed by "Themes" in the submenu that appears.

  22. Look for the child theme that you created. There won't be a pretty picture for it (because you didn't create one for it earlier), but you can find it by looking for its name. Click the "Activate" button.

  23. If you have customized your original theme, for example, to add menus, widgets, headers, etc, you will need to make those changes again. This is because WordPress treats the child theme as a new theme, separate from what you used earlier. Don't worry, your new theme still basically looks like the old one, since it inherits the default appearance the parent had (minus customizations to the menu, widgets, headers, etc), and your child theme didn't change anything except the words appearing before the comments/reply field.

  24. Once you are done with the changes, log out of WordPress. You have to log out, otherwise you can't test your modifications. When logged in, you will only see the words that a logged-in user sees in the comments area, which is different from what a visitor who is not logged in sees.

  25. Now go to one of the post pages where a comment field is shown, to see if you have correctly altered the text.

For Advanced Users who Know PHP

This section is only for those who understand PHP and know how to modify scripts written in that language.

Other parts of the comments area can be modified too. For example, if you want to change the "Leave a Reply" header so that it now reads (say) "Leave a Message", you will need to add the following line to the tsw_change_comments_field_message() function, just before the line that says return $comment_form_settings ;.

$comment_form_settings['title_reply'] = 'Leave a Message' ;

You can find the full list of things that can be altered in comment form function reference in the WordPress Codex.

Copyright © 2020 Christopher Heng. All rights reserved.
Get more free tips and articles like this, on web design, promotion, revenue and scripting, from https://www.thesitewizard.com/.

thesitewizard™ News Feed (RSS Site Feed)  Subscribe to thesitewizard.com newsfeed

Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.

Please Do Not Reprint This Article

This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.

Related Articles

New Articles

How to Link to This Page

It will appear on your page as:

How to Change the Message Shown Before the Comments Field in WordPress





Home
Donate
Contact
Link to Us
No Spam Policy
Privacy Policy
Topics
Site Map

Getting Started
Web Design
Search Engines
Revenue Making
Domains
Web Hosting
Blogging
JavaScripts
PHP
Perl / CGI
HTML
CSS
.htaccess / Apache
Newsletters
General
Seasonal
Reviews
FAQs
Wizards

 

 
Free webmasters and programmers resources, scripts and tutorials
 
HowtoHaven.com: Free How-To Guides
 
Site Design Tips at thesitewizard.com
Find this site useful?
Please link to us.