Update specific data when someone edit his profile - php

When user update his email then update a specific row table.
I know how to take the new value of email, but I dont know how to use it in other row in different table.
table name: BIG
row name: secondName
I have this
function wpdocs_check_user_email_updated( $user_id, $old_user_data ) {
$old_user_email = $old_user_data->data->user_email;
$user = get_userdata( $user_id );
$new_user_email = $user->user_email;
if ( $new_user_email !== $old_user_email ) {
// I dont know how to do it
}
}
add_action( 'profile_update', 'wpdocs_check_user_email_updated', 10, 2 );

You can use WP predefine wpdb::update check the below code.
function wpdocs_check_user_email_updated( $user_id, $old_user_data ) {
global $wpdb;
$old_user_email = $old_user_data->data->user_email;
$user = get_userdata( $user_id );
$new_user_email = $user->user_email;
if ( $new_user_email !== $old_user_email ) {
$data = array( 'your-column-name' => $new_user_email );
$where = array( 'your-column-name' => $user_id );
$wpdb->update( $wpdb->prefix . 'your_table', $data, $where );
}
}
add_action( 'profile_update', 'wpdocs_check_user_email_updated', 10, 2 );
USEFUL LINKS
wpdb::update

Related

Adding newly registered users to different leardash groups depending on different wordpress registration page

I have two WordPress registration pages eg https://example.com/adult-register-url and https://example.com/youth-register-url. I have created 2 learndash groups with different courses one group is the adult group and the other group is the youth group. I am now trying to automatically add users that register with the adult-register-url to the adult group and the youth-register-url to the youth group. However, on registration users are not being added to the correct group. Below is the code that I added to the functions.php
add_action( 'user_register', function ( $user_id = 0 ){
global $post;
$current_slug = $post->name;
if(isset($current_slug) and $current_slug =='youth-register-url'){
if ( !empty( $user_id ) ) {
$LD_GROUP_IDS = array( 5822 );
learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
$courses = array(2399,2392,2387,2039);
foreach($courses as $course){
ld_update_course_access($user_id, $course, $remove=true);
}
}
}else{
if ( !empty( $user_id ) ) {
$LD_GROUP_IDS = array( 5410 );
learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
$courses = array(2399,2392,2387,2039);
foreach($courses as $course){
ld_update_course_access($user_id, $course, $remove=true);
}
}}
});
Used the $_REQUEST variable to get the meta data using that I used the referrer key to load courses
add_action( 'user_register', function ( $user_id = 0 ){
if(isset($_REQUEST['referrer']) and $_REQUEST['referrer'] =='https://example.com/youth-register-url/'){
if ( !empty( $user_id ) ) {
$LD_GROUP_IDS = array( 5822 );
learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
$courses = array(2399,2392,2387,2039);
foreach($courses as $course){
ld_update_course_access($user_id, $course, $remove=true);
}
}
}else{
if ( !empty( $user_id ) ) {
$LD_GROUP_IDS = array( 5410 );
learndash_set_users_group_ids( $user_id, $LD_GROUP_IDS );
$courses = array(2399,2392,2387,2039);
foreach($courses as $course){
ld_update_course_access($user_id, $course, $remove=true);
}
}}
});

Set Wordpress users' default display name to 'firstname + lastname' through functions.php

I'm trying to find a solutions for displaying my users' display name in this format: "firstname lastname". By default, wordpress displays only the firstname for all of my users.
I've tried several codes found on internet but most of them are depreciated and no longer functional due to latest wordpress updates.
function change_display_name( $user_id ) {
$info = get_userdata( $user_id );
$args = array(
‘ID’ => $user_id,
‘display_name’ => $info->first_name . ‘ ‘ . $info->last_name
);
wp_update_user( $args );
}
add_action(‘user_register’,’change_display_name’);
Just change the get_userdata() function to get_user_meta()
function change_display_name( $user_id ) {
$key_first = 'first_name';
$key_last = 'last_name';
$single = true;
$user_first = get_user_meta( $user_id, $key_first, $single );
$user_last = get_user_meta( $user_id, $key_last, $single );
$args = array(
‘ID’ => $user_id,
‘display_name’ => $user_first . ‘ ‘ . $user_last
);
wp_update_user( $args );
}
add_action(‘user_register’,’change_display_name’)
I solved the issue above with the following code:
$current_user = wp_get_current_user();
$howdy = sprintf( __( 'Darling %s' ), '<span class="display-name">' .$current_user->first_name. ''. $current_user->last_name . '</span>' );
echo $howdy;

Wordpress - Send email to users when they get new followers

