WordPress - set display name from nickname with register process - wordpress

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

Related

Cancel all user orders on subscription cancelled/expired

I want to develop a setup/code in which all the orders made by the user gets cancelled when the his subscription gets cancelled or expired. I am using Wordpress, Woocommerce and Woocommerce Subscriptions plugin.
I am trying to do by using something like this:
function my_cancelled_subscription( $user_id, $subscription_key ) {
//what code should I use here????
}
add_action( 'woocommerce_subscription_status_cancelled', 'my_cancelled_subscription', 10, 2 );
Please help mee.....
I figured it out..........
function my_cancelled_subscription( $subscription ) {
$order_id = $subscription->get_parent_id();
$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();
$order->update_status( 'cancelled' );
$args = array(
'customer_id' => $user_id,
'status' => 'wc-completed',
);
$arrorders = wc_get_orders( $args );
foreach ($arrorders as $ord) {
$ord->update_status( 'cancelled' );
}
}
add_action( 'woocommerce_subscription_status_cancelled', 'my_cancelled_subscription' );

WordPress add_user_meta / update_user_meta not working (resolved)

I'm trying to add the _capabilities and _level meta data to existing users in a multisite setup so they also have access to the new section, our forum.
The forum is the 3rd site on the network and I want to grant everyone the basic access rights as Contributor. Some users I already added manually to the usermeta table so I check in the script if the user meta_key already exists or not. And to ensure the script doesn't mess with the admin accounts I only want to run the script for user IDs above 3.
When I try below code, I use it in a temp plugin which I will only run once, then I just get a whole bunch of WordPress error messages thrown at me which I somehow can't really decipher to get it working :(
The issue is somehow with the add_ / update_user_meta functions... What am I overlooking?
$prefix = 'tbl_prefix_3_';
$cap = [ "contributor" => true ];
$lvl = 1;
$users = get_users( array( 'fields' => array( 'ID' ) ) );
asort( $users );
foreach( $users as $user ) {
$user_id = intval( $user->ID );
if( $user_id > 3 ) {
$check = get_user_meta( $user_id, $prefix . 'capabilities', true );
if( empty( $check ) ) {
add_user_meta( $user_id, $prefix . 'capabilities', $cap );
} else {
update_user_meta( $user_id, $prefix . 'capabilities', $cap );
}
$check = get_user_meta( $user_id, $prefix . 'user_level', true );
if( empty( $check ) ) {
add_user_meta( $user_id, $prefix . 'user_level', $lvl );
} else {
update_user_meta( $user_id, $prefix . 'user_level', $lvl );
}
}
}
The errors:
Fatal error: Uncaught Error: Call to undefined function get_user_by()
in /home/.../wp-includes/meta.php:1652 Stack trace:
#0 /home/.../wp-includes/meta.php(176): get_object_subtype('user', 4)
#1 /home/.../wp-includes/user.php(1043): update_metadata('user', 4, 'tbl_prefix_capab...', Array, '')
#2 /home/.../wp-content/plugins/assign_rights_to_users.php(33): update_user_meta(4, 'tbl_prefix_capab...', Array)
#3 /home/.../wp-settings.php(391): include_once('/home/...') #4 /home/.../wp-config.php(253): require_once('/home/...')
#5 /home/.../wp-load.php(37): require_once('/home/...')
#6 /home/.../wp-admin/admin.php(34): require_once('/home/...') #7 /ho in /home/.../wp-includes/meta.php on line 1652
Notice: is_embed was called incorrectly. Conditional query tags do not
work before the query is run. Before then, they always return false.
Please see Debugging in WordPress for more information. (This message
was added in version 3.1.0.) in /home/.../wp-includes/functions.php on
line 5313
Notice: is_search was called incorrectly. Conditional query tags do
not work before the query is run. Before then, they always return
false. Please see Debugging in WordPress for more information. (This
message was added in version 3.1.0.) in
/home/.../wp-includes/functions.php on line 5313
There has been a critical error on this website. Please check your
site admin email inbox for instructions.
I figured it out, I had forgotten to include it in a hook so the WordPress stuff was not properly available in the plugin :|
I now placed it into an admin_init hook so I activate the plugin, let it do it's magic, and than deactivate and remove the plugin again. I also simplified the whole thing, so only using the update_user_meta now.
Anyway, this is the working result:
<?php
/**
* Plugin Name: 000 FIX MISSING USER RIGHTS
* Description: Activate, deactivate, check the results, remove the plugin!
* Version: 1.0
* Author: golabs
* #source: https://stackoverflow.com/a/66877257/3592696
*/
function fix_missing_user_rights() {
$prefix = 'wp_prefix_';
/**
* Use one of the 2 methods:
* - manual array with user ids
* OR
* - collect them from the database
* Just comment out the method you don't want to use.
*/
# $users = [ 1,2,3,4,5,6,7,8,9,10 ];
$users = get_users( array( 'fields' => 'ID' ) ); asort( $users );
foreach( $users as $user ){
update_user_meta( $user, $prefix.'capabilities', [ 'subscriber' => true ] );
update_user_meta( $user, $prefix.'user_level', 0 );
update_user_meta( $user, $prefix.'2_capabilities', [ 'customer' => true ] );
update_user_meta( $user, $prefix.'2_user_level', 8 );
update_user_meta( $user, $prefix.'3_capabilities', [ 'contributor' => true ] );
update_user_meta( $user, $prefix.'3_user_level', 1 );
}
}
add_action( 'admin_init', 'fix_missing_user_rights' );

