WordPress.org

Ready to get started?Download WordPress

Forums

Replace required asterisk with text (10 posts)

  1. Ottens
    Member
    Posted 2 years ago #

    In my comment form, I'd like to replace the asterisk that denotes required fields with the text "Required" so, instead of,

    Name*

    it looks like,

    Name (Required)

    My comments.php calls the comments_form like this:

    <?php comment_form(); ?>

    I know I can add code to functions.php to change the layout of the form but I don't know how to. Can someone help?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. Ottens
    Member
    Posted 2 years ago #

    I found that but I can't get it to work yet. I'm not that experienced at coding. If someone could show me an example, that would help.

  4. jdavhar
    Member
    Posted 2 years ago #

    http://www.1stwebdesigner.com/wordpress/comment-form-customization/
    May be helpful to you if the following isn't.

    <?php $defaults = array( 'fields' => apply_filters( 'comment_form_default_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" tabindex="1"' . $aria_req . ' />' .
                    '</p><!-- #form-section-author .form-section -->',
        '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" tabindex="2"' . $aria_req . ' />' .
                    '</p><!-- #form-section-email .form-section -->',
        'url'    => '
    <p class="comment-form-url">' .</p>
    
                    '<label for="url">' . __( 'Website' ) . '</label>' .
                    '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" tabindex="3" />' .
                    '
    
    <!-- #<span class="hiddenSpellError" pre="">form-section-url</span> .form-section -->' ) ),
        'comment_field' => '<p class="comment-form-comment">' .
                    '<label for="comment">' . __( 'Comment' ) . '</label>' .
                    '<textarea id="comment" name="comment" cols="45" rows="8" tabindex="4" aria-required="true"></textarea>' .
                    '</p><!-- #form-section-comment .form-section -->',
        '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="%s">%s</a>. <a title="Log out of this account" href="%s">Log out?</a></p>
    
    ' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ),
        'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email is <em>never</em> published nor shared.' ) . ( $req ? __( ' Required fields are marked <span class="required">*</span>' ) : '' ) . '</p>',
        'comment_notes_after' => '<dl class="form-allowed-tags"><dt>' . __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:' ) . '</dt> <dd><code>' . allowed_tags() . '</code></dd>',
        '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' ),
    ); ?>

    those are defaults, here's modified:

    <?php $defaults = array( 'fields' => apply_filters( 'comment_form_default_fields', array(
        'author' => '<p class="comment-form-author">' .
                    '<label for="author">' . __( 'Name' ) . '</label> ' .
                    ( $req ? '<span class="required">Required</span>' : '' ) .
                    '<input id="author" name="author" type="text" value="' .
                    esc_attr( $commenter['comment_author'] ) . '" size="30" tabindex="1"' . $aria_req . ' />' .
                    '</p><!-- #form-section-author .form-section -->',
        'email'  => '<p class="comment-form-email">' .
                    '<label for="email">' . __( 'Email' ) . '</label> ' .
                    ( $req ? '<span class="required">Required</span>' : '' ) .
                    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" tabindex="2"' . $aria_req . ' />' .
                    '</p><!-- #form-section-email .form-section -->',
        'url'    => '
    <p class="comment-form-url">' .</p>
    
                    '<label for="url">' . __( 'Website' ) . '</label>' .
                    '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" tabindex="3" />' .
                    '
    
    <!-- #<span class="hiddenSpellError" pre="">form-section-url</span> .form-section -->' ) ),
        'comment_field' => '<p class="comment-form-comment">' .
                    '<label for="comment">' . __( 'Comment' ) . '</label>' .
                    '<textarea id="comment" name="comment" cols="45" rows="8" tabindex="4" aria-required="true"></textarea>' .
                    '</p><!-- #form-section-comment .form-section -->',
        '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="%s">%s</a>. <a title="Log out of this account" href="%s">Log out?</a></p>
    
    ' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ),
        'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email is <em>never</em> published nor shared.' ) . ( $req ? __( ' Required fields are marked <span class="required">Required</span>' ) : '' ) . '</p>',
        'comment_notes_after' => '<dl class="form-allowed-tags"><dt>' . __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:' ) . '</dt> <dd><code>' . allowed_tags() . '</code></dd>',
        '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' ),
    ); ?>
  5. Ottens
    Member
    Posted 2 years ago #

    Thanks jdavhar. That means I have to put the full code of the comments form on my comments.php though where it currently just calls the form with a simple, single line of code. I know it's supposed to be possible to change specific things about the default form in the functions.php which would be a more elegant solution.

  6. jdavhar
    Member
    Posted 2 years ago #

    Maybe this is what you're looking for?

    <?php
    function comment_form_fixer($content){
    return str_replace('*', ' (Required)', $content);
    }
    add_filter('comment_form', 'comment_form_fixer');
    ?>
  7. Ottens
    Member
    Posted 2 years ago #

    I guess that should work but it doesn't...

  8. jdavhar
    Member
    Posted 2 years ago #

    Tested this one, works for sure:

    function modify_fields_required_marker($fields) {
    $commenter = wp_get_current_commenter();
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );
    $fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . '</label> ' . ( $req ? '<span class="required"> (Required)</span>' : '' ) .
    		         '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>';
    $fields['email'] = '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . '</label> ' . ( $req ? '<span class="required"> (Required)</span>' : '' ) .
    		         '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>';
    $fields['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>';
    return $fields;
    }
    add_filter('comment_form_default_fields','modify_fields_required_marker');
  9. crystal awards
    Member
    Posted 2 years ago #

    Yeah, I would like to ask the same question as Ottens. I can't get it to work yet. if anyone solve it, thanks!

  10. favordeal
    Member
    Posted 2 years ago #

    I tested, I thought that should work but it still doesn't...

Topic Closed

This topic has been closed to new replies.

About this Topic