I'm using a simple plugin "user following system" And all what i need to do is sending an email after the users get a new followers.
I think this is the important part of the plugin code:
function pwuf_follow_user( $user_id, $user_to_follow ) {
$following = pwuf_get_following( $user_id );
if ( $following && is_array( $following ) ) {
$following[] = $user_to_follow;
} else {
$following = array();
$following[] = $user_to_follow;
}
// retrieve the IDs of all users who are following $user_to_follow
$followers = pwuf_get_followers( $user_to_follow );
if ( $followers && is_array( $followers ) ) {
$followers[] = $user_id;
} else {
$followers = array();
$followers[] = $user_id;
}
do_action( 'pwuf_pre_follow_user', $user_id, $user_to_follow );
// update the IDs that this user is following
$followed = update_user_meta( $user_id, '_pwuf_following', $following );
// update the IDs that follow $user_id
$followers = update_user_meta( $user_to_follow, '_pwuf_followers', $followers );
// increase the followers count
$followed_count = pwuf_increase_followed_by_count( $user_to_follow ) ;
if ( $followed ) {
do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );
return true;
}
return false;
}
and here to check if a user is following another:
function pwuf_is_following( $user_id, $followed_user ) {
$following = pwuf_get_following( $user_id );
$ret = false; // is not following by default
if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
$ret = true; // is following
}
return $ret;
}
I tried to add this code after updating the user meta but nothing happen!
$subscribers = explode(",", $user_to_follow );
$emails = array ();
foreach ( $subscribers as $subscriber ) {
$user_info = get_userdata($subscriber);
$emails[] = $user_info ->user_email;
}
$body = sprintf( $user_to_follow->display_name, 'followed your work! See <%s>' );
wp_mail( $emails, 'New followers!', $body );
I believe you want to send user an email when ever user is been followed by another user. Given this scenario, there is this action hook available in the plugin which is executed when following process is successful:
do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );
You can hook your own code to this action
add_action('pwuf_post_follow_user', $user_id, $user_to_follow) {
$follower = get_userdata($user_id);
$recipient = get_userdata($user_to_follow);
$recipient_email = $recipient->user_email;
$body = sprintf('%s followed your work!', $follower->display_name );
wp_mail( $recipient_email , 'New follower!', $body );
}
Refernece: https://codex.wordpress.org/Function_Reference/get_userdata

WordPress, retrieving the ID of the user profile page currently being edited

I'm in the process of developing a WordPress plugin. When editing a user's profile page, I need to be able to retrieve the ID of that user (not the currently logged in user).
I'm using the Advanced Custom fields plugin, and using a load filter for the function in question.
add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents' );
function funbotic_load_parents( $field ) {
$args = array(
'role' => 'customer',
'orderby' => 'display_name',
'order' => 'ASC',
);
$parent_data_array = get_users( $args );
// Clear choices array in case it was previously set.
$field['choices'] = array();
foreach ( $parent_data_array as $parent ) {
$parent_ID = $parent->ID;
$parent_display_name = $parent->display_name;
$field['choices'][$parent_ID] = $parent_display_name;
}
$current_user = (int) $_GET['user_id'];
$previously_associated_parents = get_user_meta( $current_user_id, 'funbotic_parents' );
if ( empty( $previously_associated_parents ) || is_null( $previously_associated_parents ) ) {
update_user_meta( $current_user_id, 'funbotic_previously_associated_parents', $previously_associated_parents );
} else {
$new_meta = funbotic_clean_array( $previously_associated_parents );
update_user_meta( $current_user_id, 'funbotic_previously_associated_parents', $new_meta );
}
return $field;
}
The line $current_user = (int) $_GET['user_id']; is returning null. Is there a way to pull 'user_id' from the URL? It's not an ideal solution but at this point I'm starting to get desperate. The URL looks like this:
http://dev-funbotic.local/wp-admin/user-edit.php?user_id=115&wp_http_referer=%2Fwp-admin%2Fusers.php%3Fs%3Dtest%26action%3D-1%26new_role%26course_id%26group_id%26paged%3D1%26action2%3D-1%26new_role2
You can try getting the current url and breaking it down to query params like this:
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
if(!empty($query[0])){
foreach( $query as $param ){
list($name, $value) = explode('=', $param, 2);
$params[urldecode($name)][] = urldecode($value);
}
}
then you can access the user_id by $params['user_id'][0]

Insert data into multiple tables from custom user profile field in wordpress

I have created a custom profile field in wp admin and its saving data to usermeta table. But I need this data also saved in another table 'wp_ppv_performer_profile'. This field is a dropdown field in admin.
My code:
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
global $wpdb;
$phs = $_POST['hstatus'];
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, 'hstatus', $_POST['hstatus'] );
$wpdb->insert( $wpdb->wp_ppv_performer_profile, array("performer_tags" =>
$phs ), array( "performer_id", 5));
}
This code is not giving errors but its not saving 'hstatus' value in table column 'performer_tags' for performer_id = 5.
This is a working example, make sure the table name is correct:
add_action('edit_user_profile_update', 'my_save_extra_profile_fields');
function my_save_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) ){
global $wpdb;
$status = $_POST['hstatus'];
update_user_meta($user_id, 'hstatus', $status);
// change to performer_profile if ppv_ is part of the prefix, only use the table name without prefix.
$table = $wpdb->prefix . "ppv_performer_profile";
// performer_tags and performer_id are assumed to be table columns, status is also assumed to be a string here.
$data = array( 'performer_tags' => $status, 'performer_id' => 5);
$format = array( '%s', '%d');
$wpdb->insert( $table, $data, $format );
}
}
Following code is tested and working fine for me. Please make sure table name and field names are correct. And if still not working then please debug and let us know exact error which you find.
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields($user_id) {
if ( current_user_can('edit_user',$user_id) ){
global $wpdb;
$status = $_POST['hstatus'];
update_user_meta($user_id, 'hstatus', $status);
$table_name= $wpdb->prefix . "ppv_performer_profile"; // Tablename is wp_ppv_performer_profile , I use $wpdb->prefix which will take defined prefix of wordpress (wp_).
$insertdata= array( 'performer_tags' => $status, 'performer_id' => 5);
$wpdb->insert( $table_name, $insertdata);
}
}

Categories