L
L
Leonid2018-07-25 10:19:35
WordPress
Leonid, 2018-07-25 10:19:35

How to show a post on the site after adding a comment in WordPress?

How to display a message like this to the user after adding a comment in WordPress :

Thanks, your comment has been added! After moderation, it will appear on the site!

I already tried like this:
add_action( 'comment_post', function ( $comment_ID, $comment_approved ) {
  add_action( 'comment_form_before', function() {
    echo '<h1>Спасибо, ваш комментарий добавлен! После модерации он появится на сайте!</h1>'; 
  });
}, 10, 2 );

but no, that's not it...
Maybe there is already something ready? Although there is no single mechanism for displaying alerts in WordPress in the Front-End, that's why it's probably just something to invent and implement on your own...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid, 2018-07-25
@easycode

So far I had to stupidly do this:

add_action( 'comment_form_before', function() {
  ?>
  <script type="text/javascript">
  jQuery(document).ready(function($) {
    re = /comment-\d+/i;
    url = window.location.href;
    var hash = url.substring(url.indexOf("#")+1);
    if (hash.match(re)) {
      jQuery([document.documentElement, document.body]).animate({
            scrollTop: $("#before_comment_form_box").offset().top - 50
        }, 2000);
    }
  });
  </script>
  <div id="before_comment_form_box">
    Спасибо! Ваш комментарий будет опубликован после проверки модератором.
  </div>
  <?php
});

Because after adding a comment, an anchor of the form is added to the address: #comment-XXX

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question