#php #wordpress
Вопрос:
Я использую плагин, который дает мне возможность настроить конкретное предложение с помощью фильтра wordpress.
Это исходный код:
add_filter('filter_stamper_text_after_replacing_tags', 'customize_text_to_stamp', 10, 2);
function customize_text_to_stamp ($text_to_stamp, $additional_params)
{
$text_to_stamp .= "Adding this test message to the stamping test";
return $text_to_stamp;
}
Мне нужно вставить текущее имя пользователя в метку $text_to_stamp, чтобы оно возвращалось как:
$text_to_stamp .= "Some text here" . Current-Username. "some text here";
Это предложение отображается только при наличии зарегистрированного пользователя, поэтому в этой проверке нет необходимости.
Я не знаю, как получить текущее имя пользователя и как вставить его в предложение.
Вы не могли бы мне помочь?
Ответ №1:
Попробуйте приведенный ниже код, который поможет вам.
add_filter('filter_stamper_text_after_replacing_tags', 'customize_text_to_stamp', 10, 2);
function customize_text_to_stamp ($text_to_stamp, $additional_params)
{
$current_user = wp_get_current_user();
// Which you want to dispaly you can set below type
// $current_user->first_name //First Name of login User
// $current_user->last_name //Last Name of login User
// $current_user->display_name // Display name of login User
$text_to_stamp .= "Some text here" . $current_user->user_login . "some text here";
return $text_to_stamp;
}
Ответ №2:
add_filter('filter_stamper_text_after_replacing_tags','customize_text_to_stamp', 10, 2);
function customize_text_to_stamp ($text_to_stamp, $additional_params)
{
if (is_user_logged_in()){
$current_user_name=wp_get_current_user()->user_login;
$text_to_stamp .= "Some text here" . esc_html($current_user_name). "some text
here";
}else{
$text_to_stamp .= "Adding this test message to the stamping test";
}
return $text_to_stamp;
}