Having a try with Ninja Forms, I’m actually able to get value from a field ID using $form_data array variable.
function my_ninja_function( $form_data ) {
$my_field_id = 1;
$my_value_from_field_id = $form_data['fields'][$my_field_id]['value'];
echo $my_value_from_field_id;
// output value is possible
}
And now trying to get value from a field key, without success...
$my_field_key = 'my_key';
$my_value_from_field_key = $form_data['fields'][$my_field_key]['value'];
echo $my_value_from_field_key;
// output value is not possible
with a little more effort...
$form_fields = $form_data['fields'];
foreach( $form_fields as $field ){
$field_value = $field['value'];
$field_key = $field['key'];
$data[$field_key] = $field_value;
};
$my_value_from_key = $data['my_key'];
echo $my_value_from_key;
// output is possible
It works!
By value I'm assuming you mean the field's label. You can get a field's label from the field's settings like this:
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$label = $model['label'];
}
if you really do mean value, then perhaps you are referring to a form submission's field value. You can get those like this:
$sub_id = 1; // Need to know the submission's ID
$sub = Ninja_Forms()->form()->sub($sub_id)->get();
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$value = $sub->get_field_value($model['key']); // User submitted value
}
Note that NinjaForms has added field keys in version 3 after I made the suggestion, as previous versions had no unique field identifier which made exporting/importing fields and forms very problematic.
Related
Does anyone have an idea on how to auto replace letters in woocommerce order forms! basically I need when someone is typing in greeklish to auto replace those letters to Greek!
I think you will have to use this hook to get the newly created order :
add_action( 'woocommerce_new_order', 'convert_greeklish_for_wc_order', 1, 1 );
function create_invoice_for_wc_order() {
function create_invoice_for_wc_order( $order_id ) {
// get order details data...
$order = new WC_Order( $order_id );
// Here goes the code to get all the fields
// Convert fields to greek
// Set new fields values
};
}
And adapt this code to convert greeklisk to greek:
<?php
function greeklish($new_text){
$greek_len = array('α','ά','Ά','Α','β','Β','γ', 'Γ', 'δ','Δ','ε','έ','Ε','Έ','ζ','Ζ','η','ή','Η','θ','Θ','ι','ί','ϊ','ΐ','Ι','Ί', 'κ','Κ','λ','Λ','μ','Μ','ν','Ν','ξ','Ξ','ο','ό','Ο','Ό','π','Π','ρ','Ρ','σ','ς', 'Σ','τ','Τ','υ','ύ','Υ','Ύ','φ','Φ','χ','Χ','ψ','Ψ','ω','ώ','Ω','Ώ',' ',"'","'",',');
$english_len = array('a', 'a','A','A','b','B','g','G','d','D','e','e','E','E','z','Z','i','i','I','th','Th', 'i','i','i','i','I','I','k','K','l','L','m','M','n','N','x','X','o','o','O','O','p','P' ,'r','R','s','s','S','t','T','u','u','Y','Y','f','F','ch','Ch','ps','Ps','o','o','O','O',' ','',' ',' ');
$new_text = str_replace($greek_len,$english_len,$new_text);
return $new_text;
}
$conv = greeklish("Το κείμενο σου εδώ!");
echo $conv; #To keimeno sou edo!
?>
I'm using DBAL and I want to execute multiple insert query. But I have the problem: bindValue() method not working in loop. This is my code:
$insertQuery = "INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES %s
ON DUPLICATE KEY UPDATE company=VALUES(company), user=VALUES(user)";
for ($i = 0; $i < count($data); $i++) {
$inserted[] = "(':number', ':company', ':user')";
}
$insertQuery = sprintf($insertQuery, implode(",", $inserted));
$result = $db->getConnection()->prepare($insertQuery);
for ($i = 0; $i < count($data); $i++) {
$result->bindValue($data[$i]["number"]);
$result->bindValue($data[$i]["company"]);
$result->bindValue($data[$i]["user"]);
}
$result->execute();
As result I received one-line table with fields: :number, :company, :user.
What am I doing wrong?
Thanks a lot for any help!
The problem you're having is that your binding has no way to determine to which placeholder it should be doing the binding with. To visualize it better, think on the final DBAL query you're generating:
INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES
(':number', ':company', ':user'),
(':number', ':company', ':user'),
(':number', ':company', ':user');
When you do the binding, you're replacing all the parameters at the same time, ending up with a single row inserted.
One possible solution would be to give different parameter names to each row and then replace each one accordingly.
It would look like something similar to this:
public function randomParameterName()
{
return uniqid('param_');
}
...
$parameters = [];
for ($i = 0; $i < count($data); $i++) {
$parameterNames = [
'number' => $this->randomParameterName(),
'company' => $this->randomParameterName(),
'user' => $this->randomParameterName(),
];
$parameters[$i] = $parameterNames;
$inserted[] = sprintf("(':%s', ':%s', ':%s')",
$parameterNames['number'],
$parameterNames['company'],
$parameterNames['user']
);
}
$insertQuery = sprintf($insertQuery, implode(",", $inserted));
$result = $db->getConnection()->prepare($insertQuery);
foreach ($parameters as $i => $parameter) {
$result->bindValue($parameter['number'], $data[$i]["number"]);
$result->bindValue($parameter['company'], $data[$i]["company"]);
$result->bindValue($parameter['user'], $data[$i]["user"]);
}
You could probably extend your $data variable and incorporate the new parameter names into it. This would remove the need of yet another array $parameters to hold reference to the newly created parameter names.
Hope this helps
There is another alternative:
$queryStart = "INSERT INTO {$tableName} (" . implode(', ', array_keys($buffer[0])) . ") VALUES ";
$queryRows = $params = $types = [];
foreach ($rowBuffer as $row) {
$rowQuery = '(' . implode(', ', array_fill(0, count($row), '?')) . ')';
$rowParams = array_values($row);
list($rowQuery, $rowParams, $types) = SQLParserUtils::expandListParameters($rowQuery, $rowParams, $types);
$queryRows[] = $rowQuery;
$params = array_merge($params, $rowParams);
}
$query = $queryStart . implode(', ', $queryRows);
$connection->executeQuery($query, $params, $types);
I am using Gravity Forms (1.8.9) and Wordpress (3.9.1)
I have a form field as a datepicker on my site, and I want to allow submissions only if the user is 21 or older to be able to submit the form.
I was originally using the following code. It only worked for a single form for a while, but then it stopped working completely: http://lanche86.com/gravity-forms-18-years-old-verification/
I would like to be able to use the same code on different forms. Help!
You can do something like this:
add_filter("gform_field_validation_1_1", "dob_validate", 10, 4);
function dob_validate($result, $value, $form, $field){
//Check if dob field matches required age
if ($result["is_valid"]){
// this the minimum age requirement we are validating
$minimum_age = 18;
// calculate age in years like a human, not a computer, based on the same birth date every year
$age = date('Y') - substr($value, 6, 4);
if (strtotime(date('Y-m-d')) - strtotime(date('Y') . '-' . substr($value, 0, 2) . '-' . substr($value, 3, 2)) < 0) {
$age--;
}
if( $age < $minimum_age ){
$result["is_valid"] = false;
$result["message"] = "Sorry, you must be at least $minimum_age years old. You're $age years old.";
}
}
//Check if dob field is empty
if(empty($value)){
$result["is_valid"] = false;
$result["message"] = "This field is required.";
}
return $result;
}
I'm using Gravity Forms 1.8.8 and latest Wordpress and works as desired. Screenshot:
You can also edit this according to form and field:
add_filter("gform_field_validation_1_1", "dob_validate", 10, 4);
Where gform_field_validation_1_1 is for form 1 and field 1. If your forms id is 8 and field number is 2, you can change it to gform_field_validation_8_2.
You can also add that same filter multiple times for multiple forms and fields without recreating the dob_validate function.
If anybody still needs to implement this, try this:
add_filter( 'gform_field_validation_1_1', function ( $result, $value, $form, $field ) {
if ( $result['is_valid'] ) {
if ( is_array( $value ) ) {
$value = array_values( $value );
}
$date_value = GFFormsModel::prepare_date( $field->dateFormat, $value );
$today = new DateTime();
$diff = $today->diff( new DateTime( $date_value ) );
$age = $diff->y;
if ( $age < 18 ) {
$result['is_valid'] = false;
$result['message'] = 'Underage';
}
}
return $result;
}, 10, 4 );
It's on Gravity Forms Documentation: https://docs.gravityforms.com/gform_field_validation/#13-date-field-age-validation
Current I have WP code like this. I need to make it translateable by poedit. How do I wrap the code to make it work? Im not sure which method is use for this case. Some thing like:
<?php my_e( 'Total sales' ); ?> or __('Total sales', 'my')
This is the code. I need to translate ["Sales amount"], ["Number of sales"]
foreach ($results as $result) {
$date = $result->formatted_post_date;
$statistics[$date]["Sales amount"] += $wp_list_table->column_total_sales($result->ID);
$statistics[$date]["Number of sales"]++;
$statistics[$date]["date"] = $date;
$max_number_of_sales = max(array($max_number_of_sales,$statistics[$date]["Number of sales"] )); }
Thank you for help
You have to use __('string','textdomain') to assign a translated string to some variable. And _e('string','textdomain') to echo a translated string. See I18n_for_WordPress_Developers.
Two observations:
you'll not be able to translate array keys, see php.net/manual/en/language.types.array.php
what you're doing seems wrong. I'd do it like:
$sales_amount = 0;
$sales_number = 0;
foreach ($results as $result) {
$sales_amount += $wp_list_table->column_total_sales($result->ID);
$sales_number++;
$date = $result->formatted_post_date;
$statistics[$date]["sales_amount"] = $sales_amount;
$statistics[$date]["sales_number"] = $sales_number;
}
echo __( 'Sales Amount', 'my' ) . $sales_amount;
Given the following select list for the "field_priority" field, how can I display the label, given the key (e.g. 0, 1, 3)?
0|Low
1|Medium
2|High
3|Urgent
You can get it nice and quickly using field_info_field():
$key = 0; // Or whatever
$field = field_info_field('field_priority');
$label = $field['settings']['allowed_values'][$key];
There's a whole host of field functions in field.info.inc, they're very handy.
I would suggest to use list_allowed_values() instead of searching the array yourself:
$field = field_info_field('field_priority');
$options = list_allowed_values($field);
$label = $values[$key];
Or in short:
$label = list_allowed_values(field_info_field('field_priority'))[$key];