Is there a way to check that comment was successfully submitted? I want to display some text or hide a comment form for example if comment was succesfully submitted.
Is there a way to check that comment was successfully submitted? I want to display some text or hide a comment form for example if comment was succesfully submitted.
You got this action comment_post which fires just after comment is inserted in database
The following example uses the comment_post hook to run a function immediately after a comment is posted. The function checks whether the comment is approved and, if so, executes the code specified.
function show_message_function( $comment_ID, $comment_approved ) {
if( 1 === $comment_approved ){
//function logic goes here
}}add_action( 'comment_post', 'show_message_function', 10, 2 );
Note that the add_action line includes the priority and the number of parameters (, 10, 2). If we leave the number of parameters out, we will only be able to access to the first parameter ($comment_ID) in our function. We will not be able to access the second parameter ($comment_approved).
For More reference please check the comment_post hook link.
Wordpress adds hashtag to URL if comment was successfully submitted. The easiest way to hide comment form or display some info is to check if hash exists with Javascript.
hash = window.location.hash;
if(hash){
$('#commentform').hide();
}
I have tried this in the following way, you can try this ....
put the following code in functions.php
function hide_comment_form_function( $comment_ID, $comment_approved ){
$commentData = get_comment( $comment_ID );
$postTitle = get_the_title($commentData->comment_post_ID);
$url = get_site_url() ."/" .$postTitle . "/?status=cmt_post";
header("Location: $url");
exit();}add_action( 'comment_post', 'hide_comment_form_function', 10, 2 );
And the following code in header.php
if(isset($_GET['status']) && ($_GET['status'] == "cmt_post")){
?>
<style>
#commentform, #reply-title
{
display: none;
}
</style>
<?php}
This will hide the comment form after submit the comment.