最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Why wordpress custom template comments shows Undefined index?

matteradmin7PV0评论

During making a custom template comment I see this notice:

Notice : Undefined index: comment_field in C:\xampp\htdocs\..\wp-content\themes\..\functions\comments-html.php on line 155

Following is my Code..Can anybody solve this?

Wordpress Version : 5.1.1 PHP 7.2

<?php function brick_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
$comment_ID = get_comment_ID();
extract($args, EXTR_SKIP);
if ( 'div' == $args['style'] ) {
    $tag = 'div';
    $add_below = 'comment';
} else {
    $tag = 'li';
    $add_below = 'div-comment';
}
?>
<<?php echo $tag ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ) ?> id="comment-<?php comment_ID() ?>">
<div class="commentbox">
    <?php if ( 'div' != $args['style'] ) : ?>
    <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
        <?php endif;
        $author_name = get_comment_author( $comment_ID );
        ?><div class="comment-inner-body">
            <div class="cm clearfix">
                <?php if ( $comment->comment_approved == '0' ) : ?>
                    <div class="comment-awaiting-moderation">
                        <?php _e( 'Your comment is awaiting moderation.' ); ?>
                    </div>
                <?php endif; ?>

                <div class="img">
                    <?php echo get_avatar( $comment, 80 ); ?>
                </div>
                <div class="txt">

                    <div class="headline clearfix">
                        <div class="right-head">
                            <span class="title"><?php echo $author_name; ?></span>
                            <span class="date"><?php echo get_comment_date('Y/m/d'); ?></span>
                        </div>
                        <div class="reply-comment left-header">
                            <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'reply_text ' => 'پاسخ به نظر', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
                        </div>
                    </div>
                    <div class="details">
                        <?php comment_text(); ?>
                    </div>
                </div>
            </div>
            <?php if ( 'div' != $args['style'] ) : ?>
        </div>
    <?php endif; ?>
    </div>
</div>
<?php } if( ! function_exists('brick_comment_form') ){
/**
 * Comment form
 */
function brick_comment_form($args = array(), $post_id = null ){
    $post_id = get_the_ID();
    $commenter = wp_get_current_commenter();
    $user = wp_get_current_user();
    $user_identity = $user->exists() ? $user->display_name : '';
    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' => '
        <div class="col-12 col-sm-12 col-md-4 pcol-5 padding-5">
            <div class="comment-form-author comment-field form-group">
                <input class="form-control" id="author" placeholder="نام و نام خانوادگی" name="author" type="text" 
                value="' . esc_attr( $commenter['comment_author'] ) . '" ' . $aria_req . ' />
            </div>
        ',
        'url'   => '
            <div class="comment-form-url comment-field form-group">' .
            '<input class="form-control" id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" maxlength="200" placeholder="نام سایت"/>
            </div>
    ',
        'email'  => '
            <div class="comment-form-email comment-field form-group">
                <input id="email" class="form-control" name="email" placeholder="آدرس ایمیل" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' 
                value="' . esc_attr(  $commenter['comment_author_email'] ) . '" ' . $aria_req . ' />
            </div>
        </div>
    ',
        'comment_field'  => '
        <div class="col-12 col-sm-12 col-md-8 comment-text form-group clearfix  padding-5">
            <textarea class="form-control" id="comment" name="comment" placeholder="متن دیدگاه" aria-required="true"></textarea>
        ',
        'submit'  => '
            <label class="submitCM float-left ">
                <input class="btn send-btn" id="submit" name="submit" type="submit" value="ارسال" />        
            </label>
        </div>',
    );
    $required_text = sprintf( ' ' . __('Required fields are marked %s', 'websima'), '<span class="required">*</span>' );
    $defaults = array(
        'fields'               => apply_filters( 'comment_form_default_fields', $fields ),
        'must_log_in'          => '
<div class="alert alert-danger 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 ) ) ) )
            . '</div>',

        'logged_in_as'  => '<div class="col-12"><div class="alert-by-comment"><div class="alert logged-in-as">' . sprintf( __( ' شما با نام کاربری <a href="%1$s" class="trn_elm_all">%2$s</a> وارد شده اید . <a href="%3$s" title="Log out of this account" class="log-out trn_elm_all">خروج</a>', 'websima' ), get_edit_user_link(), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</div></div></div>
<div class="col-12">
    <div class="form-group comment-form-comment">
        <div class="comment-text">
            <textarea class="form-control" id="comment" name="comment" placeholder="' . _x( 'اینجا پیام خود را بنویسید...',  'websima' ) . '" rows="5" aria-required="true"></textarea>
        </div>
        <div class="form-submit text-left when_login">
            <div class="send-for-damin">
                <input class="comment-sent full-btn btn send-btn"  id="submit" name="submit" type="submit" value="ارسال" /> 
            </div>
        </div>
    </div>
</div> ',

        'comment_notes_before' => '',
        'comment_notes_after'  => '<div class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s', 'websima' ), ' <code>' . allowed_tags() . '</code>' ) . '</div>',
        'id_form'              => 'commentform',
        'id_submit'            => 'submit',
        'title_reply'          => __( 'Leave a Reply', 'websima' ),
        'title_reply_to'       => __( 'Leave a Reply to %s', 'websima' ),
        'cancel_reply_link'    => __( 'انصراف', 'websima' ),
        'label_submit'         => __( 'Post Comment', 'websima' ),
        'format'               => 'xhtml',
    );
    $args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
    if ( comments_open( $post_id ) ) { ?>
        <?php do_action( 'comment_form_before' ); ?>
        <div id="respond" class="comment-respond">
        <?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) { ?>
            <?php echo $args['must_log_in']; ?>
            <?php do_action( 'comment_form_must_log_in_after' ); ?>
        <?php } else { ?>
            <form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>"
                  class="row form-horizontal border_bt comment-form"<?php echo $html5 ? ' novalidate' : ''; ?>  >
                <?php do_action( 'comment_form_top' ); ?>
                <?php if ( is_user_logged_in() ) { ?>
                    <?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
                    <?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
                <?php } else { ?>
                    <?php echo $args['comment_notes_before']; ?>
                    <?php
                    do_action( 'comment_form_before_fields' );
                    foreach ( (array) $args['fields'] as $name => $field ) {
                        echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
                    }
                    do_action( 'comment_form_after_fields' );
                }
                echo apply_filters( 'comment_form_field_comment', $args['comment_field'] );
                echo $args['comment_notes_after']; ?>
                <?php comment_id_fields( $post_id ); ?>
                <?php do_action( 'comment_form', $post_id ); ?>
            </form>
            </div>

        <?php } ?>

        <?php do_action( 'comment_form_after' ); ?>
    <?php } else { ?>
        <?php do_action( 'comment_form_comments_closed' ); ?>
    <?php } ?>
    <?php
} }?>

line error :

echo apply_filters( 'comment_form_field_comment', $args['comment_field'] );

thanks :)

Post a comment

comment list (0)

  1. No comments so far