Please can someone help me write a filter for update_welcome_user_subject()
What I have currently is:
function custom_welcome_user_subject( $subject ) {
$subject = sprintf(
__( 'Login information [%2$s]' ),
$current_site->site_name,
$user->user_login
);
return $subject;
}
add_filter( 'update_welcome_user_subject', 'custom_welcome_user_subject', 11, 1 );
This code is passing "Login information []", but not the username. I do not appear to have access to the $user variable.
The relevant code in core is found in wp-includes/ms-functions.php line 1576
Please can someone fix my filter so the username is passed in the subject line.
Many thanks
Reigel Gallarde answers:
$current_user = wp_get_current_user();
then you can access user login like this...
$current_user->user_login
designbuildtest comments:
Hi Reigel, your suggestion does not work I'm afraid.
Reigel Gallarde comments:
I see.... try this...
function custom_welcome_user_subject( $subject ) {
$_subject = str_replace('New','',$subject);
$_subject = explode('User:',$_subject);
$subject = sprintf( __( 'Login information [%2$s]' ), $_subject[0], $_subject[1] );
return $subject;
}
designbuildtest comments:
Thanks Reigel, that works. Any idea why I can't access the $user variable in my original code?
timDesain Nanang answers:
You can try something like:
function custom_welcome_user_subject( $subject ) {
$new_sbj = explode(': ', $subject);
$subject = 'Login information ['.$new_sbj[1].']';
return $subject;
}
add_filter( 'update_welcome_user_subject', 'custom_welcome_user_subject', 11, 1 );
designbuildtest comments:
Many thanks - works like a charm. Is similar to Reigel's suggestion above but a little more concise.
Do you have any idea why I can't access the $user variable in my original code?
Thanks again
timDesain Nanang comments:
i see, but we don't copied each other (at 1:20am).
line: 1576
$subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_site->site_name, $user->user_login) );
this filter doesn't have variable that passed to the filter functions.
we can just manipulate the output ($subject) string.
ref: https://codex.wordpress.org/Function_Reference/apply_filters
designbuildtest comments:
Many thanks. Your explanation helps me understand why my original code wasn't working :-)
Don't worry, I never thought the two answers were copied. I'm closing the question now and splitting the prize award 50/50.
Thanks again for your help.