Send custom refereal code to registration in WordPress - 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 );
}
}

Related

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.

how to use session in woocommerce pages like we use normal php session?

How can i store my data in woocommerce session and used it anywhere in woocommerce pages(like thankyou page,processing mail page). 2nd i just have to send a unique code in woocommerce email(both admin and user). How to do it? Thanks in advance
For using session you can do
// Test if your are on Back Office, WC()->session isn't set
if( !is_admin() ){
$data = 'test';
WC()->session->set( 'name_for_your_data' , $data );
$retrive_data = WC()->session->get( 'name_for_your_data' );
}

Save data of custom metabox in Wordpress

I added a meta-box to the edit link page & I can't save the data whatever I put in the field. How can I only update the meta-box without saving the data in the database? Here is my code:
// backwards compatible
add_action( 'admin_init', 'blc_add_custom_link_box', 1 );
/* Do something with the data entered */
add_action( 'save_link', 'blc_save_linkdata' );
/* Adds a box to the main column on the Post and Page edit screens */
function blc_add_custom_link_box() {
add_meta_box(
'backlinkdiv',
'Backlink URL',
'blc_backlink_url_input',
'link',
'normal',
'high'
);
}
/* Prints the box content */
function blc_backlink_url_input( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'blc_noncename' );
// The actual fields for data entry
echo '<input type="text" id="backlink-url" name="backlink_url" value="put your backlink here" size="60" />';
#echo "<p> _e('Example: <code>http://Example.org/Linkpage</code> — don’t forget the <code>http://</code>')</p>";
}
How can I save or update the data of the input field of metabox? Only the data should be updated in the metabox. It should not save in database by any type of custom field.
I think it actually would be a good idea to save as a custom field, only one that doesn't show up in the custom field box. You can accomplish the latter by adding a "_" at the beginning of the custom field's name (i.e. "_my_custom_field" instead of "my_custom_field".
Here's a sample function to save your meta box data. I changed the names to match the code you have above.
:
<?php
function blc_save_postdata($post_id){
// Verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['blc_noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
// Verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
// to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions to edit pages and/or posts
if ( 'page' == $_POST['post_type'] || 'post' == $_POST['post_type']) {
if ( !current_user_can( 'edit_page', $post_id ) || !current_user_can( 'edit_post', $post_id ))
return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$blc = $_POST['backlink_url'];
// save data in INVISIBLE custom field (note the "_" prefixing the custom fields' name
update_post_meta($post_id, '_backlink_url', $blc);
}
//On post save, save plugin's data
add_action('save_post', array($this, 'blc_save_postdata'));
?>
And that should be it. I used this page as a reference: http://codex.wordpress.org/Function_Reference/add_meta_box
Hook the action save_post - it receives saved post ID and allows you to update the post the way you need when submitting post editor page. Don't forget that this action will be called for EVERY post saved - you need to only handle posts having your custom meta box.
you must disable this code
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
what it does is that it blocks your code below cause it detects that your doing some auto save on it.

Access User Meta Data on User Registration in 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."

Resources