获得评论表单的评论

时间:2017-12-19 23:05:45

标签: php wordpress

在构建自定义注释表单时,我需要获取正在回复的注释的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”,因此我需要在“回复”模式下将其放入输入中。为了清楚起见,这是在点击该评论的“回复”按钮之后。

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:3)

您只需获取$ _GET数组的此信息,例如

$_GET['replytocom'];

所以在你的课堂上

public function clCustomCommentFields($fields){
    $market = get_comment_meta( $$_GET['replytocom'], 'market', true );

    $fields['market'] = $this->clCustomCommentFieldMarket($market);
    return $fields;
}
相关问题