update_user_meta not saving to database - php

I have code that is running in the "woocommerce_created_customer" hook.
I have created custom fields in my checkout and some of them need to be copied to the user profile.
I have different checkout pages using WooFunnels plugin, some ask for the custom fields and some don't but I need to make sure that all the custom fields have meta_data.
So, I have the following code in the hook:
if (empty($_REQUEST["condition_other_info"])){
update_user_meta( $user_id, "condition_other_info", "");
}else{
update_user_meta( $user_id, "condition_other_info", $_REQUEST["condition_other_info"]);
}
if (empty($_REQUEST["date_of_birth"]) ){
$dob_update_return = update_user_meta( $user_id, "date_of_birth", "2000-01-01");
error_log("return of update_user_meta of 'date_of_birth':". $dob_update_return);
}else{
update_user_meta( $user_id, "date_of_birth", $_REQUEST["date_of_birth"]);
}
No, here is the funny part. The first one works fine and saves to the database (along with about 11 other similar "update_user_meta" commands, but the second one (along with 3 others) does not. The 4 that do not work are in the middle of the good 11 ones.
No errors are thrown.
Not only that, but when I check my debug log, $dob_update_return shows me a meta_id value (that is in order with the orders) but when I go to the Database after the user is created, these 4 meta records are gone, while the others are there... Those 4 numbered keys are missing.
Am I the only one that thinks this is quite strange?
I feel like there might be other code that it is deleting these values after I add them, but I can not figure out how to find that code...

Related

Delete ACF user profile repeater field row from user account (WordPress)

I am using ACF repeater field for user profiles, which shows like this in the user profile page:
That's cool.
But on the front-end I have a form that I want to use to delete a specific row. My custom form simply lists all the rows with radio buttons, and so if the user selected number 3 and submitted the form then I want to delete the third row (in this example, the row with Catherine Davies would be deleted).
The form works fine in that it submits as expected and returns the value the user selected, but my code to delete the row that was selected doesn't seem to work.
The ACF documentation seems a little vague on the subject. Based on a combination of the ACF doc and this StackOverflow post, I expect this code to work but it does not:
$user_ID = get_current_user_id();
$field = 'extra_user_info'; // Name of the repeater field
$row_to_delete = $_POST["row_to_delete"];
delete_sub_row($field, $row_to_delete, 'user_' . $user_id);
Just to be sure, even if I hardcode the $row_to_delete variable to any number (from 1 to 4) it still does not delete any row.
Just to clarify, I wish to delete an entire row.
Note: I realise I could just embed the ACF form on the front-end, but for reasons I won't go into this is not an option, hence using my own custom form.
There could be another issue but in your example at least, your $user_ID variable has capital letters, while the variable passed to delete_sub_row() is lowercase. PHP variables are case-sensitive.

Trying to populate custom fields in Wordpress from WP database

I'm trying to populate a custom field, let's say 'field_123' (in this case from ACF plugin) with data from a Wordpress database table (which is coming from an API), let's say 'house' > 'square_metres'.
Posts (cpt) are already showing the main content 'out of the box' but I would want to add some more database tables to populate other custom fields. (which I then can use later on on different grids in visual composer)
I started with something but then realized soon enough I wasn't gonna solve the problem myself..
global $wpdb;
$new_value = $_POST['acf']['field_123'];
$wpdb->update(
"house",
array(
"square_metres" => $new_value,
),
array( '%s' )
);
}
add_action('acf/save_post', 'update_field', 1);```
Someone any suggestions? Or a push in the right direction? Perhaps there are smarter/easier ways to achieve this.
Thanks!
Is it all on the same DB? "house" is an existing table? what's it's name on the database? are you sure you didn't call it "wp_house"? in that case you are missing $wpdb->prefix."house" as the table name.
That last row of "add_action" is confusing me, what were you exactly trying to do? With that code, if the "house" table exist, you whould be able to populate its square_metres value with what you have in $new_value just fine (be careful, you didn't specify any WHERE clause, you are going to update ALL the rows in a single shot, dunno if this was intentional).
Is your post missing a piece of code?

Update a post's taxonomy when a specific acf_form() is updated

I'm currently building a parent admissions portal for a school. Pupils are created as a custom post type (pupil) when a user (parent) signs up – the parent is the author of each pupil post. These pupil posts have a custom taxonomy named status which determines if the pupil's admission forms have been submitted and reviewed. I'm using acf_form() to create the admission forms that display on the frontend and are filled in by the parent user. Each pupil currently has 6 ACF field groups (6 forms). I'm getting the keys of these field groups and looping through them with a foreach loop to display 6 individual acf_form() forms for each of the field groups.
When a pupil post is created, it's status is automatically set to "in progress" i.e. the parent is filling out the forms.
What I want
When a user updates one specific form/field group of the 6, I want the pupil's status taxonomy to be changed from "in progress" to "submitted". To explain further, this form is the final form the user/parent needs to complete and so once they press the acf_form() update button, the taxonomy should be updated.
So essentially...
I'm looking for a way to hook into the action of saving an acf_form(), doing a check to see if the form that was updated was a specific form (by the field group key?) and then updating the status taxonomy of the post that the form was submitted on.
What I've tried
I've tried using ACF's pre_save_post filter and save_post action but I can't figure out a way to check which field group was saved as those two functions just give me the $post_id. I simply added the code from the ACF Docs and tried to print_r() / echo the data from the functions to see if there was any way for me to check the field group key, but nothing is being printed on the page when I update the acf_form().
So...
Does anyone know how I can hook into that action properly and get the correct data that will allow me to do the checks that I want?
I found a way to do what I'm looking for but I feel like it isn't a very good or robust way to do it so I'm still open to suggestions/advice.
The code:
function my_acf_save_post( $post_id ) {
$keys_of_specific_field_group = array(
'field_5ce2970bc9fbd' => '',
'field_5ce29741c9fbe' => '',
'field_5ce297c7c9fc1' => '',
'field_5ce297ddc9fc2' => '',
'field_5ce297fac9fc3' => '',
'field_5ce2980ec9fc4' => '',
'field_5ce29874c9fc5' => '',
'_validate_email' => ''
);
if ( $_POST['acf'] === $keys_of_specific_field_group ) {
wp_set_post_terms($post_id, array(21), 'status');
}
}
add_action('acf/save_post', 'my_acf_save_post', 1);
Basically I am just checking if the field keys returned from $_POST['acf'] match the field keys of the fields in the specific form I want to check, and if they do I am updating the post's status taxonomy (array(21) is the ID of the 'submitted' term I want to set).
Hopefully there's a nicer/better way to do this as if any fields are added to the field group I will have to manually update the code.

WooCommerce - How to get customer's city from review/comment?

I am building a plugin that will cycle through all customer reviews and show them on any page.
I know how to get the user's display name from the review/comment, but how to get his billing location also?
I think what I need is a way to get the WooCommerce user data from the review/comment ID - if this is possible??
Providing they've submitted an order and you've got their user ID, you should be able to do something like:
$city = get_user_meta( $user_id, 'shipping_city', true );
or...
$city = get_user_meta( $user_id, 'billing_city', true );
It might be worth having a look at your user meta table to see what's in there.
Edit
Just re-read the question and maybe you don't have the user ID yet. You should be able to get this from the $wpdb->comments table.

keeping track of deleted posts without using wordpress functions

i have 2 wordpress websites A and B.
if A somehow changes (adding a post , editing a post , deleting a post ) , B needs to change as well .
so in A by using a php robot which is checking database every couple of minutes , i,ve created a json feed for added and edited posts of A .
it's easy to track added/edited posts by checking wp_posts table . but i can't find a way to know which posts have been deleted ( Delete Permanently ).
this script is suppose to be portable and usable for people who are not programmer , so i dont want to use wordpress functions or do any change/addition to wordpress system .
so is there any way to find deleted posts by checking the database or something like that ?
You can check for posts in the trash by using the query:
SELECT * FROM `wp_posts` WHERE `post_status` = 'trash'
but if you are trying to find posts that have been removed from the database you will need to either track the missing IDs in the wp_posts table or add some functionality to track when a post is deleted. You can do this by using the delete_post action hook in your theme:
function my_tracking_function() {
// Put ID into new database table
}
add_action( 'delete_post', 'my_tracking_function', 1 );
And then pass that table to the other site for processing. Once both sites are synced you can run a cleanup routine to empty the tracking table. I realize that this is partially using Wordpress functionality, but the passing of the table data can be done outside of Wordpress using SQL.

Categories