1


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.

0

You got this action comment_post which fires just after comment is inserted in database

3
  • Any ideas how to use it to receive for example comment status on front-end? Just after redirection to the same page. – anton Jul 14 '16 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) – JMau Jul 14 '16 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 ? – anton Jul 14 '16 at 14:17
0

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.

1
  • 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 ? – anton Jul 14 '16 at 14:18
0

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();
}
0

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.

2
  • 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 – anton Jul 16 '16 at 11:08
  • Yes, we can do this by using hashtag. – Arpita Hunka Jul 16 '16 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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