問題描述

使用 wordpress 函式 comment_form 函式有沒有辦法用 html 元素包裝提交按鈕。

$comments_args = array(
            'id_form'               => 'main-contact-form',
            'class_form'            => 'contact-form',
            'class_submit'          => 'btn btn-primary btn-lg',
            'label_submit'          => 'Add Comment',
            'fields'                =>  $fields,
            'title_reply_before'    => '<div class="message_heading"><h4>',
            'title_reply_after'    => '</h4><p>Make sure you enter the(*)required information where indicate.HTML code is not allowed</p></div>',
            'comment_field' => '
                <div class="row"><div class="col-sm-7">                        
                    <div class="form-group">
                        <label>Message *</label>
                        <textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
                    </div> 
                </div>'

        );

        comment_form($comments_args);

我要輸出程式碼提交按鈕就像:

 <div class="form-group">
     <input type="submit" class="btn btn-primary btn-lg>
 </div>

最佳解決方案

我們可以使用 comment_form 函式的 submit_button 引數來更改提交按鈕的 HTML 。

submit_button 的預設 HTML 為

<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />

你可以改變你的程式碼。

$comments_args = array(
   ....

   'submit_button' => '<div class="form-group">
            <input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />
        </div>'

   ....
);

更新:

關於%1$s%2$s 等。

如果您看看 comment_form()原始碼,可以看到 submit_button 正在透過這樣的 sprintf 。

$submit_button = sprintf(
  $args['submit_button'],
    esc_attr( $args['name_submit'] ),
    esc_attr( $args['id_submit'] ),
    esc_attr( $args['class_submit'] ),
    esc_attr( $args['label_submit'] )
);

次佳解決方案

請將以下程式碼放在主題的 functions.php 檔案中,並將 div 中的提交按鈕包裝起來:

// define the comment_form_submit_button callback
function filter_comment_form_submit_button( $submit_button, $args ) {
    // make filter magic happen here...
    $submit_before = '<div class="form-group">';
    $submit_after = '</div>';
    return $submit_before . $submit_button . $submit_after;
};

// add the filter
add_filter( 'comment_form_submit_button', 'filter_comment_form_submit_button', 10, 2 );

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。