代码之家  ›  专栏  ›  技术社区  ›  ArtisticPhoenix

从评论表单获取对评论的回复

  •  -1
  • ArtisticPhoenix  · 技术社区  · 8 年前

    在构建自定义评论表单时,我需要获取正在回复的评论的ID(如果正在回复)。

    因此,回复将与原始评论在同一市场。例如,我需要该字段的值与作为回复的原始注释的值相同。

    市场是一个自定义字段,这将是一个地理区域,但实际上只是一个文本字段。我还计划将其设为只读,前提是我可以从“家长”的评论中获得信息。

    一些Wordpress插件代码(简化)

    class clComments{
    
        public function init(){
            //display the custom fields in comment form
            add_filter('comment_form_fields', [$this,'clCustomCommentFields'] );
            //Save the custom fields
            add_action( 'comment_post', [$this,'clSaveCommentMetaData'] );
        }
    
        public function clCustomCommentFields($fields){
            /*-----------------------------------
              When replying to a comment I need to get that comment's
              id, so I can pull out the custom metadata for the comment
              that it is replying to
            -----------------------------------*/
    
            //$repledToId = ?; //this should be the id of the comment this is replying to
    
            $market = get_comment_meta( $repledToId, 'market', true );
    
            $fields['market'] = $this->clCustomCommentFieldMarket($market);
            return $fields;
        }
    
        /**
         * I have bunch of custom fields so I don't like repeating myself
         */
        public function clCustomCommentNormalizeValue( $value ){
            if( false === $value){
                return '';
            }
            return 'value="'.esc_attr($value).'" ';
        }
    
        public function clCustomCommentFieldMarket($default = false){
             return '<p class="comment-form-market">'.
             '<label for="cf_market" style="display:block;" ><span class="required">*</span> '. __( 'What market(s) are you are interested in' ).'?</label>'.
             '<input id="cf_market" name="market" type="text" aria-required="true" required="required" '.$this->clCustomCommentNormalizeValue($default).'/></p>';
        }
    
        /**
         * save custom meta data
         */
        public function clSaveCommentMetaData($comment_id){
            if(!empty($_POST['market'])){
                add_comment_meta( $comment_id, 'market', wp_filter_nohtml_kses($_POST['market']) );
            }
        }
    
     }
    

    希望这是有意义的,我只需要这个评论回复的ID。但这必须在构建评论表单时进行 filter:comment_form_fields

    如果您需要更多信息,请随时询问。如果你要投反对票,至少尊重我,告诉我为什么。

    更新

    下面是评论表单的屏幕截图。我需要的是图片底部的评论,这对于“市场”有一个“Any”值,所以我需要在输入时输入“reply”模式。需要明确的是,这是在点击该评论上的“回复”按钮之后。

    enter image description here

    谢谢

    1 回复  |  直到 8 年前
        1
  •  3
  •   mega6382    8 年前

    您只需为$\u get数组获取此信息,例如

    $_GET['replytocom'];
    

    所以在你的课堂上

    public function clCustomCommentFields($fields){
        $market = get_comment_meta( $$_GET['replytocom'], 'market', true );
    
        $fields['market'] = $this->clCustomCommentFieldMarket($market);
        return $fields;
    }