Dynamic email in CF7 (using custom fields) - php

I'm trying to dynamically update the CF7 to field by replacing the recipient string with a custom post field value, though I can't figure out how to assign the value it's definitely replacing the string as I get an error and if I change email it sends. So it tells me the problem is when I'm trying to get the value.
<?php
function wpcf7_dynamic_email_field($args) {
$dynamic_email = get_post_meta(get_the_ID(), 'dynamic_email', true);
if(!empty($args['recipient'])) {
$args['recipient'] = str_replace('emailtobereplaced#email.com', $dynamic_email["dynamic_email"], $args['recipient']);
return $args;
}
return false;
}
add_filter('wpcf7_mail_components', 'wpcf7_dynamic_email_field');
?>
Can anyone point me in the right direction here? My custom field is called dynamic_email and I need the value

get_the_id() won't return the proper ID because Contact form 7 uses Ajax to perform the send.
You can get the post ID from the submission unit tag. The unit tag is a hidden form field that gets posted with the form and it looks cryptic but it stores information about the post. An example unit tag looks like wpcf7-f235-p27-o1. Using a regex we can get the post id out of the unit tag.
function wpcf7_dynamic_email_field( $args ) {
$dynamic_email = '';
$submission = WPCF7_Submission::get_instance();
$unit_tag = $submission->get_meta( 'unit_tag' );
// get the post ID from the unit tag
if ( $unit_tag && preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $unit_tag, $matches ) ) {
$post_id = absint( $matches[2] );
$dynamic_email = get_post_meta( $post_id, 'dynamic_email', true );
}
if ( $dynamic_email ) {
$args['recipient'] = str_replace('emailtobereplaced#email.com', $dynamic_email["dynamic_email"], $args['recipient']);
}
return $args;
}
add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );
Since this is a filter and not an action you always want to return the first parameter that was passed to your callback in this case $args.

Related

Wordpress Functions.php filter get_metadata is always empty

I have a post and I need to process some of the meta data attached to that post.
I am struggling to get the post meta when calling get_metadata('post',53 ) where 53 is the specific postid I need.
The write_log function outputs to my debug log and the return value is always empty.
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
if ( 'company_categories' == $params['facet']['name'] ) {
write_log($params); //params contains correct values
$defaults = $params['defaults'];
$post_id = (int) $defaults['post_id'];
$post = get_post($post_id);
$postType = $post->post_type;
write_log($postType." - ".$post_id); //postType and post_id contains correct values
//global $post;
if($postType == 'upt_user'){
$meta = get_metadata('post',$post_id );
write_log($meta); //meta is empty when using both $post_id variable and actual id value '53'
}
}
return $rows;
}, 999, 2 );
If I add the exact same code to a php template it returns the metadata without issue.
e.g.
$meta = get_metadata('post',53 );
I actually started with the get_post_meta function but I'm currently using get_metadata to simplify and rule out any issues with misusing the get_post_meta function and parameters.
Any suggestions of how to resolve please?

Gravity Forms: Dynamically populate field label or placeholder

I want to populate an input field with data from the current user.
At the moment I populate the fields value parameter but I want to keep the input empty.
Therefore it would be great if I could populate the label or the placeholder parameter of the input field.
I couldn't find anything in the docs.
Here's my current code:
add_filter('gform_field_value_username', create_function("", '$value = populate_usermeta(\'nickname\'); return $value;' ));
I believe this should work out-of-the-box with Gravity Forms:
With some help from the Gravity Forms support and some own testing, I came up with a solution:
function populate_usermeta($meta_key){
global $current_user;
return $current_user->__get($meta_key);
}
add_filter( 'gform_pre_render', 'populate_text' );
//Note: when changing choice values, we also need to use the gform_pre_validation so that the new values are available when validating the field.
add_filter( 'gform_pre_validation', 'populate_text' );
//Note: this will allow for the labels to be used during the submission process in case values are enabled
add_filter( 'gform_pre_submission_filter', 'populate_text' );
function populate_text( $form ) {
$items = array();
$fields = $form['fields'];
foreach( $form['fields'] as &$field ) {
if ( $field->type != 'text' || strpos( $field->cssClass, 'populate-placeholder' ) === false ) {
continue;
}
$field->placeholder = populate_usermeta($field->inputName);
}
return $form;
}

Stripping Formatting on Contact Form 7 fields

Is it possible to remove certain characters once the user has submitted the form? For example the user selects £10,000 and it would be stripped to 10000 when submitted and sent via email.
TIA
Here's the answer
// define the wpcf7_posted_data callback
function action_wpcf7_posted_data( $array ) {
//'amount' is the name that you gave the field in the CF7 admin.
$amount = $array['amount'];
if( !empty( $amount ) ){
$array['amount'] = preg_replace('/[\£,]/', '', $array['amount']);
}
return $array;
};
add_filter( 'wpcf7_posted_data', 'action_wpcf7_posted_data', 10, 1 );

Condition only when the post is complete edited, not added or trash

