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.
4 Answers
You got this action comment_post which fires just after comment is inserted in database
-
Any ideas how to use it to receive for example comment status on front-end? Just after redirection to the same page.– antonCommented Jul 14, 2016 at 13:05
-
@anton you got an example on page documentation with the
$comment_approved
var that gives you the information you're looking for (status)– JMauCommented Jul 14, 2016 at 13:13 -
Thanks @Jmau for idea. But I need to hide comment form after comment successfully submitted. I don't have any idea how I can send information about comment status back to the post page. Do you have any idea how to do this with comment_post hook ?– antonCommented Jul 14, 2016 at 14:17
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.
-
I need to hide comment form after comment successfully submitted. I don't have any idea how I can send information about comment status back to the post page. Do you have any idea how to do this with comment_post hook ?– antonCommented Jul 14, 2016 at 14:18
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.
-
I noticed, that wordpress adds hashtag with comment id after successful comment submiting. I think it's much easier to check if this hashtag exist with javascript, but thank you for your answer– antonCommented Jul 16, 2016 at 11:08
-