Delete comments function

时间:2018-02-10 作者:Luigi Briganti

我做了一个功能,向登录用户显示她的所有评论,并允许她通过与打印的每个评论相关的删除按钮单独删除这些评论。该功能起作用,但在执行表单操作后,刷新时页面仍会显示已删除的注释,并且仅当您再次转到该页面(不是刷新,而是通过单击浏览器地址栏上的返回键)时,页面才会显示已批准的注释。如何解决此问题?

代码如下:

            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 个回复
SO网友:Beee

从我的头脑中(未经测试),使用clean_post_cache 删除评论后?

结束

相关推荐

类似wpdb的请求显示所有数据库数据

我想得到一个user\\u nicenames和ID的列表。但不是我现在得到的所有用户名。我知道我应该在$名称前后使用%但似乎没有任何效果。这是迄今为止我找到的获得一些产出的唯一方法;global $wpdb; //get access to the WordPress database object variable //get names of all users $name = $wpdb->esc_like(stripslashes($_POST[\'name\']