Resetting comment field in code

Hi @janew , we’re going to do it the other way around, basically saying “if not user logged in then proceed…”

For that we’ll wrap the “if a comment field is set” part with another if, checking for the negative of “is_user_logged_in()” by just putting a “!” in front of it 🙂

Like this:

// remove default comment form so it won't appear twice
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; 

That’s all, please let me know if it does what you need.

Best regards,

@alexmoise That’s perfect!

I was sooo close to figuring this out. lol

I figured it had to be this way but couldn’t think how to write is_user_not_logged_in, and totally forgot about the ! – I can understand it when it shows up in code I am reading, but I can never remember those symbols to write them.

So everything is how I envisioned, except I think I would have made the mistake of leaving the last line return $defaults within the isset area – which is to say I would have had two braces after that line instead of one.

So thank you very much for this, and for the rest of the code that it belongs to. It’s all super fantastic. I wish WordPress was easier to customize in this way, but maybe it just isn’t possible.

Thank you!

Welcome @janew, happy to know it worked!

Yeah, it will become a bit easier with Gutenberg and full site editing, but from that point on, the code will still do wonders … it always does 🙂

Have a great day,

Source link