0

I've done a function that shows to the logged in user all her comments and gives her the possibility to delete them singularly through a delete button related to each comment printed. The function works, but after the form action is executed, on refresh the page still shows the deleted comment, and only if you go again to the page (not refreshing, but by clicking the return key on the address bar of the browser) the page shows only the approved comments. How can I fix this problem?

Here's the code:

            function custom_delete_post_comment() {
                   $comment_id = comment_ID();
                   wp_delete_comment( $comment_id, true ) ;

                         if ( isset( $_POST['comment_delete_nonce'] ) ) {
                            if( wp_verify_nonce( $_POST['comment_delete_nonce'], 'comment_delete_nonce' ) ) {
                            set_query_var( 'commentid1', $_POST['commentid'] );
                            wp_delete_comment( get_query_var( 'commentid1'), true );
                            }
                        }
            }


            function users_frontend(){

            global $wpdb;
            global $current_user;

            if(is_user_logged_in()){

            $result = $wpdb->get_results("SELECT * FROM wp_comments WHERE user_id = $current_user->ID");

                $content = '<div class="welcome"> Ciao, '. $current_user->first_name .'</div>';

                    $content .= '<h2>Questi sono gli eventi a cui ti sei iscritto. Per cancellare la singola iscrizione, clicca sulla "x" in alto a destra su ogni post</h2>';
                $content .= '<div class="list-comment">';
                foreach($result as $row){
                        $content .= '<div class="comment-recap">';
                        $content .= get_the_post_thumbnail( $row->comment_post_ID );
                        $content .= '<form method="POST" action="'. custom_delete_post_comment() .'" class="delete-comment-form">
                        <input type="hidden" name="comment_delete_nonce" value="'. wp_create_nonce('comment_delete_nonce') .'" />
                        <input type="hidden" name="commentid" value="'. $row->comment_ID .'" />
                        <input type="submit" value="x" title="x" id="submit-btn" class="btn" />
                        </form>
                        <span><a href="'. get_permalink( $row->comment_post_ID ) .'">'. get_the_title( $row->comment_post_ID ) .'</a></span>
                        </div>';
                        $content .= wp_get_comment_status( $row->comment_ID );
                }
                    $content .= '</div>';

                }

            return $content;
            }

            echo users_frontend();

             ?>
1
  • "only if you go again to the page (not refreshing, but by clicking the return key on the address bar of the browser)". This sounds like the page is in your browser's cache. What if you perform a hard refresh instead?
    – Iceable
    Feb 10, 2018 at 14:04

1 Answer 1

0

Just from the top of my head (untested), would it be an idea to use clean_post_cache after the comment is deleted ?

2
  • How do I do that? Feb 10, 2018 at 15:16
  • Add clean_post_cache( $post_id_where_comment_belongs_to ); on the line after wp_delete_comment.
    – Beee
    Feb 11, 2018 at 18:43

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.