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 );
Related
How to adjust the submitted Data before it gets submitted??
(I am trying to adjust the dropdown value's to use the value instead of the text)
Here is my code:
function wpf_dev_process( $fields, $entry, $form_data ) {
if ( absint( $form_data['id'] ) !== 66203 ) {
return $fields;
}
foreach ( $fields as $key => $value ){
if ($fields[$key]["type"] == "select"){
$fields[$key]["value"] = $fields[$key]["value_raw"];
}
}
echo "<pre>". print_r($fields, true)."</pre><hr>";
return $fields;
}
add_action( 'wpforms_process', 'wpf_dev_process', 10, 3 );
My echo statement shows on my page the "corrected" data as can be seen here in this image :
echo "<pre>". print_r($fields, true)."</pre><hr>";
But this is a User Registration Form and the data submitted to the Database is still the original value and not the value_raw that I change it to?
Anyone have any idea?
I solved this using the wpforms_user_registered action.
After the system created the user, I used update_user_meta to insert the correct values from the drop-downs.
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;
}
so i currently have gravity forms installed with a number of different forms. I want users to be able to see all the forms but only able to submit a maximum of 3.
I found Gravity Wiz Limit submissions, im able to limit the email address to being used 3 times but this is only working for 1 form, i need it to allow users to submit 3 different forms (eg. a global limitation)
https://gist.github.com/spivurno/4024361
having looked through this and then finding
add_filter( 'gpls_rule_groups', function( $rule_groups, $form_id ) {
// Update "123" to the ID of your form.
$primary_form_id = 123;
if( $form_id == $primary_form_id ) {
return $rule_groups;
}
$rule_groups = array_merge( $rule_groups, GPLS_RuleGroup::load_by_form( $primary_form_id ) );
foreach( $rule_groups as $rule_group ) {
$rule_group->applicable_forms = false;
}
return $rule_groups;
}, 10, 2 );
and
add_filter( 'gpls_apply_limit_per_form', '__return_false' );
it looks like its possible but how can i implement this ?
Is it for a user or guest or both? By user you could create a user meta and then use the gform_after_submission to add to the meta. Then use a pre_render to check and see if they are maxed out, and redirect them if they are.
add_action( 'gform_after_submission_{form_id}', 'after_submit_{form_id}', 10, 2 );
function after_submit_{form_id}( $entry, $form, $field ) {
$user_id = get_current_user_id();
$meta_key = 'count_submissions';
$get_meta = get_user_meta($user_id, $meta_key, true);
$qty = $get_meta + 1;
update_user_meta( $user_id, $meta_key, $qty );
}
If you need both, you might just create a database table and collect IPs and a submission count. Then update that on submission.
I've tried to replace username with first name billing using the code below changed from this answer thread, but keep getting 500 error.
If I use first and last name it works but I would prefer to use first name only.
Code is as follows:
add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){
// Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
$wrong_user_names = array( 'info', 'contact' );
// get the first billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ( ! empty($first_name) ) ) && in_array( $new_customer_data['user_login'], $wrong_user_names ) ){
// the customer billing complete name
$first_name = $first_name;
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( str_replace( $first_name ) );
}
return $new_customer_data;
}
My question would be, how would I configure WooCommerce to generate the username by the custom fields: First Name (billing_first_name) instead of full name or username?
Try the following, to replace username by the billing firstname during checkout registration:
add_filter( 'woocommerce_new_customer_data', 'customer_username_based_on_firstname', 20, 1 );
function customer_username_based_on_firstname( $new_customer_data ){
// Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
$wrong_user_names = array( 'info', 'contact' );
// get the first billing name
if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
if( ! empty($first_name) && ! in_array( $_POST['billing_first_name'], $wrong_user_names ) ){
// Replacing 'user_login' in the user data array, before data is inserted
$new_customer_data['user_login'] = sanitize_user( $first_name );
}
return $new_customer_data;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Your error was coming from str_replace( $first_name ). This php function needs 3 arguments.
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.