WordPress additonal check while login

When a user register in my site I have set the user_activation_key column value from wp_users table like that:
$code = sha1( $user_id . time() );
global $wpdb;
$wpdb->update(
$wpdb->prefix.'users', //table name
array( 'user_activation_key' => $code ),
array( 'ID' => $user_id ),
array( '%s' ),
array( '%d' )
);
It's because I want to make activation system by sending an email with clickable link:
$activation_link = add_query_arg(
array(
'key' => $code,
'user' => $user_id
), get_permalink( 44 )
);
$message = "<div style='padding : 20px; border : 1px solid #ddd; color : #000;'>Hello $surname, <br/><br/>Please confirm your email addresss . Click this link to confirm : <a href='$activation_link'>Confirm Now</a><br/><br/></div>";
$to = $email;
$subject = 'Confirm your registration process"';
$body = $message;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Now, the column user_activation_key has the hash code and user_status column value is 0
Now the actual question:
When user go to www.site.com/wp-admin that means login page I want to show an error message if the user_status column value is 0.
I don't' have any idea which hook or how can I cehck this while user login?
Use the admin_notices hook to display messages in the backend, the code below is similar to what your after:
add_action('admin_notices', 'account_activation_check');
function account_activation_check() {
global $wpdb;
// setup vars //
$currentID = get_current_user_id();
$user = get_user_by( 'ID', $currentID );
$userStatus = $user->user_status;
// check if user status is 0 //
if($userStatus == 0) {
echo '<div class="error"><p>Your email has not been verified!</p></div>';
}
}
I have solved the issues by using the wp_authenticate_user hook. Here is the code:
add_filter( 'wp_authenticate_user', 'shibbir_authenticate_user', 10, 2 );
function shibbir_authenticate_user( $user ) {
if ( $user->data->user_status == 0 ) {
return new WP_Error( 'error', __( 'Your account is not activate, Please contact site admininstrator.' , 'shibbir' ) );
}
return $user;
}

Updating GEO my WP fields using frontend Advanced custom fields form - WordPress

I’ve created a frontend form using the advanced custom fields (ACF) plugin. Within this form I’ve added some location fields such as postcode and city.
Below is the function used to update the post
global $post_id;
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
function tsm_do_pre_save_post( $post_id ) {
// Bail if not logged in or not able to post
if ( ! ( is_user_logged_in() ) ) {
return;
}
// check if this is to be a new post
if( $post_id != 'new_job' ) {
return $post_id;
}
$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;
$user_id = get_current_user_id();
// Create a new post
$post = array(
'ID'=> $post_id,
'post_type' => 'members',
'post_status' => 'publish',
'post_title' => $userID,
'post_author' => $user_id,
'category' => $_POST['acf']['field_594d0ffc2a66d'],
);
// insert the post
$post_id = wp_insert_post( $post );
// Save the fields to the post
do_action( 'acf/save_post' , $post_id );
return $post_id;
exit;
}
Once this form has been submitted, the custom post type is updated with no issues.
How can I add the submitted location data (postcode, city etc) into the GEO MY WP information for the post?
I’ve tried to follow the information in the docs secetion (http://docs.geomywp.com/gmw_pt_update_location/) but not having much luck.
GEO my WP function fron docs below:
function gmw_update_post_type_post_location( $post_id ) {
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;
// check autosave //
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
//check if user can edit post
if ( !current_user_can( 'edit_post', $post_id ) )
return;
//get the address from the custom field "address"
$address = get_post_meta( $post_id, 'address', true );
//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;
//include the update location file file
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
//make sure the file included and the function exists
if ( !function_exists( 'gmw_pt_update_location' ) )
return;
//Create the array that will pass to the function
$args = array(
'post_id' => $post_id, //Post Id of the post
'address' => $address // the address we pull from the custom field above
);
//run the udpate location function
gmw_pt_update_location( $args );
}
//execute the function whenever post type is being updated
add_action( 'save_post_post', 'gmw_update_post_type_post_location' );
I believe I need to somehow combine the two functions so when the ACF form is updated, it also needs to update the GEO my wp meta data also.
I've tried combining the two functions but not having much luck.
If someone can point me in the right direction it will be very much appreciated
Thanks
Updated code.....
I've tried to add the gmw_update_post_type_post_location() function within the ACF function (code below) but still not having much luck...
global $post_id;
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
function tsm_do_pre_save_post( $post_id ) {
// Bail if not logged in or not able to post
if ( ! ( is_user_logged_in() ) ) {
return;
}
// check if this is to be a new post
if( $post_id != 'new_job' ) {
return $post_id;
}
$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;
$user_id = get_current_user_id();
$profilePostcode = get_post_meta( $post_id, 'location_postcode', true );
// Create a new post
$post = array(
'ID'=> $post_id,
'post_type' => 'members', // Your post type ( post, page, custom post type )
'post_status' => 'publish', // You can use -> publish, draft, private, etc.
'post_title' => $userID, // Post Title ACF field key
'post_author' => $user_id,
'category' => $_POST['acf']['field_594d0ffc2a66d'], // Post Content ACF field key
);
// insert the post
$post_id = wp_insert_post( $post );
// Save the fields to the post
do_action( 'acf/save_post' , $post_id );
gmw_update_post_type_members_location($post_id);
return $post_id;
exit;
}
function gmw_update_post_type_members_location( $post_id ) {
$profilePostcode = get_post_meta( $post_id, 'profile_location_postcode', true );
if ( empty( $profilePostcode ) )
return;
$address = array(
//'street' => $_POST['location_address'],
//'city' => $_POST['location_town'],
//'state' => $_POST['location_state'],
'zipcode' => $profilePostcode,
//'country' => $_POST['location_country']
);
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
if ( !function_exists( 'gmw_pt_update_location' ) )
return;
$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;
$user_id = get_current_user_id();
$args = array(
'post_id' => $post_id, //Post Id of the post
'post_title' => $userID, // Post Title ACF field key
'post_author' => $user_id,
'post_type' => 'members',
'address' => $address
);
//run the udpate location function
gmw_pt_update_location( $args );
}

Gravity Form save to Database usermeta table

I have created the following wordpress function to save a form created in Gravity Forms to the usermeta database based on one I had working for CF7 but it isn't working, hopefully someone can see where I've made a mistake. It needs to update the current users fields.
add_action('gform_after_submission', 'input_fields', 10, 2);
function input_fields($entry, $form){
$name = $entry['1'];
$email = $entry['4'];
global $wpdb, $current_user;
$wpdb->insert(
'usermeta',
array(
'description' => $email,
'former_name' => $name
)
);
}
I've seen other examples which are pretty much identical so i'm a bit stuck.
This should do the trick:
add_action( 'gform_after_submission', 'input_fields', 10, 2 );
function input_fields( $entry, $form ) {
$name = $entry[1];
$email = $entry[4];
update_user_meta( get_current_user_id(), 'description', $email );
update_user_meta( get_current_user_id(), 'former_name', $name )
}
Alternately, I'd recommend upgrading to a Developer license and getting access to the User Registration add-on which would do this even more easily. :)
Semicolon sign ";" is missing at line 8 so this is right the answer. :)
add_action( 'gform_after_submission', 'input_fields', 10, 2 );
function input_fields( $entry, $form ) {
$name = $entry[1];
$email = $entry[4];
update_user_meta( get_current_user_id(), 'description', $email );
update_user_meta( get_current_user_id(), 'former_name', $name );
}

Resources