自定义COMMENT_FORM字段不显示

时间:2012-10-15 作者:user1255049

我理想的评论表单将有3个字段-名称、位置和评论。我正在使用以下代码位-

评论。php

<?php comment_form(
    array(
        \'fields\' => apply_filters( \'comment_form_default_fields\', $fields ),
        \'comment_notes_after\' => \' \',
        \'title_reply\' => \'Please feel free to share your home owning hopes, dreams, or concerns?\',
        \'logged_in_as\' => \'\',
    )
); ?>
功能。php

<?php

function my_fields($fields) {
$fields[\'Name\'] = \'<p>Name</p>\';
$fields[\'Location\'] = \'<p>Location</p>\';
return $fields;
}
add_filter(\'comment_form_default_fields\',\'my_fields\');

?>
正如您在live site, 这行不通。有什么想法为什么/如何补救?

1 个回复
SO网友:chrisguitarguy

我想你对如何comment_form 作品那么让我们看看(这是wp-includes/comment-template.php):

<?php
function comment_form( $args = array(), $post_id = null ) {
    // snip snip 
    $fields =  array(
        \'author\' => \'<p class="comment-form-author">\' . \'<label for="author">\' . __( \'Name\' ) . \'</label> \' . ( $req ? \'<span class="required">*</span>\' : \'\' ) .
                    \'<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\' ) . \'</label> \' . ( $req ? \'<span class="required">*</span>\' : \'\' ) .
                    \'<input id="email" name="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" type="text" value="\' . esc_attr( $commenter[\'comment_author_url\'] ) . \'" size="30" /></p>\',
    );

    $required_text = sprintf( \' \' . __(\'Required fields are marked %s\'), \'<span class="required">*</span>\' );
    $defaults = array(
        \'fields\'               => apply_filters( \'comment_form_default_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>\' ), admin_url( \'profile.php\' ), $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\' ),
    );

    $args = wp_parse_args( $args, apply_filters( \'comment_form_defaults\', $defaults ) );

        // $args get used to fill out the fields here
}
只需重写要更改的参数。如果需要自定义字段,只需传入字段,无需使用钩子comments_form_default_fields.

过于简化的示例:

<?php comment_form(array(
    \'fields\'    => array(
        \'author\' => \'<input type="text" name="author" />\',
        \'location\' => \'<input type="text" name="location" />\',
    ),
));
WordPress将负责保存作者字段,但要保存位置,您还需要做更多的工作。我写了一个tutorial about this.

基本上:钩入comment_post. 检查$_POST 对于字段,保存它:

<?php
add_action( \'comment_post\', \'wpse69222_insert_comment\', 10, 1 );
function wpse69222_insert_comment( $comment_id )
{
    // prolly should do more validation here?
    if( isset( $_POST[\'location\'] ) )
        update_comment_meta( $comment_id, \'location\', esc_attr( $_POST[\'location\'] ) );
}
您可能还需要管理区域字段和从管理员处编辑该位置的方法,但这是另一个问题。

结束

相关推荐

将do_action()替换为Comments.php中的普通提交表单

在我的评论中。php我有以下代码: <input name=\"submit\" type=\"submit\" id=\"submit\" tabindex=\"5\" value=\"Submit\" /> <?php comment_id_fields(); ?> <?php do_action(\'comment_form\', $post->ID); ?> 有没有办法用一个简单的提交表单来替换此代码?并且不使用:do\\u