Wordpress - Send email on Post meta value change - wordpress

I want to accomplice the following. I want to automatically send an email on post publish/update when a certain post field value has changed.
In a post
A ACF field that has 4 options, lets say [ 'draft', 'ready for group1', 'ready for group 2', 'ready']
If this field gets changed on post update send email to "this" email address.
I think I need to know 2 things for this.
- How and where (what action) do I need to insert custom code on post publish/update
- How to compare new post data to old state (and is this possible/availible in the action above)

You can hook onto acf/save_post for this purpose. Read the documentation here:
https://www.advancedcustomfields.com/resources/acf-save_post/
Since you want the callback to fire before the values are stored, in order to compare the old value against the new one, keep in mind to add a priority of less than 10. Assuming the field with 4 options has the field key field_4afd4af14415f:
function on_acf_post_save($post_id) {
$post_type = get_post_type($post_id);
if ($post_type === 'your-post-type') {
$old_val = get_field('field_4afd4af14415f', $post_id);
$new_val = $_POST['acf']['field_4afd4af14415f'];
if ($old_val != $new_val) {
// Send desired mail in here:
// wp_mail(...);
}
}
}
add_action('acf/save_post', 'on_acf_post_save', 5, 1); // priority = 5
If your ACF field isn't at the top level, but inside a Group or Repeater, you will have to adapt the code reading from the $_POST['acf'] and get_field() result.

Related

Wordpress Help (WPForms + PayPal Workflow)

CONTEXT:
I’ve used the WPForms plugin in Wordpress to create a custom donation form for my client. The client wants to accept donations via PayPal.
PROBLEM:
WPForms has a PayPal add-on feature but it costs $200.
QUESTION
To avoid paying $200 for the expensive plugin, how can I manually direct/post the WPForm data entered by the user (name, email, address, donation amount, and employment info) to PayPal?
In the example code you’ll see below, we’re first checking the form ID to make sure it matches the form that’s being targeted. Then we’re checking a specific field (by the field ID) to see if it’s empty.
Just remember to change the form ID from 5 to match your form ID and change the '4' to match your field ID.
function wpf_dev_process( $fields, $entry, $form_data ) {
// Optional, you can limit to specific forms. Below, we restrict output to
// form #5.
if ( absint( $form_data['id'] ) !== 5 ) {
return $fields;
}
// check the field ID 4 to see if it's empty and if it is, run the error
if(empty($fields[4]['value']))
{
// Add to global errors. This will stop form entry from being saved to the database.
// Uncomment the line below if you need to display the error above the form.
// wpforms()->process->errors[ $form_data['id'] ]['header'] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Check the field ID 4 and show error message at the top of form and under the specific field
wpforms()->process->errors[ $form_data['id'] ] [ '4' ] = esc_html__( 'Some error occurred.', 'plugin-domain' );
// Add additional logic (what to do if error is not displayed)
}
}
add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );

Gravity forms: Authorize.net Invoice number

