我已经构建了一个自定义注释表单,该表单与默认值挂钩comment_form()
在《十二个孩子》主题中。这是我的密码functions.php
function pietergoosen_custom_comment_fields($fields) {
$commenter = wp_get_current_commenter();
$req = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$fields = array(
\'author\' =>
\'<p class="comment-form-author"><label for="author">\' . __( \'Name\', \'pietergoosen\' ) .
( $req ? \'<span class="required">*</span>\' : \'\' ) . \'</label> \' .
\'<input id="author" name="author" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) .
\'" size="30"\' . $aria_req . \' /></p>\',
\'email\' =>
\'<p class="comment-form-email"><label for="email">\' . __( \'email\', \'pietergoosen\' ) .
( $req ? \'<span class="required">*</span>\' : \'\' ) . \'</label> \' .
\'<input id="email" name="email" type="text" value="\' . esc_attr( $commenter[\'comment_author_email\'] ) .
\'" size="30"\' . $aria_req . \' /></p>\',
\'verwysing\' =>
\'<p class="comment-form-refer"><label for="refer">\' . __( \'Where did you hear about us\', \'pietergoosen\' ) . \'</label>\' .
\'<input id="verwysing" name="refer" type="text" value="\' . esc_attr( $commenter[\'comment_author_refer\'] ) .
\'" size="30" /></p>\',
);
return $fields;
}
add_filter(\'comment_form_default_fields\',\'pietergoosen_custom_comment_fields\');
这就是我将jquery脚本放入队列的方式
functions.php
以及jquey验证
comment-validation.js
. 它工作正常,不会转到wordpress使用的默认错误页。
function pietergoosen_comments_validation() {
if(is_single() && comments_open() ) {
wp_enqueue_script(\'jqueryvalidate\', get_stylesheet_directory_uri().\'/js/jquery.validate.pack.js\', array(\'jquery\'));
wp_enqueue_script(\'commentvalidation\', get_stylesheet_directory_uri().\'/js/comment-validation.js\', array(\'jquery\',\'jqueryvalidate\'));
}
}
add_action(\'wp_enqueue_scripts\', \'pietergoosen_comments_validation\');
jQuery(document).ready(function($) {
$(\'#commentform\').validate({
rules: {
author: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
comment: {
required: true,
minlength: 10
}
},
messages: {
author: "Please provide a valid name",
email: "Please provide a valid email",
comment: "Comments needs to be at least 20 characters"
},
errorElement: "div",
errorPlacement: function(error, element) {
element.before(error);
}
});
});
问题是,我修改了代码以包括表单的其他自定义项,如下所示:
function pietergoosen_custom_comment_fields($args = array(), $post_id = null) {
if ( null === $post_id )
$post_id = get_the_ID();
else
$id = $post_id;
$commenter = wp_get_current_commenter();
$user = wp_get_current_user();
$user_identity = $user->exists() ? $user->display_name : \'\';
$req = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$html5 = isset( $args[\'format\'] ) && \'html5\' === $args[\'format\'];
$fields = array(
\'author\' =>
\'<p class="comment-form-author"><label for="author">\' . __( \'Name\', \'pietergoosen\' ) .
( $req ? \'<span class="required">*</span>\' : \'\' ) . \'</label> \' .
\'<input id="author" name="author" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) .
\'" size="30"\' . $aria_req . \' /></p>\',
\'email\' =>
\'<p class="comment-form-email"><label for="email">\' . __( \'email\', \'pietergoosen\' ) .
( $req ? \'<span class="required">*</span>\' : \'\' ) . \'</label> \' .
\'<input id="email" name="email" type="text" value="\' . esc_attr( $commenter[\'comment_author_email\'] ) .
\'" size="30"\' . $aria_req . \' /></p>\',
\'verwysing\' =>
\'<p class="comment-form-refer"><label for="refer">\' . __( \'Where did you hear about us\', \'pietergoosen\' ) . \'</label>\' .
\'<input id="refer" name="refer" type="text" value="\' . esc_attr( $commenter[\'comment_author_refer\'] ) .
\'" size="30" /></p>\',
);
$required_text = sprintf( \' \' . __(\'Fields marked %s are required and must be filled in\'), \'<span class="required">*</span>\' );
$arg = array(
\'fields\' => apply_filters( \'comment_form_default_fields\', $fields ),
\'comment_field\' => \'<p class="comment-form-comment"><label for="comment">\' . _x( \'Share your thoughts on this post\', \'noun\' ) . \'</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>\',
\'must_log_in\' => \'<p class="must-log-in">\' . sprintf( __( \'Please <a href="%s">sign in</a> to share your thoughts\' ), wp_login_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
\'logged_in_as\' => \'<p class="logged-in-as">\' . sprintf( __( \'You are currently signed in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Sign out">Sign out?</a>\' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
\'comment_notes_before\' => \'<p class="comment-notes">\' . __( \'Be assured, your email will never be shared with anyone\' ) . ( $req ? $required_text : \'\' ) . \'</p>\',
\'comment_notes_after\' => \'<p class="form-allowed-tags">\' . sprintf( __( \'Thank you for sharing your thoughts.\' )) . \'</p>\',
\'title_reply\' => __( \'Share your thoughts\' ),
\'title_reply_to\' => __( \'Share your thoughts on %s\' ),
\'cancel_reply_link\' => __( \'Remove your thoughts\' ),
\'label_submit\' => __( \'Send\' ),
);
return $arg;
}
add_filter(\'comment_form_defaults\', \'pietergoosen_custom_comment_fields\');
我的评论表单中的所有内容都正常工作,但现在我的验证脚本不起作用了。如果验证失败,它将重定向到默认的wordpress错误页面。我的自定义验证脚本无法验证页面上提交的信息。
是我使用了错误的钩子,还是验证脚本中遗漏了什么。
最合适的回答,由SO网友:Pieter Goosen 整理而成
好的,我已经解决了。您需要从wp core复制“comment\\u form()”的完整第一部分。这是代码
<?php
function comment_form( $args = array(), $post_id = null ) {
if ( null === $post_id )
$post_id = get_the_ID();
else
$id = $post_id;
$commenter = wp_get_current_commenter();
$user = wp_get_current_user();
$user_identity = $user->exists() ? $user->display_name : \'\';
$args = wp_parse_args( $args );
if ( ! isset( $args[\'format\'] ) )
$args[\'format\'] = current_theme_supports( \'html5\', \'comment-form\' ) ? \'html5\' : \'xhtml\';
$req = get_option( \'require_name_email\' );
$aria_req = ( $req ? " aria-required=\'true\'" : \'\' );
$html5 = \'html5\' === $args[\'format\'];
$fields = array(
\'author\' => \'<p class="comment-form-author">\' . \'<label for="author">\' . __( \'Name\' ) . ( $req ? \' <span class="required">*</span>\' : \'\' ) . \'</label> \' .
\'<input id="author" name="author" type="text" value="\' . esc_attr( $commenter[\'comment_author\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'email\' => \'<p class="comment-form-email"><label for="email">\' . __( \'Email\' ) . ( $req ? \' <span class="required">*</span>\' : \'\' ) . \'</label> \' .
\'<input id="email" name="email" \' . ( $html5 ? \'type="email"\' : \'type="text"\' ) . \' value="\' . esc_attr( $commenter[\'comment_author_email\'] ) . \'" size="30"\' . $aria_req . \' /></p>\',
\'url\' => \'<p class="comment-form-url"><label for="url">\' . __( \'Website\' ) . \'</label> \' .
\'<input id="url" name="url" \' . ( $html5 ? \'type="url"\' : \'type="text"\' ) . \' value="\' . esc_attr( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\',
);
$required_text = sprintf( \' \' . __(\'Required fields are marked %s\'), \'<span class="required">*</span>\' );
/**
* Filter the default comment form fields.
*
* @since 3.0.0
*
* @param array $fields The default comment fields.
*/
$fields = apply_filters( \'comment_form_default_fields\', $fields );
$defaults = array(
\'fields\' => $fields,
\'comment_field\' => \'<p class="comment-form-comment"><label for="comment">\' . _x( \'Comment\', \'noun\' ) . \'</label> <textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>\',
\'must_log_in\' => \'<p class="must-log-in">\' . sprintf( __( \'You must be <a href="%s">logged in</a> to post a comment.\' ), wp_login_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
\'logged_in_as\' => \'<p class="logged-in-as">\' . sprintf( __( \'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>\' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( \'the_permalink\', get_permalink( $post_id ) ) ) ) . \'</p>\',
\'comment_notes_before\' => \'<p class="comment-notes">\' . __( \'Your email address will not be published.\' ) . ( $req ? $required_text : \'\' ) . \'</p>\',
\'comment_notes_after\' => \'<p class="form-allowed-tags">\' . sprintf( __( \'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s\' ), \' <code>\' . allowed_tags() . \'</code>\' ) . \'</p>\',
\'id_form\' => \'commentform\',
\'id_submit\' => \'submit\',
\'title_reply\' => __( \'Leave a Reply\' ),
\'title_reply_to\' => __( \'Leave a Reply to %s\' ),
\'cancel_reply_link\' => __( \'Cancel reply\' ),
\'label_submit\' => __( \'Post Comment\' ),
\'format\' => \'xhtml\',
);
?>
现在可以进行更改、重命名函数并将其添加到
comment_form_defaults
即使您只需要更改一件事,您也需要处理这一完整的部分,以便验证正常工作