In wordpress in user edit page fields below exist :
username* : which must be unique
name
nickname* : which is by default made from username
List item
display name
I want to change that default to name
in this page the solution below was suggested which did not work for me
adding thses code at the end of function.php in my theme :
add_action( 'user_registration_after_register_user_action', 'ur_insert_nickname', 1, 3 );
function ur_insert_nickname( $valid_form_data, $form_id, $user_id ) {
$user_nickname = $valid_form_data['first_name']->value . ' ' . $valid_form_data['last_name']->value;
update_user_meta( $user_id, 'nickname', $user_nickname );
}
now does anyone has any suggestions?
Try this approach Tested in(admin backend)
add_action('user_register','my_function');
function my_function($user_id){
//do your stuff
$first_name = get_user_meta($user_id, 'first_name', true);
$last_name = get_user_meta($user_id, 'last_name', true);
$user_nickname = $first_name.' '.$last_name;
update_user_meta( $user_id, 'nickname', $user_nickname );
}
Related
It is possible set display name from entered string into nickname registration field? I trying do this with simple hook, but after all it is not work.
function set_default_display_name( $user_id ) {
$user = get_userdata( $user_id );
$name = $user->nickname;
$args = array(
'ID' => $user_id,
'display_name' => $name
);
wp_update_user( $args );
}
add_action( 'user_register', 'set_default_display_name' );
By default immediately after registration the display name was set from WP username (login) not nickname. Can sombody help me to set a display name from nickname?
// change default display name format
add_action('user_register', 'registration_save_displayname', 1000);
function registration_save_displayname($user_id) {
if ( isset( $_POST['first_name'])){
$pretty_name = $_POST['first_name'];
wp_update_user( array ('ID' => $user_id, 'display_name'=> $pretty_name) ) ;
}
}
I am using this code which is partially working for changing the profile url everywhere in buddypress and wordpress from “http:/mywebsite/user/username” to “http:/mywebsite/user/userid”
function _bp_core_get_user_domain($domain, $user_id, $user_nicename = false, $user_login = false) {
if ( empty( $user_id ) ){
return;
}
if( isset($user_nicename) ){
$user_nicename = bp_core_get_username($user_id);
}
$after_domain = bp_get_members_root_slug() . '/' . $user_id;
$domain = trailingslashit( bp_get_root_domain() . '/' . $after_domain );
$domain = apply_filters( 'bp_core_get_user_domain_pre_cache', $domain, $user_id, $user_nicename, $user_login );
if ( !empty( $domain ) ) {
wp_cache_set( 'bp_user_domain_' . $user_id, $domain, 'bp' );
}
return $domain;
}
add_filter('bp_core_get_user_domain', '_bp_core_get_user_domain', 10, 4);
function _bp_core_get_userid($userid, $username){
if(is_numeric($username)){
$aux = get_userdata( $username );
if( get_userdata( $username ) )
$userid = $username;
}
return $userid;
}
add_filter('bp_core_get_userid', '_bp_core_get_userid', 10, 2);
function _bp_get_activity_parent_content($content){
global $bp;
$user = get_user_by('slug', $bp->displayed_user->fullname); // 'slug' - user_nicename
return preg_replace('/href=\"(.*?)\"/is', 'href="'.bp_core_get_user_domain($user->ID, $bp->displayed_user->fullname).'"', $content);
}
add_filter( 'bp_get_activity_parent_content','_bp_get_activity_parent_content', 10, 1 );
add_filter('bp_core_get_userid_from_nicename', '_bp_core_get_userid', 10, 2);
It is working perfectly for me at the moment BUT not on this little place (see picture):
http://i.imgur.com/4dX0RUB.png
– url change of the author of an activity starting-message is not working in both groups activities and personnal activities
url change of the author of an activity REPLY is working
I don’t know if I am explaining very well what issue I have got but I hope you will understand.
Thank you for your answers
PS : thanks to aSeptik from StackExchange for the code
It's impossible to do that on a fly gracefully. BuddyPress Activity component is developed in such a way, that those text with the user link in activity stream (for site-wide, personal and groups) is stored directly in the database as an action. Just take a look at wp_bp_activity.action field in your DB.
So you should filter and preg_replace it as well. I guess you know that you are penalting yourself with rendering speed loss.
I am using the following snippet in my theme to get the logged-in user's avatar of the default wp user's setting page.
<?php echo get_avatar($id_or_email, $size='64', $default, $alt='User name' ); ?>
However, After installing the buddypress; I wanted to display the uploaded profile's photo of the user where the above snippet only shows the default wp avatar.
Any idea on how to achieve that?
You should enable both options:
<?php
if ( empty ( bp_loggedin_user_id() == false ) ) {
$user_id = bp_loggedin_user_id();
echo bp_core_fetch_avatar( array( 'item_id' => $user_id) );
} else {
echo get_avatar($id_or_email, $size='64', $default, $alt='User name' );
}
?>
Use the BuddyPress function bp_core_fetch_avatar
$user_id = bp_loggedin_user_id();
echo bp_core_fetch_avatar( array( 'item_id' => $user_id) );
Review the function in bp-core/bp-core-avatars.php to see the available arguments.
I'm trying to change the link of the user profile and it seems that I'm too noob.
The current structure of the link is: domain.com/profile/username.
What I want is to be is like: domain.com/username/city where city is taken from wp_postmeta table
I tried something using this function:
add_action('init', 'wpse82004_init');
function wpse82004_init()
{
global $wp_rewrite;
$city = get_user_meta( get_current_user_id(), 'city', TRUE );
$wp_rewrite->author_base = $city;
$wp_rewrite->author_structure = '/%author%' . '/' . $wp_rewrite->author_base;
}
The problem is that is returns the city of the current logged user on all profiles I click.
Any help would be appreciated. Thanks
untested code
add_action('init', 'wpse82004_init');
function wpse82004_init()
{
global $wp_rewrite;
//parse username from url
$url = $_SERVER["REQUEST_URI"];
$username = preg_replace('/^.*profile\/(.*)$/i', '$1', $url);
//get user by username
$user = get_user_by('slug', $username);
//rewrite the city value of anticipated user other than current user
$city = get_user_meta( $user->ID, 'city', TRUE );
$wp_rewrite->author_base = $city;
$wp_rewrite->author_structure = '/%author%' . '/' . $wp_rewrite->author_base;
}
I've been trying for days now to take users who have just registered to my WordPress site and automatically log them in and then redirect them to a URL of my choice.
By default, WordPress sends you a username and a password, then you must log in manually. This is a total pain. How can i overcome this.
I have my own registration page(core php page) which successfully adds users into DB. But the point is, i should avoid users to login again.
Once registration is done, it should automatically redirects to home page or profile page.
I am a newbie to wordpress functionalities. It would be grateful if someone(who have knowledge on core functionality of wordpress) at least suggests a way/solution.
Looking forward.
Thanks
// Add on function.php for Auto login after register and redirect to a home page. varified this code
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
$user = get_user_by( 'id', $user_id );
do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
wp_redirect( home_url() ); // You can change home_url() to the specific URL,such as "wp_redirect( 'http://www.wpcoke.com' )";
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Following is based on how WooCommerce creates a new user and logs him in:
$user_pass = esc_attr( $_POST['account_password'] );
$new_user_data = array(
'user_login' => $_POST['account_username'],
'user_pass' => $user_pass,
'user_email' => $_POST['account_email'],
'role' => 'subscriber'
);
$user_id = wp_insert_user( $new_user_data );
// Set the global user object
$current_user = get_user_by( 'id', $user_id );
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user_id, true, $secure_cookie );
to redirect use wp_safe_redirect, e.g.
wp_safe_redirect( home_url( '/' ) );
exit;
Instead of touching core files... You can use this
$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie;
$auth_secure_cookie = $secure_cookie;
wp_set_auth_cookie($user_id, true, $secure_cookie);
$user_info = get_userdata($user_id);
do_action('wp_login', $user_info->user_login, $user_info);
The function wp_create_user returns the just created user_id which you can use to create a cookie and log the user in. If you wish, you can redirect the logged in user to the profile or home page.
Thanks for your support guys..i did on my own with the following code..thanks for your time and support :)
<i>$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'"));
$username=$getdetails['user_login'];
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ){
echo $user->get_error_message();
}else{
wp_redirect( home_url() );
}