Access User Meta Data on User Registration in Wordpress - wordpress

I am attempting to carry out a few functions when a user registers on a wordpress site. I have created a module for this which carries out the following function:
add_action( 'user_register', 'tml_new_user_registered' );
function tml_new_user_registered( $user_id ) {
//wp_set_auth_cookie( $user_id, false, is_ssl() );
//wp_redirect( admin_url( 'profile.php' ) );
$user_info = get_userdata($user_id);
$subscription_value = get_user_meta( $user_id, "subscribe_to_newsletter", TRUE);
if($subscription_value == "Yes") {
//include("Subscriber.Add.php");
}
echo "<pre>: ";
print_r($user_info);
print_r($subscription_value);
echo "</pre>";
exit;
}
But it seems that i am not able to access any user meta data as at the end of this stage none of it is stored.
Any ideas how i execute a function once Wordpress has completed the whole registration process of adding meta data into the relevant tables too?
I attempted to use this:
add_filter('user_register ','tml_new_user_registered',99);
But with no luck unfortunately.
Thanks in advance!

I read at the action reference api page that the user id is passed as user ID. Try substituting your $user_id for $user_ID.

I don't think the user metadata is available at the point where this action hook is triggered. From the Codex
"Not all user meta data has been stored in the database when this action is triggered. For example, nickname is in the database but first_name and last_name are not (as of 3.9.1). The password has already been encrypted when this action is triggered."

Related

Allow checkout without login for existing users only (woocommerce)

I'm trying to find a solution that will allow paying for an order without login for registered users only. Guests must not be able to pay for the order. I have already found some snippets which link orders if email exists or create a new account if not. I would like to simplify the purchase process for existing users but still avoid guest checkout for new customers.
Here is the code which I have already tried (it works to link orders to existing customers, but I would like to prevent guest checkout and new account creation during checkout). Actually there is no need to login users. I would like to simply allow to complete the order in case if the user (email or use ID) exists and decline if user do not exists.
`
//assign user in guest order
add_action( 'woocommerce_new_order', 'action_woocommerce_new_order');
function action_woocommerce_new_order( $order_id ) {
$order = new WC_Order($order_id);
$user = $order->get_user();
if( !$user ){
//guest order
$userdata = get_user_by( 'email', $order->get_billing_email() );
if(isset( $userdata->ID )){
//registered
update_post_meta($order_id, '_customer_user', $userdata->ID );
}else{
//Guest
}
}
}
`

Wordpress plugin form submitting to database on every browser refresh

I am developing a wordpress plugin, backend is working as I need but I am facing issue in frontend, I have created a shortcode page from I am posting few values to next page but when I refresh the next it is sending values to database again and again. I want to stop it, here is function:
function technician_checklist_report_generated(){
global $current_user;
global $wpdb;
$submit="";
if($_POST['customerid']!=""){
$crow = $wpdb->get_row( 'SELECT customer_id, firstname, lastname, createdon, marina, vesselmodel, vesselname, vesselyear
FROM wp_yacht_customers
WHERE customer_id= '.$_POST['customerid']
);
}
if($crow->customer_id!=""){
$table = 'wp_yatch_vessel_config_checklist';
array_pop($_POST); // delete last element in post array (submit)
$data = $_POST;
$format;
$wpdb->insert( $table, $data, $format );
}
require(dirname(__FILE__) .'/view/technician-start-reporting.php');
}
add_shortcode('technician-start-reporting',
'technician_checklist_report_generated');
When I reach to technician-start-reporting page it is sending data to MySQL again and again on browser refresh.
Is there any other way to go another page because I used wp_redirect and it showing error "header already sent... :
You can only use wp_redirect before content is sent to the browser.
Rather than process the form in the template[insite shortcode], you can hook an earlier action, like wp_loaded (this will be triggered before headers being sent).
<?php
add_action( 'wp_loaded', 'wpv_process_form' );
function wpv_process_form(){
if( isset( $_POST['customerid'] ) ):
// process form, and then
wp_redirect( get_permalink( $pid ) ); // redirect to desired page id
exit();
endif;
}
this way you'll be using less queries too.

Unable to save Query Vars with a post

I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.

Send custom refereal code to registration in WordPress

I'm passing a referral code that I'm saving to wp_usertmeta with a custom field added in functions.php
So far, so good!
Link to wp register: wp-login.php?action=register&ref=2
I save the referral code with:
<?php $ref = $_GET['ref']?>
It works BUT, if i enter a username thats already taken i get the standard error message from WordPress that it's taken.
When this happens the URL reloads to: wp-login.php?action=register and i cant use $_GET['ref']
Is there any alternative way to do this?
The easiest way to solve this would be to save the $_GET['ref'] value to a cookie or the PHP session. Once the user is registered, you can refer to the cookie/session to save to the database using the user_register action hook. Once it is saved, you should clear this value if it is a cookie unless it is needed for something else to reduce the size of each request.
//set the cookie
if ( isset( $_GET['ref'] ) ){
setcookie( "ref", $_GET['ref'] );
}
Then save the value using the action hook
add_action( 'user_register', 'my_user_register' );
function my_user_register( $user_id ){
if ( isset( $_COOKIE['ref'] ) ){
// save the ref to the user meta
update_user_meta( $user_id, 'ref', $_COOKIE['ref'] );
// delete the cookie
setcookie( "ref", null, -1 );
}
}

Wordpress Signed On User Lost

I programmatically log in a user in Wordpress (before headers are sent), using a session variable from a previous page. Later in this same page, in the body section, I var_dump: wp_get_current_user(). This returns the WP_User I logged in with. In the 'log in' section I have tried combinations of wp_signon(), wp_set_current_user() and wp_set_auth_cookie().
The problem comes, when I navigate away from this page, the user isn't logged in anymore. When I use wp-login.php, the user stays logged in. I've struggled with this for quite a while and can't seem to find, why the user won't stay logged in.
Can someone help please. I'll add snippets of currently used code below:
$creds = array(
"user_login" => $_SESSION['email'],
"user_password" => $newPass,
"remember" => true
);
$user = wp_signon( $creds );
if ( is_a( $user, 'WP_User' ) ) {
wp_set_current_user( $user->ID, $user->user_login );
wp_set_auth_cookie( $user->ID, true );
}
// Code follows ...
<body>
<?php if ( is_user_logged_in() ) var_dump(wp_get_current_user()); ?>
Where is it being executed? You'll need to put it in an action. Use:
add_action('theme_setup','your_code');
function your_code(){
//signin code block goes here
};
I eventually moved a segment of the code to another file. Something I failed to include was that I used wp_create_user on the same page. As soon as I replaced wp_set_current_user and wp_set_auth_cookie with wp_signon and moved it to a separate page, everything worked perfectly.

Resources