I'm creating some conditions in a function to return me only when the post is edited, not when it's created or deleted.
I've added some conditions but I still keep getting when I create or delete a post.
add_action( 'post_updated', 'my_product_edited');
function my_product_edited( $post_id, $post_after, $post_before ) {
// Check to see if we are autosaving
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// assure the post type
if ( ! get_post_type($post_id) == 'product' )
return;
if ( wp_is_post_revision( $post_id ) )
return;
}
You want to know how to get access to do work only when a post is edited.
You do use a callback registered to the post_updated event. However, we have to build a function that checks if this is an "edit" action.
What determines an "edit" only action?
You'd think that editpost as the action value would tell you that it is an edit action. Actually, this action is set when adding a new post as well as editing. Why?
WordPress grabs the next record and assigns the post ID when you click on Add New. The action's hidden input is set to editpost within the form's HTML.
Therefore, new and editing posts both use the editpost value for $_POST['action'].
We need to figure out what keys and values we can use then to differentiate between a new and edited post submission.
Add New Post HTML
Let's look at the HTML for the "Add New Post" form:
Take a look at the hidden field:
<input id="hiddenaction" name="action" value="editpost" type="hidden">
See how for a "new post" the hidden field "action" is set to a value of "editpost".
Edit Post HTML
Next, let's look at the HTML for the edit post interface:
Okay, see that it also uses the same value in the "action" hidden field.
<input id="hiddenaction" name="action" value="editpost" type="hidden">
What Field Can We Use?
Comparing the HTML fields above, you'll notice that the _wp_http_referer value is different between the Add New Post and Edit Post forms. Aha, we can use that field in combination with the action field.
Function to Check if Editing Post Submission
Let's put it together now and build a function that checks if this submission is for an "edit" only.
/**
* Checks if the post's submission is an "edit"
*
* #since 1.0.0
*
* #param int $post_id
*
* #return bool
*/
function is_post_submission_an_edit_task( $post_id ) {
if ( ! isset( $_POST['action'] ) ) {
return false;
}
if ( 'editpost' != $_POST['action'] ) {
return false;
}
if ( ! isset( $_POST['_wp_http_referer'] ) ) {
return false;
}
$referer = '/wp-admin/post.php?post=' . $post_id . '&action=edit&message=';
return $referer === substr( $_POST['_wp_http_referer'], 0, strlen( $referer ) );
}
The last couple of lines looks at the referer but not the message= value. Why? Because that message value can change. Therefore, we clip that value and look at the rest of the string.
Using The Function
Next, you want to register a callback to the event named 'post_updated'. This callback allows for 3 parameters to be passed to you. You want at least 2 of them. Therefore, you need to specify:
priority number, i.e. I set to 10
number of arguments that you want, e.g. I set it to 3
Here's the code:
add_action( 'post_updated', 'process_after_product_edited', 10, 3 );
/**
* Process work when the product has been edited.
*
* #since 1.0.0
*
* #param int $post_id Post ID.
* #param WP_Post $post_after Post object following the update.
* #param WP_Post $post_before Post object before the update.
*/
function process_after_product_edited( $post_id, $post_after, $post_before ) {
if ( ! is_post_being_updated( $post_id ) ) {
return;
}
if ( $post_after->post_type != 'product' ) {
return;
}
// now you can do your work
}
The first check verifies if this is an "edit" submission. If no, we bail out as there's nothing to do.
The second check determines if the post type is a product, per your specific use case. If no, bail out as there's nothing to do.
Otherwise, you can put your code at the bottom as "Yes, this product was edited."
You can add the following check after your post type check.
if (!isset($_POST['action']) || $_POST['action'] != 'editpost')
{
return;
}
You seem to be trying to do what I have already accomplished in my plugin here: https://wordpress.org/plugins/tld-woocommerce-downloadable-product-update-emails/
Hopefully it helps you.

Remove Spaces from Advanced Custom Field data on Save using act/ save_post

I'm trying to remove spaces automatically from data entered into a custom field generated by the ACF plugin when a custom post is updated or saved in wordpress.
I believe I need to use the acf/save_post hook but I'm struggling to get the preg_replace to work. I wonder if I'm not using the right identifier as the custom field name has field name postcodes but when inspected it has name fields[field_55c7969262970]. Can't seem to make it work with that either.
function remove_spaces( $post_id ) {
if( empty($_POST['postcodes']) ) {
return;
} else{
$postcodes = $_POST['postcodes'];
$postcodes = preg_replace('/\s+/', '', $postcodes);
return $postcodes; }
}
add_action('acf/save_post', 'remove_spaces', 1);
I think you are better off using the acf/update_value filter. From thr docs: "This hook allows you to modify the value of a field before it is saved to the database."
function remove_spaces($value, $post_id, $field) {
if(empty($value)) {
return;
}
$value = preg_replace('/\s+/', '', $value);
return $value;
}
add_filter('acf/update_value/name=postcodes', 'remove_spaces', 10, 3);

Categories