0

My comment form has validation so that when a user tries to post a blank comment, it currently shows the "This field is required" message under the comment box. How do I change the message content to "Please enter your comment."? I'm guessing that the following line of code might do the trick but I don't know how to nest HTML code in my functions.php file.

document.getElementById("comment-error").innerHTML = "Please enter your comment.";

Any help would be most appreciated.

1 Answer 1

0

if your above Javascript is working fine and you want to insert that into a page.

Kindly add the following code to your functions.php. I have tested this code it's working fine.

function handle_comment_error() {
?>
    <script>
    document.getElementById("comment-error").innerHTML = "Please enter your comment.";
    </script>
<?php
 }add_action('wp_head', 'handle_comment_error');

Let me know if you face any issues.

2
  • I tried this and unfortunately, it doesn't work for me . Could it be because the comment form (where the validation error message displays) is in the body of the page and your code tries to add an action to wp_head (the header)?
    – Scarlett
    Commented Jul 9, 2021 at 0:43
  • no, it's not like that, the wp_head insert script on the top of the page, and that works for all elements doesn't matter that element is on the header/body or somewhere. Can you please share the link with me to your project? Or you can check the trigger and can run the above code at that particular time. Commented Jul 9, 2021 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.