0

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

1 Answer 1

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.

1
  • 1
    Thank you very much , this is what i was trying to do . Commented Jul 16, 2021 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.