Wordpress Action "update_user_meta" - wordpress

Im trying to create an action when a user meta value is updated, another meta value of the same user will be updated. Like this:
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
update_user_meta($user_id, 'meta_to_store', $_POST['my_meta_stored_field']);
}
I can not understand the procedure, any ideas? Thanks in advance;
NOTE: Now works! Thanks Amin.T

Its not the right hook and you are missing some functions parameters, so you need something like this:
add_action('edit_user_profile_update', 'update_extra_profile_fields');
function update_extra_profile_fields($user_id) {
$new_value = "new_value";
update_user_meta($user_id, 'other_meta', $new_value);
}

Related

WordPress ACF Save Post function, need it only to fire when the page saved is custom post type. Or, if field is present?

I am trying to manipulate the my_acf_save_post function to do some maths and update a field with the resulting number. This part is working, the fields full_market_price and example_price are used to work out calculated_price.
I did this some days ago but have now run in a problem whenever trying to save a page, or post that doesn't need this function, and doesn't contain the ACF fields. So every section of the website rather than just the 1 that requires this maths. It results in various errors and I can't save the pages properly.
How can I make this code snippet only work if the page being saved is within custom post type? So it won't break the other pages?
I'm trying something using
if (is_single() && is_post_type('nameofposttype'))
however I can't seem to get it right, PHP not my strongest! Is a better way to ask if the fields does not exist, then do nothing?
Many thanks for any help or ideas,
function my_acf_save_post( $post_id ) {
// get values
$fmp = get_field('full_market_price');
$saPercent = get_field('example_price');
// do something
$examplePrice = ($fmp / 100) * $saPercent;
update_field('calculated_price', $examplePrice, $post_id);
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I just ran into a similar issue. I would do something like:
function my_acf_save_post ($post_id) {
// If not CPT, exit
if (get_post_type($post_id) != 'nameofposttype') {
return;
}
// Remainder of code
...
}
add_action('acf/save_post', 'my_acf_save_post', 20);

Contact Form 7 get_posted_data not working in hook wpcf7_before_send_mail

I have borrowed this code from stackoverflow in attempt to find a solution. I need to create a new post from submitted data on a form. I was using the filter:
add_filter( 'wpcf7_posted_data', 'save_new_booked_event_data' );
but found that it fired after every submit and not AFTER validation. So I moved on to this code:
add_action('wpcf7_before_send_mail','contactform7_before_send_mail',1);function
contactform7_before_send_mail( $contact_form ) {
if ( !isset($contact_form->posted_data) && class_exists('WPCF7_Submission') ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$formData = $submission->get_posted_data();
$formField = $formData['_wpcf7'];
update_post_meta(199, 'first_name', $formField);
}
} else {
// We can't retrieve the form data
return $contact_form;
}
return $contact_form;
}
Five hours later: the get_posted_data method is always NULL. I ve tried approaching it from different angles including turning off ajax and posting the form. I've checked and dumped every object to make sure that it works but it always turns up NULL. What am I missing here? Please anyone?
Thank you!
Educated guess:
The get_posted_data method relies on setup_posted_data firing, first, and that occurs when the constructor for the WPCF7_Submission class is invoked.
Perhaps there are multiple contact forms and your contactform7_before_send_mail function is hooked to one of them that hasn't been submitted, and therefore, the data are null.
That being the case, you could override and force population of posted data for your form if you pass an instance of WPCF7_ContactForm to the WPCF7_Submission::get_instance(); call.
Something like:
$form = new WPCF7_ContactForm($id_of_your_form);
$submission = WPCF7_Submission::get_instance($form);
By the way! Your snippet appears to be from this wordpress plugin. (Form to Post) In case you want a more configurable solution, or to browse the context it lives in.

Trigger WooCommerce Email on Custom Order Actions

I have been experimenting all day and researching the whole web, and I cant seem to get this action to work. Basically I'm trying to trigger a Woo Email when a custom order action is selected. In this case, its a gift receipt.
Please Note: When I turn on debugging, I get a headers already sent notice, none when its off.
Here is the code which I had tried:
function gift_receipt_add_order_meta_box_action($actions)
{
global $theorder;
$actions['send_gift_receipt'] = __('Send Gift Receipt', 'enyc');
return $actions;
}
add_action('woocommerce_order_actions', 'gift_receipt_add_order_meta_box_action');
function gift_receipt_wc_process_order_meta_box_action()
{
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if (!empty($mails))
{
foreach ($mails as $mail)
{
if ($mail->id == 'wc_gift_order_email')
{
$mail->trigger($order->id);
}
}
}
}
add_action('woocommerce_order_action_send_gift_receipt', 'gift_receipt_wc_process_order_meta_box_action');
Thanks.
function gift_receipt_wc_process_order_meta_box_action()
is missing $order
function gift_receipt_wc_process_order_meta_box_action($order)
might this be the issue?
So I figured it out after some more coffee. The problem was 2 fold:
1) I did not pass the order ($order) info to the function gift_receipt_wc_process_order_meta_box_action()
2) the id (name) of the email was actually 'wc_gift_order' instead of 'wc_gift_order_email'
Thanks for the help!

Custom Order Listing Page With WooCommerce

I have created a plugin which has a separate page for order listing.. in that it looks like the same as WooCommerce's Order Listing page but. i am unable to get the comments of the order so i added my custom post type to wc_order_types after that there is no order listed.. its showing a empty table. ?
add_filter( 'wc_order_types',array($this,'add_wc_order_types'),10,3);
public function add_wc_order_types($order_types,$type){
$order_types[] = WC_QD_PT;
return $order_types;
}
apply_filters( 'wc_order_types', $order_types, $for ); is default wc_filters which takes 2 parameters you have asked for 3 here add_filter( 'wc_order_types',array($this,'add_wc_order_types'),10,3); and again supplied 2.
visit http://docs.woothemes.com/wc-apidocs/source-function-wc_get_order_types.html#149 It may help you do this.
I Solved The issue just by adding a if condition in my hook's function
function add_wc_order_types($order_types,$type){
$order_type = $order_types;
if('' == $type){
$order_type[] = WC_QD_PT;
}
return $order_type;
}

retrieve cck field for form_alter

I would like to retrieve a cck field "field_info" in my form alter to insert into another table when user is submitting. This doesn't seem to work.
//mymodule_form_alter() implemented
function mymodule_form_mysubmit{
$test = $form['field_info']['#value'];
//insert stuff code
}
Is there any mistake in the code?
You say module_form_alter() is implemented, but just to confirm, you need to have the following in it:
$form['#submit'][] = 'mymodule_form_mysubmit';
Assuming you do, to get the value of field_info, your submit function should look like:
function mymodule_form_mysubmit($form, &$form_state) {
$test = $form_state['values']['field_info'][0]['value'];
}
$form_state contains the current state of the form being submitted. CCK always assumes that there could be multiple values for a field, so it always puts things in an array (hence ['field_info'][0]).
I found the solution
$test = $form['field_info'][0]['#value'];

Resources