Измените имя автора обзора на первые и последние буквы только со звездочками в WooCommerce - PullRequest
1 голос
/ 19 июня 2020

Я использую « Как изменить отображаемое имя автора обзора в WooCommerce », чтобы я мог изменить автора обзора, показанного на сайте, на имя, за которым следует начальная фамилия.

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

Что мне нужно сделать, так это использовать только имя (не отображать фамилию) и заменить все символы, кроме первого и последнего, на '*'.

Так, например, Джеймс становится J *** s, а Майкл становится M ***** l

1 Ответ

1 голос
/ 19 июня 2020

Настройки:

  • Отображать имя
  • Джеймс становится J *** s

добавлен комментарий с пояснением в коде

function my_comment_author( $author, $comment_id, $comment ) {  
    // NOT empty
    if ( $comment ) {
        // Get user id
        $user_id = $comment->user_id;

        // User id exists
        if( $user_id > 0 ) {
            // Get user data
            $user = get_userdata( $user_id );

            // User first name
            $user_first_name = $user->first_name;

            // Call function
            $author = replace_with_stars( $user_first_name );       
        } else {
            $author = __('Anonymous', 'woocommerce');
        }
    }

    return $author;
}
add_filter('get_comment_author', 'my_comment_author', 10, 3 );

function replace_with_stars( $str ) {
    // Returns the length of the given string.
    $len = strlen( $str );

    return substr( $str, 0, 1 ).str_repeat('*', $len - 2).substr( $str, $len - 1, 1 );
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...