I couldn't seem to make this code to work. I'm trying to change the notification email on Gravity Forms/Wordpress by dynamically retrieving an author's email address.
The code below works if I hard-code an author's email address to the '$notification['to']'.
I'm just not sure what I must be doing wrong.
Appreciate the help. Thanks.
add_filter( 'gform_notification_19', 'change_notification_email', 10, 3 );
function change_notification_email( $user_email, $notification, $form, $entry ) {
//There is no concept of admin notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'Applicant' ) {
global $post;
$author_id=$post->post_author;
$user_email = get_the_author_meta( 'user_email' , $author_id );
// toType can be routing or email
$notification['toType'] = 'email';
$notification['to'] = '$user_email';
}
return $notification;
}
According to the codex this should work. Try also to print the variables to see if they are getting the proper values. Also, is your filter correct?
add_filter( 'gform_notification_19', 'change_notification_email', 10, 3 );
function change_notification_email( $user_email, $notification, $form, $entry ) {
//There is no concept of admin notifications anymore, so we will need to target notifications based on other criteria, such as name
if ( $notification['name'] == 'Applicant' ) {
global $post;
$author_id=$post->post_author;
$user_email = get_the_author_meta( 'user_email' , $author_id );
// toType can be routing or email
$notification['toType'] = 'email';
$notification['to'] = $user_email;
}
return $notification;
}
Related
I need update WordPress account email with woocommerce billing email after successful checkouts. I used this code but it does not work :
/* Update account email based on woocommerce billing email */
add_filter( 'woocommerce_thankyou' , 'custom_update_checkout_fields', 10, 2 );
function custom_update_checkout_fields($user_id, $old_user_data ) {
$current_user = wp_get_current_user();
// Updating Billing info
if($current_user->user_email != $current_user->billing_email)
update_user_meta($user_id, 'billing_email', $current_user->user_email);
}
Am I used an outdated code?
There are some mistakes. Try the following instead:
add_filter( 'woocommerce_thankyou' , 'thankyou_update_wordpress_user_email' );
function thankyou_update_wordpress_user_email( $order_id ) {
$order = wc_get_order( $order_id );
$user = $order->get_user();
$billing_email = $order->get_billing_email();
// Updating user account email
if( is_a($user, 'WP_User' ) && $user->user_email != $billing_email ) {
$user_data = wp_update_user( array( 'ID' => $user->ID, 'user_email' => $billing_email ) );
}
}
Code goes in functions.php file of the active child theme (or active theme). It should work.
Note: When changing user email, WordPress Send an email to the new user email.
I am using Gravityforms along with User registration add-on and have a form which when submitted should change the role of the current user to a new role without any underlying conditions.
Using the gravity\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role and trying this:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // update 'role' to the name of the desired role
}
But its not working! Does anyone have any idea why this is incorrect or any other modifications to the code?
There's a couple things I see with your code when comparing it to the Gravity Forms doc.
Here's your code with some added comments:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user; // you probably don't need this
get_currentuserinfo(); // you probably don't need this
$user_id = $current_user->ID; // $user_id should already be a numeric value passed in to the function containing the logged in user's ID so you shouldn't need to do this. You're resetting the $user_id variable here to whatever is being pulled out of the get_currentuserinfo() function, and I'm guessing that's the problem
//I would get rid of this echo and if statement
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // the word "role" here needs to be the role name
}
I think you can simplify it some. Try this instead:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
$user = new WP_User( $user_id );
$user->set_role( 'new_role_name_here' ); // Add an existing role here to update the user too
}
I want to pre-populate the values for the checkout's billing fields to the DB stored values of the user before his first purchase.
I've tried the following code:
add_filter( 'woocommerce_checkout_fields' , function ( $fields ) {
$fields['billing']['billing_first_name']['placeholder'] = 'First Name';
$fields['billing']['billing_first_name']['default'] = wp_get_current_user()->user_firstname;
return $fields;
});
I've read about this solution in an other post. Placeholder works great, but the value doesn't.
Also, the WooCommerce Doc (Lesson 1) doesn't say about anything the array value 'default'
You're pretty close. This worked for me. I don't know if it was necessary, but I used a named function and only get the user_firstname property if the user exists.
add_filter( 'woocommerce_checkout_fields' , 'kia_checkout_field_defaults', 20 );
function kia_checkout_field_defaults( $fields ) {
$user = wp_get_current_user();
$first_name = $user ? $user->user_firstname : '';
$fields['billing']['billing_first_name']['placeholder'] = 'First Name';
$fields['billing']['billing_first_name']['default'] = $first_name;
return $fields;
}
WooCommerce under Settings/Account has an option named
Automatically generate username from customer email
but the username it generates is not the full email address.
my.email#example.com
becomes myemail as a username.
What method would I need to hook into to override the user generation so that the
full email is set as the username?
You can use something like this in your functions.php. The woocommerce_new_customer_username filter could be used to change the generated username. If you just return the customer email as the username, it will be used instead of the generated one (in your case the name before #).
function my_new_customer_username( $username, $email, $new_user_args, $suffix ) {
return $email;
}
add_filter( 'woocommerce_new_customer_username', 'my_new_customer_username', 10, 4 );
Simply use the hook pre_user_login
add_filter( 'pre_user_login' , 'wpso_same_user_email' );
function wpso_same_user_email( $user_login ) {
if( isset($_POST['billing_email'] ) ) {
$user_login = $_POST['billing_email'];
}
if( isset($_POST['email'] ) ) {
$user_login = $_POST['email'];
}
return $user_login;
}
Sorry for reviving an old thread, but it seems this is the best option (goes in your child functions.php or custom plugin)
add_filter( 'woocommerce_new_customer_data', function( $data ) {
$data['user_login'] = $data['user_email'];
return $data;
} );
here are 2 source references: 1 and 2
I'm creating plugin which will register all new users as role = unverified and stop user from login until email verification. I want to send url on their email which will change the current unverified role to author. I know how to send email etc all of that in WordPress but can't figure out how to create url which will change the role. I'm using custom ajax form to login,register and lostpassword because of it i'm unable to use pie register and register-plus-redux plugins.
Current code
function add_roles_on_plugin_activation() {
add_role( 'custom_role', 'Unverified', array( 'read' => true, 'level_0' => true ) );
}
register_activation_hook( __FILE__, 'add_roles_on_plugin_activation' );
function remove_roles_on_plugin_deactivation() {
remove_role( 'custom_role' );
}
register_deactivation_hook( __FILE__, 'remove_roles_on_plugin_deactivation' );
add_filter('pre_option_default_role', function($default_role){
return 'custom_role';
return $default_role;
});
function error_email_verify() {
$user = wp_get_current_user();
if ( in_array( 'custom_role', (array) $user->roles ) ) {
$logout_url = wp_login_url().'?mode=emailverify';
wp_logout();
wp_redirect( $logout_url );
exit();
}
}
add_action('wp_loaded', 'error_email_verify');
function my_login_message() {
if( $_GET['mode'] == 'emailverify' ){
$message = '<p id="login_error"><b>Verify your email.</b></p>';
return $message;
}
}
add_filter('login_message', 'my_login_message');
Here is what I would do. Basically I would generate a link that would have parameters attached to it something like this:
Generate Link
$username = 'user_login';
$hashcode = sha1(md5(md5("hacaak".$username."aalog")));
$link = get_home_url().'/?a='.$hashcode.'&b='.$hashcode.'&u='.$username.'&c='.$hashcode.'';
Output of link
[http:yourwebsite.com/a=fe440709d341e7b4994636b12e556aa7f23bb9ce&b=fe440709d341e7b4994636b12e556aa7f23bb9ce&u=jack&c=fe440709d341e7b4994636b12e556aa7f23bb9ce][1]
When the user clicks on this link in the email you just sent them use this method to catch the GET parameter coming into the site:
function to get the variables from url
function catch_email(){
if(isset($_GET['a']))
{
$username = sanitize_user($_GET['u']);
$user=get_user_by( 'login', $username );
$user_id = $user->ID;
$user_role = new WP_User($user_id);
$user_role->remove_role( 'unverified' );
$user_role->add_role( 'author' );
$url = get_home_url().'/Login?mode=emailverify';
wp_redirect($url);
}
}
add_action('get_header', 'catch_email');