Skip to content
Advertisement

WordPress Filter – Get Username

I am using a plugin that gives me the option to customize a specific sentence through wordpress filter.

This is the orginal code:

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;
}

I need to insert current-username in the $text_to_stamp, so it returns like:

$text_to_stamp .= "Some text here" . Current-Username. "some text here";

This sentence is only displayed if there is a logged in user, so this verification is not necessary.

I dont know how to get the current username and how to insert it into the sentence.

Can you help me?

Advertisement

Answer

Try the below code that will help you.

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;
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement