Update ACF field based on user's email domain - wordpress

I need some help and I can't seem to figure it out since I'm not exactly sure how to do this.
I have a custom post type called Companies. In the admin user interface, I have created an ACF relationship field to the Companies post type called user_company. Right now I am manually selecting the Companies post based on the user's email address and I would like to do this programatically.
`
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$company = get_field('user_company');
list($user, $domain) = explode('#', $args['user_email'] );
if ($domain == 'gmail.com') {
$company[] = 'Google';
update_field('user_company', $company, $post_title);
};
if ($domain == 'amyling.com') {
$company[] = 'Pearlsin Arts';
update_field('user_company', $company, $post_title);
};
}
add_action( 'user_register', 'update_user_company', 10, 2 );
`

I think your function is correct and the only thing that seems wrong is that you try to update a custom field based on a post title and you should use the post ID or in your case the user ID instead.
So you should try this:
function update_user_company( $user_id, $args ) {
$user = get_user_by( 'ID', $user_id );
$email = $user->user_email;
$company = get_field('user_company');
list($user, $domain) = explode('#', $args['user_email'] );
if ($domain == 'gmail.com') {
$company[] = 'Google';
update_field('user_company', $company, $user_id);
};
if ($domain == 'amyling.com') {
$company[] = 'Pearlsin Arts';
update_field('user_company', $company, $user_id);
};
}
add_action( 'user_register', 'update_user_company', 10, 2 );
Hope this helps

Related

Auto redirection in WooCommerce after login based on user meta data

I am trying to check if a user meta data is empty or not. If empty, redirect the user to a page, else redirect to the default page.
But my following codes is only redirecting to the default page.
add_filter('woocommerce_login_redirect', 'ac_my_acct_login_redirect');
function ac_my_acct_login_redirect($redirect_to) {
$user_id = get_current_user_id();
$father = get_user_meta( $user_id, 'fath_name', true );
$update_pro = esc_url(get_permalink('123')); // the page I want for redirection if metadata is empty
$my_acct = esc_url(get_permalink( wc_get_page_id( 'myaccount' ) )); // default woocommerce my account page
if(empty($father)){
$redirect_to = $update_pro;
}
else {
$redirect_to = $my_acct;
}
return $redirect_to;
}
meta key = fath_name (even it has value, still the redirection is not working as intended). Any advice?
Your code contains some mistakes
get_current_user_id() is not necessary, as $user is passed to the callback function
get_permalink() expects an int (123), not a string ("123")
Make sure the page ID actually exists
So you get:
function filter_woocommerce_login_redirect( $redirect, $user ) {
// Get user meta
$value = get_user_meta( $user->ID, 'fath_name', true );
// Empty
if ( empty( $value ) ) {
$redirect = get_permalink( 123 );
} else {
// Get the "My account" url
$redirect = get_permalink( wc_get_page_id( 'myaccount' ) );
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'filter_woocommerce_login_redirect', 10, 2 );

Redirect user based on custom metadata, wordpress/woocommerce

I have added some custom metadata to wordpress per user and would like to redirect each user after login based on this custom metadata.
For instance i have a page called foo and another called bar. User one has a custom key (acCustomerName) in the metadata stating foo. And the other user has bar in his.
Depending on whats inside that field I would like to redirect the first user to http://example.tld/foo and the other to http://example.tld/bar.
I am able to retrieve this information perfectly fine and echo it out directly to test the function but are not able to retrieve that information inside the sencond function named forward_user and would like to know why.
Right now the user is redirected only to http://example.tld only. So it seems like the function does not get the $kundtyp string.
function kundtyp($Uuser) {
$key = 'acCustomerName';
$single = true;
return get_user_meta( $Uuser, $key, $single );
}
function forward_user( $redirect, $user ) {
$kundtyp = kundtyp($user);
$redirect_page_id = url_to_postid( $redirect );
$checkout_page_id = wc_get_page_id( 'checkout' );
if( $redirect_page_id == $checkout_page_id ) {
return $redirect;
}
return site_url( kundtyp( get_current_user_id() ) );
}
add_filter( 'woocommerce_login_redirect', 'forward_user', 1100, 2 );
Here is the updated code, you have to pass the ID of the user object to get_user_meta data function. You were passing the WP_User object to your function. I don't think you can use function get_current_user_id() because the global wp_user object has not been set yet. I checked it and it always returns 0 as the value. This would explain why they provide the $user variable in the filter.
function kundtyp($Uuser) {
$key = 'acCustomerName';
$single = true;
return get_user_meta( $Uuser->ID, $key, $single );
}
function forward_user( $redirect, $user ) {
$kundtyp = kundtyp($user);
$redirect_page_id = url_to_postid( $redirect );
$checkout_page_id = wc_get_page_id( 'checkout' );
if( $redirect_page_id == $checkout_page_id ) {
return $redirect;
}
return site_url( kundtyp( $user ) );
}
add_filter( 'woocommerce_login_redirect', 'forward_user', 1100, 2 );

Add fields in custom taxonomy in WordPress admin for saved data

I want to add custom fields in custom taxonomy for my plugin in form-fields and also show saved data as like name, slug and description are showed.
Someone suggest something!
function custom_column_header( $columns ){
$columns['address'] = 'Address';
$columns['phoneno'] = 'Phone No';
return $columns;
}
add_filter( "manage_edit-nwcm_news_category_columns", 'custom_column_header', 10);
// To show the column value
function custom_column_content( $value, $column_name , $term){
if ($column_name === 'address') {
$t_id = $term;
$term_meta = get_option( "$t_id" );
print_r($term_meta['address']);
}
if ($column_name === 'phoneno') {
$t_id = $term;
$term_meta = get_option( "$t_id" );
print_r($term_meta['phone']);
}
}
add_action( "manage_nwcm_news_category_custom_column", 'custom_column_content', 10, 3);

WordPress - set display name from nickname with register process

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

How is wordpress user validation works?

I have created a custom registration form in the front end using wp_insert_user() function:
$data = array();
$data['user_login'] = $_POST['username'];
$data['user_email'] = $_POST['email'];
$data['user_pass'] = wp_generate_password ( 12, false );
$data['role'] = 'pending';
$user = wp_insert_user( $data );
if ( is_wp_error($user) ){
$error = $user->get_error_message();
echo json_encode( array( 'loggedin' => false, 'info' => $error ) );
} else {
wp_new_user_notification( $user, $data['user_pass'] );
}
But I want the user to receive an activation email with activation link. How can I do this?
If you look at the WordPress codex for the wp_new_user_notification method (http://codex.wordpress.org/Function_Reference/wp_new_user_notification#Examples), there is an example of how to redefine this function. In that example, functionality is added to send an email to the created user.

Resources