Use of comment_reply_link_args filter

i am trying to create a filter like this

add_filter('comment_reply_link_args','change_author_title');
function change_author_title( $args ) {
  $defaults = array(
        'add_below'     => 'comment',
        'respond_id'    => 'respond',
        'reply_text'    => __( 'Reply' ),
        /* translators: Comment reply button text. %s: Comment author name. */
        'reply_to_text' => __( 'THIS TEXT' ),
        'login_text'    => __( 'Log in to Reply' ),
        'max_depth'     => 0,
        'depth'         => 0,
        'before'        => '',
        'after'         => '',
    );

    $args = wp_parse_args( $args, $defaults );
    return $args;
}

What i actually need is to filter the ‘reply_to_text’ from the $defaults array.

The reason behind this is that i created a filter with get_comment_author

add_filter('get_comment_author', 'my_comment_author', 10, 2);
function my_comment_author( $author, $comment_ID ) {
    if (! is_admin()) {
    // Get the comment ID from WP_Query
    $comment = get_comment( $comment_ID );
    $authoremail = get_comment_author_email( $comment); 
    $user = get_user_by( 'email', $comment->comment_author_email );

    if (! email_exists($authoremail)) {
        if(mb_strlen($comment->comment_author, 'UTF-8')>13){
        $author = mb_substr($comment->comment_author,0,13, 'UTF-8').'.'; }
    } else {
        if( ! empty( $user->first_name ) ){
        $author = $user->first_name;  }
        else {
        return $author;  }
    } }

    return $author;
}

and i would like to pass the same output in $defaults array ‘reply_to_text’.

Could someone give me a tip?

Thank you

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You mean, something like this?

add_filter( 'comment_reply_link_args', 'change_author_title', 10, 2 );
function change_author_title( $args, $comment ) {
    $args['reply_to_text'] = 'Reply to ' . get_comment_author( $comment );
    return $args;
}

Explanation: I changed the function declaration so that it accepts the second parameter ($comment) which is the comment object (a WP_Comment instance), then I simply call the get_comment_author() which fires the get_comment_author hook. That way, your my_comment_author() function would be called and then you’d get the same author name for use with the reply_to_text arg.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x