Cyber Monday Sale!

Save up to $50 through December 5th.

Shop Now

[Resolved] Changing Comment form default values

Home Forums Support [Resolved] Changing Comment form default values

Home Forums Support Changing Comment form default values

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1914606
    Kathryn

    Hello there,

    I have copied comments.php and added the code from this page. Comments are displayed below the form. I need to change the following:

    1. I need to change ‘LEAVE A COMMENT’ label to ‘COMMENT’
    2. Below ‘LEAVE A COMMENT’ it says ‘Logged in as admin. Log out?’ – I have custom pages (with their own links) which I would like ‘admin’ and ‘Log out’ to be linked to instead of default wp-admin pages.
    3. Below the comment input field and button, where it says ‘2 THOUGHTS ON…’ I would like to just say ‘2 COMMENTS’ without ‘ON’ and post title part.
    4. Below the comments number is the comment list – I would like the name to show first_name instead of username and to be linked to custom profile page (i have made a page) instead of default one.
    5. Edit button below the name – I would also like to change to custom link (haven’t yet made a template, but will later). In case I don’t make template, can you please tell me how to disable it totally.

    Appreciate your help.

    #1914825
    Elvin
    Staff
    Customer Support

    Hi Kathryn,

    For #1:

    You can use the filter provided here – https://docs.generatepress.com/article/generate_leave_comment/

    For #2:

    This is a WordPress core template.

    See here – https://github.com/WordPress/WordPress/blob/efaf4a8938bbeb8510c8e1e4cc6fe84a434c17c3/wp-includes/comment-template.php#L2455-L2464

    But I believe you can filter it using this filter – https://developer.wordpress.org/reference/hooks/comment_form_defaults/

    For #3:

    You can use this filter to change that – https://docs.generatepress.com/article/generate_comment_form_title/

    For #4:

    I don’t recommend changing this as you should let the users display their preferred $display_name. (for privacy)

    But if you really must change it, you can use get_comment_author filter for it.

    For #5:

    The edit link is baked into the theme – https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/comments.php#L28
    https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/comments.php#L59

    But we should be able to filter that using this code:

    add_filter( 'edit_comment_link', 'change_edit_comment_link', 10, 3 );
    function change_edit_comment_link( $link, $comment_id, $text ){
    	// do your thing here.
    }
    #1914930
    Kathryn

    Thank you very much!

    5. I actually don’t need to change anything here, since edit button is not available for non-admin.

    4. I have changed this several times in my templates to display first_name for each user, but here it is getting back to username again. First name is not important that much, it is changable – used so users can quickly change their name. Can I somehow force it to display first name?

    3. This filter is nice but I actually need it to be correct if plural and not plural, like with thoughts, e.g. ‘1 comment’, ‘2 comments’ etc (without ‘on post_title’ part at all). Tried to decipher %1s and %2s in code, but it overcomes my php capabilities.

    2. Here, I don’t need link to admin profile, just the text – again, to display first name, not username. Since I set that users must be logged in in order to comment, I need ‘logged in’ to be linked to my custom login page, not wp-login.php. Logout part works fine!

    1. Changed!

    #1914954
    Elvin
    Staff
    Customer Support

    For #2:

    Try this snippet.

    function change_logged_in_as_to_first_name( $defaults ) {
    	$comment = get_comment( $comment_ID );
    	$user=get_userdata($comment->user_id);
    	$firstname = $user->first_name;
      $defaults['logged_in_as'] = sprintf(
    			'<p class="logged-in-as">%s</p>',
    			sprintf(
    				/* translators: 1: Edit user link, 2: Accessibility text, 3: User name, 4: Logout URL. */
    				__( '<a href="%1$s" aria-label="%2$s">Logged in as %3$s</a>. <a href="%4$s">Log out?</a>' ),
    				get_edit_user_link(),
    				/* translators: %s: User name. */
    				esc_attr( sprintf( __( 'Logged in as %s. Edit your profile.' ), $firstname ) ),
    				$firstname,
    				/** This filter is documented in wp-includes/link-template.php */
    				wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ), $post_id ) )
    			)
    		);
      return $defaults;
    }
    add_filter( 'comment_form_defaults', 'change_logged_in_as_to_first_name' );

    For #3:

    Try Tom’s recommendation here – https://generatepress.com/forums/topic/removing-comment-title-thoughts-on/#post-327453

    For #4:

    Try this snippet.

    add_filter('get_comment_author', 'my_comment_author', 10, 1);
    
    function my_comment_author( $author = '' ) {
    
        $comment = get_comment( $comment_ID );
    
        if ( !empty($comment->comment_author) ) {
            if (!empty($comment->user_id)){
                $user=get_userdata($comment->user_id);
                $author=$user->first_name; // this is the actual line you want to change
            } else {
                $author = __('Anonymous');
            }
        } else {
            $author = $comment->comment_author;
        }
    
        return $author;
    }

    But IMO, I think you should let the users decide their display name. πŸ™‚

    #1915041
    Kathryn

    4. Done, thank you!
    3. I have child theme, so I am comfortable with changing comments.php core template. Removed those strings and now it is as it should be!
    2. It is not working unfortunately. Still displays username and it is still linked. Is it possible to just have plain text there, no link at all and although I see the first_name is in your code, it is not displayed as it should. And Log out can stay default one also (to current page).

    /** Users can change their name at any moment, so they choose after all. πŸ™‚

    #1915603
    Kathryn

    This is from the comments.php:

    $fields = array(
    		'author' => '<input placeholder="' . __( 'Name','generatepress' ) . ' *" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" />',
    		'email' => '<input placeholder="' . __( 'Email','generatepress' ) . ' *" id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" />',
    		'url' => '<input placeholder="' . __( 'Website','generatepress' ) . '" id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" />',
    	);
    
    $defaults = array(
    		'fields'		=> apply_filters( 'comment_form_default_fields', $fields ),
    		'comment_field' => '<p class="comment-form-comment"><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
    		'must_log_in' 	=> '<p class="must-log-in">' .  sprintf( __( 'You must be <a href="/sign-in/">logged in</a> to post a comment.','generatepress' ), wp_login_url( get_permalink() ) ) . '</p>',
    		'logged_in_as'	=> '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" class="comment-logout" title="Log out of this account">Log out?</a>','generatepress' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( get_permalink() ) ) . '</p>',
    		'comment_notes_before' => null,
    		'comment_notes_after'  => null,
    		'id_form'              => 'commentform',
    		'id_submit'            => 'submit',
    		'title_reply'          => apply_filters( 'generate_leave_comment', __( 'Leave a Comment','generatepress' ) ),
    		'title_reply_to'       => apply_filters( 'generate_leave_reply', __( 'Leave a Reply to %s','generatepress' ) ),
    		'cancel_reply_link'    => apply_filters( 'generate_cancel_reply', __( 'Cancel reply','generatepress' ) ),
    		'label_submit'         => apply_filters( 'generate_post_comment', __( 'Post Comment','generatepress' ) ),
    	);

    Under ‘must_log_in’ inside $defaults, I changed login url and it works. I need to change this author to display first name and to remove link. For removing link I see I can remove ‘a’ tag, but don’t know how to change this ‘%2$s’ to display $user->first_name or $author->first_name

    #1915741
    Kathryn

    Also, below in the comment list, admin name is still linked (contributor’s is not) – is it possible to unlink it, just to have plain text there. It returns first_name in this place (as it should), but it is unnecessary linked, it can just be text and I can’t find how to remove link for this. I am fine with deleting anything from the comments.php, using child theme. I tried removing admin_url( 'profile.php' ) from $defaults logged_in_as, but no success.

    #1916081
    Elvin
    Staff
    Customer Support

    Hi,

    I tried removing admin_url( ‘profile.php’ ) from $defaults logged_in_as, but no success.

    This should be it this line Logged in as <a href="%1$s">%2$s</a> is responsible for the “Logged in as”

    But if you’re changing this, make sure you’re not using the PHP filter I’ve provided as it may conflict.

    If you simply want to remove the link you can just change this line to Logged in as %2$s so we don’t have to bother w/ making it a link.

    Make sure you change $user_identity because that’s basically where the %2$s value comes from.

    You can add this code before the $defaults array.

    $comment = get_comment( get_comment_ID() );
    	$user=get_userdata($comment->user_id);
    	$firstname = $user->first_name;

    And then replace $user_identity with $firstname

    #1916096
    Kathryn

    Didn’t work, unfortunately. Removed it completely altogether with logout link, leave a reply, cancel reply and all that useless crap. I don’t know what for are all those things which are unchangable and basically have no use other than enormous time wasting.

    #1916106
    Elvin
    Staff
    Customer Support

    Can you provide us temporary backend access to the site? To check what may be conflicting with it.

    You can use the private information text field to provide the site details. Let us know.

    #1916109
    Kathryn

    No need and unfortunately, no time for that. I have displayed none all of it, although I would make use of something to cancel reply and return to default comment field, but since I can’t remove ‘Reply to USERNAME’, without removing ‘cancel reply’ everything has to go altogether. Because it is username and I need first_name, it is essential, everywhere else I have first_name displayed and users can edit that first_name as they want.

    #1916988
    Kathryn

    Actually, found a callback from comments.php inside original theme files inc/structure/comments.php, took the callback function from there and pasted it into functions.php of my child theme, renamed the function and the callback inside the comments.php file and edited the function.

    #1917150
    Tom
    Lead Developer
    Lead Developer

    Hi Kathryn,

    Did that do the trick for you? If so, it’s always a good idea to keep an eye on theme updates to see if there’s anything you should add/remove from your child theme templates πŸ™‚

    #1918247
    Kathryn

    Hi Tom,

    Yeah, it did the trick – the point is in making different ‘callback’ and therefore having different(ly named) function in functions.php. When I tried to edit default ‘generate_comment’ (maybe I am not exactly correct with default callback name) callback and took the default function with default name, it didn’t work. Only when I did comment out old callback and added new, copied function from inc folder and renamed it, only then edits were applied. Sure, thank you.

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.