I was wondering If you knew how to add an invoice code to the forms in Authorize.net.
I check the authorize.net feed settings but they do not ask for an invoice code.Then, I started to do some research and found the hook gform_authorizenet_save_entry_id that could be used to create that invoice code.
The problem comes that there is no documentation about this hook. It was only mentioned as one of the updates. So, I'm creating a hidden field with an {entry_id} as a default value and trying to find a way to pass it as an Invoice Number.
Any help would be appreciated. Thanks :)
Update:
I was able to add a transaction code to the form using the following Snippet
//Adding the transaction code
add_filter( 'gform_authorizenet_transaction_pre_capture', 'set_invoice_number', 10, 5 );
function set_invoice_number( $transaction, $form_data, $config, $form, $entry ) {
if ( $form['id'] == 6 ) {
// your submission ID format to be inserted into the hidden field
$SubmissionID = 'RW-' . $entry['id'];
$transaction->invoice_num = $SubmissionID;
}
return $transaction;
}
I got the invoice number to become "RW-" but the $entry['id'] is not printing anything
You can assign an invoice number using an input field by adding this code to your theme's functions.php file:
add_filter('gform_authorizenet_transaction_pre_capture', 'set_invoice_number', 10, 4);
function set_invoice_number($transaction, $form_data, $config, $form)
{
$transaction->invoice_num = rgpost('input_YOUR INPUT FIELD NUMBER HERE');
}
If the input field is the hidden field containing the form's entry id that should accomplish what you're wanting.
I use the following code to append the "Form Name" to the invoice number passed to Authorize.net. I believe that the previous answer would work if the first field you create in each form was the "Invoice Number" field, but if you add the field later, or add it to your previously created forms, they wouldn't all have the same field number so it may not work. This code will use the automatically generated unique invoice number and add the form name.
i.e. Form Name: "Yearly Subscription" Auto Invioce Number: "1234567890"
Info that Authorize.net receives: "1234567890-Yearly_Subscription"
I have found that instead of adding custom functions to your theme file, it is better to build a custom plugin and add them there instead. This way if your theme updates, you won't lose your functions. The code to create your plugin, along with the functions for your Authorize.net code is included below. Save this file as My_Custom_Functions_Plugin.php and upload it to your web host in the "wp-content/plugins/" folder and then activate it. Next time you need to add any customized functions, just add them at the end of this file.
<?php
/*
Plugin Name: My_Custom_Functions_Plugin
Plugin URL: http://WEBSITE
Description: Custom Functions and Scripts for WEBSITE
Version: 1.0.0
Author: NAME
Author URI: http://WEBSITE
*/
// Functions for apending form name to authorize.net invoice
add_filter( 'gform_authorizenet_transaction_pre_capture', 'invoice_num', 10, 4 );
function invoice_num( $transaction, $form_data, $config, $form ) {
$transaction->invoice_num = uniqid() . '-' . rgar( $form_data, 'form_title' );
gf_authorizenet()->log_debug( 'gform_authorizenet_transaction_pre_capture: ' . print_r( $transaction, 1 ) );
return $transaction;}
// End of Authorize.net Function
// Add any additional Functions below this comment line to keep your themes functions.php file from getting overwritten on theme updates. Copy and paste this comment line before each function and give it a description to help keep you organized about what your function does
?>
Hope this helps!

wordpress: let subscribers submit content to post ( yes / no, radio buttons )

i'm currently struggeling with finding the right way for a function in wordpress.
there are many subscribers who should be able to say "yes" or "no" to a post.
until now, i added some more fields to the comments template.
but its not the right way because of empty comments, its like raping the comments function.
the point is: how to let subscribers (or any other role) submit content (like a comment) to a post?
is there any other way than the_content & the_comment, user specific?
You can create custom form and after submission add data in database in wp_postmeta table.
For example `
get data from form
$data = array();
$data['user_id'] = $user->ID; // current user id
$data['post_id'] = $_POST['post_id']; // post id which you will receive after form submission
$data['answer'] = $_POST['answer']; // this is your radio button value yes or now
You can serialize $data or you can keep it as json in database.
INSERT INTO wp_postmeta (meta_key,meta_value) VALUES ('user_vote', $serialized_data);
Best Regards,
Davit.

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

Change wordpress post author on the fly while publishing the post

I am developing my site and require to change post author id on the fly while publishing.
All post will be write and publish by admin but as content provided by different author and need to change post author id from admin to other author ( author id will get from custom field )
So how to do this on the fly while publishing.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.
$post_id = $post->ID; // change this to whathever
$user_id = '4'; // change this too
$the_post = array();
$the_post['ID'] = $post_id;
$the_post['post_author'] = $user_id;
wp_insert_post( $the_post );
The code above will update the currently looped through post (via $post->ID) with an author with ID of 4.
Read how wp_insert_post works here. Basically, if you pass it an ID of a post that already exists it will update that post with the new information you pass to it. (the author ID in this case)
If you want to grab things from the custom fields you can use:
$author = get_post_meta($this->ID, 'author_id', TRUE);
More info on custom fields.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.

Resources