1

I am trying to figure out how to add user information and specific meta data to a separate table through PHP. I would prefer to do this when a user posted to the wp_users and wp_usermeta table. I am using the WooCommerce Auction plugin and the Salient theme.

Also if I made a PHP file that ran a query to pull the data and then insert, where is a good place I can call that function to ensure the table is up to date?

This is the function I've created.

add_action('user_register', 'save_to_donors',10,1);

function save_to_donors( $user_id ) {

    $single = true;

    $user_email = get_userdata($user_id);

    $fn= get_user_meta($user_id, 'first_ name', $single );
    $ln= get_user_meta($user_id, 'last_name', $single );
    $em= $user_email->user_email;
    $ph= get_user_meta($user_id, 'billing_phone', $single );
    $ad= get_user_meta($user_id, 'billing_address_1', $single );
    $ci= get_user_meta($user_id, 'billing_city', $single );
    $st= get_user_meta($user_id, 'billing_state', $single );
    $zi= get_user_meta($user_id, 'billing_postcode',$single );
    $do= get_user_meta($user_id, '_money_spent', $single );

    $sql = "INSERT INTO donors (user_id,first_name,last_name,email," .
    "phone,address,city,state,zip,amount_donated)".
    "VALUES('$user_id','$fn','$ln','$em','$ph','$ad','$ci','$st','$zi','$do')";

    $result = mysqli_query($wpdb, $sql) or die('Write Error!');

 }
3
  • 1
    This question might be helpful: wordpress.stackexchange.com/questions/63600/… Commented Jul 20, 2016 at 0:54
  • When in the process are you trying to add data to the donors table? Why not just add in the user id and the donation amount? With the user id you can fetch the required data from the user and user meta tables
    – stoi2m1
    Commented Jul 21, 2016 at 1:39
  • I am trying to add data after the user registration and then updating the table when the user donates.
    – dozer
    Commented Jul 21, 2016 at 15:55

3 Answers 3

1

Looks to me you should be seeking a woocommerce purchase completion hook. This would be when you could add that a donation has been made and at what amount, then you could grab any user information, amounted donated and other info you need and save it to your donor table.

use this:

add_action('woocommerce_order_status_completed', 'save_to_donors',10,1);

instead of this:

add_action('user_register', 'save_to_donors',10,1);

also change $user_id to $order_id as the parameter being passed into your function.

You will need to make some changes to your function since now $order_id will be passed into your function instead of $user_id. You can use some of the following code to get the $order object and $user_id I also found some other code that will get you info from the order vs from the users meta, not sure if they are going to be different.

$order = new WC_Order( $order_id );
$user_id = $order->user_id;
$billing_address = $order->get_billing_address();
$billing_address_html = $order->get_formatted_billing_address(); // for printing or displaying on web page
$shipping_address = $order->get_shipping_address();
$shipping_address_html = $order->get_formatted_shipping_address(); // for printing or displaying on web page

Info on the use of this hook woocommerce_order_status_completed
Info on how to get user id from order id

1
  • Thanks for the idea, I think adding after an order will be the right way to go.
    – dozer
    Commented Jul 21, 2016 at 15:57
1

This action hook allows you to access data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.

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. The password has already been encrypted when this action is triggered.

Typically, this hook is used for saving additional user meta passed by custom registration forms.

This example will save a first_name field passed by a custom registration field.

Also, keep in mind that validation of registration fields should not be performed within this hook! Validate using the registration_errors hook, instead (the user_register hook will not be called if registration_errors validation fails).

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) {

    if ( isset( $_POST['first_name'] ) )
        update_user_meta($user_id, 'first_name', $_POST['first_name']);

}
3
  • Would I put this new function in user.php? My understanding of hooks is you can place it anywhere since its in a global array. Is that correct?
    – dozer
    Commented Jul 20, 2016 at 21:52
  • you put it in your functions.php file, dont mess with user.php its a wp core file
    – stoi2m1
    Commented Jul 20, 2016 at 22:31
  • Okay, is there another tag that occurs after meta data has been inserted? "user_register" only has user_data by then
    – dozer
    Commented Jul 21, 2016 at 0:41
0

NOTE: This answer looks irrelevant to the OP's question after further analysis of the added/updated function in the question. Im leaving this answer in place and creating a new answer for historical use, since part of the question seems applicable.

You could use something like the following to create user meta when users are added to the user table. Put this in the functions.php file of your theme.

function myplugin_registration_save( $user_id ) {

    if ( isset( $_POST['first_name'] ) )
        update_user_meta(
            $user_id, 
            'your_key', // name of key 
            $your_value // value of your key
        );

}
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

To retrieve this data for later use use the function:

get_user_meta($user_id, 'your_key', $single);

More info on user_register hook More info on update_user_meta() More info on get_user_meta()

5
  • 3
    Please post a proper answer. Use How to Answer as a guide to help you structure a good answer. Answers should never be links to external sources. All relevant info from the link should be inside your answer. Thanks Commented Jul 20, 2016 at 6:59
  • @PieterGoosen FYI this post has been updated by the OP now
    – Tim Malone
    Commented Jul 21, 2016 at 6:15
  • @TimMalone and thus making this answer invalid now.
    – stoi2m1
    Commented Jul 21, 2016 at 6:29
  • Actually I meant that you had updated your post.. I was hinting to Pieter that he could remove a downvote if he thought it was better now ;) Didn't notice the question was updated too.. that sucks if it makes yours irrelevant now. You could edit yours to add a notice to this effect if you think your answer can still help others; otherwise it might be worth considering deleting it if you don't have the time/inclination to revise again.
    – Tim Malone
    Commented Jul 21, 2016 at 7:40
  • @TimMalone good idea to just delete this, but two of us thought the question was relevant to the question, since we both gave practically the same answer. I added a note about that and I made a new relevant answer, thanks for the tip though.
    – stoi2m1
    Commented Jul 21, 2016 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.