admin管理员组文章数量:1130349
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.
4 Answers
Reset to default 0You 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.
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.
4 Answers
Reset to default 0You 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.
本文标签: publishCheck if comment was successfully submited
版权声明:本文标题:publish - Check if comment was successfully submited 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749037745a2306654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论