• Resolved Nipon

    (@nipon)


    I added placeholders to the comment form using the following code in comments. php and it works just fine.

    <?php
    	$comments_args = array(
            // change the title of send button
            // 'label_submit'=>'Send',
            // change the title of the reply section
            // 'title_reply'=>'Write a Reply or Comment',
            // remove "Text or HTML to be displayed after the set of comment fields"
            'comment_notes_after' => '<p><small>Your comment may be subject to editorial review.</small></p>',
            // redefine your own textarea (the comment body)
            'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><br /><textarea id="comment" name="comment" aria-required="true" placeholder="Type your comment here..." rows="8" cols="37" wrap="hard"></textarea></p>',
    		'fields' => apply_filters( 'comment_form_default_fields', array(
    
        'author' =>
          '<p class="comment-form-author">' .
          '<input id="author" placeholder="Your name..." name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
          '" size="30"' . $aria_req . ' />' . ( $req ? '<span style="color:red" class="required">*</span>' : '' ) . '</p>',
    
        'email' =>
          '<p class="comment-form-email">' .
          '<input id="email" placeholder="Your email..." name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
          '" size="30"' . $aria_req . ' />' . ( $req ? '<span style="color:red" class="required">*</span>' : '' ) . '</p>',
    
        'url' =>
          '<p class="comment-form-url">' .
          '<input id="url" placeholder="Your website..." name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
          '" size="30" /></p>'
        )
      ),
    );
    	comment_form($comments_args); ?>

    However, according to the Codex, I must set the variables (present in the above code) within my callback using the following code – which I did not do.

    $commenter = wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );

    Where do I exactly place the above code? The output, by the way, is just fine i.e. I can see the placeholders in the comment form alright.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Sorry for the slow response, I’ve been away.

    Without that snippet, your code will be throwing “undefined variable” warnings, though it will otherwise work OK, except that a theme specific attribute will be missing, which will not matter unless you are using that theme. Still, you want to avoid throwing warnings, so the code should be included, or the portions reliant on it edited out.

    The snippet needs to be before the arguments array is defined, so in your example, right after <?php.

    Thread Starter Nipon

    (@nipon)

    Thanks. That really helped.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding placeholders in comments form’ is closed to new replies.