• Resolved janew

    (@janew)


    Someone over at https://ww.wp.xz.cn/support/forum/how-to-and-troubleshooting/ suggested I post this here. Seems like a WordPress thing, rather than a theme thing, but I’m not tryna make waves, just get some help….

    OK, I figured out how to do some of the stuff I wanted, but I am seeing that the rest of what I want to do is contained wholly in the comments_form, which I can’t touch.

    Specifically, I want to move p.comment-notes from below the title to below the email entry field.

    I am using the first code provided here:
    https://ww.wp.xz.cn/support/topic/change-comments-form/
    to re-order the fields … but it doesn’t seem to affect the p.comment-notes text, which remains at the top. So I guess I am using the wrong word. I have tried both
    $comment_notes_field = $fields['comment_notes'];
    and
    $comment_notes_beforefield = $fields['comment_notes_before'];

    What is the correct secret word? Or how can I do this?

    I also want to either have an asterisk beside the Comments label (because that’s required too, right?) or to remove the asterisk from the others and change the text to read ‘all fields required’.

    I can use the second code on that page to change the field labels, to add an asterisk to the Comment label, but I’d prefer to remove the asterisk altogether and change the text to read ‘All fields required’. But I can’t figure out how to change the required text.

    How do I change the ‘Required fields are marked *’ text?

    I have tried using the comment_form_defaults hook, but all I’ve been able to change is the title. Everything else I attempt remains the same, or removes all formatting and just drops it in as plain text.

    *In case it’s a concern, I am removing the Website field entirely from the comments form
    p.comment-form-url {display: none; }
    So requiring all fields is just requiring name, email, and comments.

    Thanks!

Viewing 1 replies (of 1 total)
  • Thread Starter janew

    (@janew)

    Nevermind! lol

    I have found workarounds and a great code from @alexmoise (referenced in link above), to which I added the ‘cookies’ field. This is the full, slightly modified code:

    // comment form fields re-defined:
    add_filter( 'comment_form_default_fields', 'mo_comment_fields_custom_html' );
    function mo_comment_fields_custom_html( $fields ) {
    	// first unset the existing fields:
    	unset( $fields['comment'] );
    	unset( $fields['author'] );
    	unset( $fields['email'] );
    	unset( $fields['url'] );
    	unset( $fields['cookies'] );
    	// then re-define them as needed:
    	$fields = [
    		'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'A CUSTOM COMMENT LABEL', 'noun', 'textdomain' ) . '</label> ' .
    			'<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" aria-required="true" required="required"></textarea></p>',
    		'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'A CUSTOM NAME LABEL', 'textdomain'  ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
    			'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245"' . $aria_req . $html_req . ' /></p>',
    		'email'  => '<p class="comment-form-email"><label for="email">' . __( 'A CUSTOM EMAIL LABEL', 'textdomain'  ) . ( $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" maxlength="100" aria-describedby="email-notes"' . $aria_req . $html_req  . ' /></p>',
    		'cookies'	=> '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes" />&nbsp;<label for="wp-comment-cookies-consent">Save my name and email in this browser for the next time I comment.</label></p>',
    		'url'    => '<p class="comment-form-url"><label for="url">' . __( 'A CUSTOM WEBSITE LABEL', 'textdomain'  ) . '</label> ' .
    			'<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>',
    	];
    	// done customizing, now return the fields:
    	return $fields;
    }
    // remove default comment form so it won't appear twice; leave it if user is already logged in
    add_filter( 'comment_form_defaults', 'mo_remove_default_comment_field', 10, 1 ); 
    
    function mo_remove_default_comment_field( $defaults ) { 
    	if ( !is_user_logged_in() ) {
    		if ( isset( $defaults[ 'comment_field' ] ) ) { 
    			$defaults[ 'comment_field' ] = ''; 
    		}
    	}
    	return $defaults; 
    }

    I used css to hide the display of the comment-notes section and simply added “(will not be published)” note after the Email label. Hiding Website field makes all fields required and, though I didn’t figure out a way to add an “all fields required” notice (it’s probably simple but I’m sick of dealing with this so not even gonna try) I think it’s implied.

    Hope this helps someone else! All credit to @alexmoise!

Viewing 1 replies (of 1 total)

The topic ‘renovate comments section’ is closed to new replies.