有一个带有许多附加字段的自定义评论表单。
<?php
$post_id = get_the_ID();
?>
<div class="company-review-control">
<div class="settings-form custom-checkbox custom-file">
<?php
add_action('comment_form_logged_in_after', 'extend_comment_custom_fields');
add_action('comment_form_after_fields', 'extend_comment_custom_fields');
function extend_comment_custom_fields()
{
echo '<div class="rating-label"><span>Product rating</span><div class="rating">';
for ($i = 5; $i > 0; $i--) {
echo '<input id="' . $i . '_star_product" class="rating-input" type="radio" name="rating_product" value="' . $i . '"/><label for="' . $i . '_star_product" class="rating-star"></label>';
}
echo '</div></div>';
echo '<div class="rating-label"><span>Service & Delivery</span><div class="rating">';
for ($i = 5; $i > 0; $i--) {
echo '<input id="' . $i . '_star_service" class="rating-input" type="radio" name="rating_service" value="' . $i . '"/><label for="' . $i . '_star_service" class="rating-star"></label>';
}
echo '</div></div>';
echo '<input id="rating_full" type="hidden" name="rating" value="0"/>';
echo '<p class="comment-form-title">' .
'<label for="title">' . __('Title of your review', 'am') . '</label>' .
'<input id="comment_title" name="comment_title" type="text" placeholder="If you could say it in one sentence, what would you say?"/>';
echo '</p>';
echo '<label class="label_file label_file_upload"><span>' . __('Add your photo (optional)', 'am') . '</span><input type="file" name="file_upload" accept=".png, .jpg, .jpeg, .webp"></label>';
echo '<label class="label_file label_file_proof"><span>' . __('Proof of purchase', 'am') . '</span><input type="file" name="file_proof" accept=".png, .jpg, .jpeg, .webp"></label>';
echo '<label class="label_checkbox label_checkbox_confirm"><input type="checkbox" name="checkbox_confirm"><span class="checkmark"></span>
<span class="checkbox-txt">Eos tollit ancillae ea, lorem consulatu qui ne, eu eros eirmod scaevola sea. Et nec tantas accusamus salutatus, sit commodo veritus te, erat legere fabulas has ut. Rebum laudem cum ea, ius essent fuisset ut. Viderer petentium cu his.</span></label>';
echo '<input type="hidden" name="comment_nonce" value="' . wp_create_nonce('comment_nonce') . '" />';
}
$comment_args = array(
'comment_notes_before' => '',
'logged_in_as' => '',
'id_form' => 'comment_form_review',
'id_submit' => 'submit',
'class_container' => 'comment-respond',
'class_form' => 'comment_form_review',
'title_reply' => __('Write a review of ', 'am') . get_the_title(),
'title_reply_to' => __('Write a review of %s ', 'am') . get_the_title(),
'title_reply_before' => '<p id="reply-title" class="comment-reply-title h3">',
'title_reply_after' => '</p>',
'comment_notes_after' => '',
'comment_field' => '<p class="comment-form_textarea">
<label for="comment"><span class="comment_title">' . _x('Your review', 'noun') . '</span></label>
<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required="required" placeholder="Write your review to help others learn about this online business"></textarea>
</p>',
'label_submit' => 'Save personal info',
'submit_button' => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',
);
comment_form($comment_args);
?>
</div>
</div>
这些评论的列表带有回复按钮。通过单击特定评论下的某个表单,该表单会以令人遗憾的状态显示,所有字段都具有相同的字段。
问题 - 如何通过单击“回复”在同一页面上显示表单的另一个版本?
<?php
comment_reply_link(array(
'reply_text' => __('Reply', 'am'),
'depth' => $depth,
'max_depth' => $args['max_depth'],
'login_text' => __('Log in to Reply', 'am'),
'before' => '',
));
?>
提交此表单时,我们使用此钩子对其进行过滤:
// 2. Validation
add_filter('pre_comment_on_post', 'custom_verify_comment');
function custom_verify_comment($commentdata)
{
if (empty($_POST['rating_product'])) {
wp_die('Please select a product rating!');
}
if (empty($_POST['rating_service'])) {
wp_die('Please select a service rating!');
}
// Проверка Nonce
if (!isset($_POST['comment_nonce']) || !wp_verify_nonce($_POST['comment_nonce'], 'comment_nonce')) {
wp_die('Nonce verification failed!');
}
return $commentdata;
}
要创建评论回复模板,我实际上必须在弹出窗口中编写自己的模板。然后,通过单击“回复”按钮,使用评论 ID 获取其数据,然后验证它,然后发送它。
这就是表单验证分为不同模板的方式