I'm using Ninja Form plugin in my WordPress installation.
My form has 3 input text fields.
I need, after pressing the submit button, to validate one of this input by checking if the entered value exists in a custom table in my database.
If the value doesn't already exists nothing should happen (Ninja Form save the form), if it exists I need to add a Ninja Form error and let the user change the input in order to save the form with a new value.
How can I hook the submit action? How can I get in this hook the input value I need? How can I add a Ninja Form error if the value exists in order to prevent the form save?
You can do this using the ninja_forms_submit_data hook. There you can access the value of a field using its id through the variable $form_data. When adding an error message for the field to $form_data['errors'] the form will not be saved.
Like this (in functions.php):
add_filter('ninja_forms_submit_data', 'custom_ninja_forms_submit_data');
function custom_ninja_forms_submit_data($form_data)
{
$field_id = 2;
$field_value = $form_data['fields'][$field_id]['value'];
$exists = true; // Check your database if $field_value exists
if($exists)
{
$form_data['errors']['fields'][$field_id] = 'Value already exists';
}
return $form_data;
}
Related
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.
I need to create a action/hook to get a a form element value when a user submit the user registration form.
My registration form has the elements:
user
password
nif
I need to creat an action to get the value of 'nif'. I found this which should be my starting point, but I don't know how to retrieve the value of 'nif'.
add_action( 'user_register', 'registration_get_nif', 10, 1 );
function registration_get_nif( $user_id ) {
// HERE. GET VALUE OF 'nif'....
}
Presuming your form has an input with the name nif you can use PHP's $_POST variable.
$nif = $_POST['nif'];
You'll want to heavily sanitise that variable, as it can be manipulated by the browser.
Context
We've created (I actually inherited code) few forms with Gravity forms. One of the form is used to manage profil informations.
Description
The form doesn't update entries but instead creates new entries. Therefore the result is that in the profil page we have the initial information if we modify and save, after reload nothing changes. But, in the database, a new entry is created.
Tried solution
Setting a field to be unique doesn't fix it but returns an error saying that the value used in the field has already been used.
Try updating the entry ID before the entry is saved. This example from the gform_entry_id_pre_save_lead documentation will help:
add_filter( 'gform_entry_id_pre_save_lead', 'my_update_entry_on_form_submission', 10, 2 );
function my_update_entry_on_form_submission( $entry_id, $form ) {
$update_entry_id = rgpost( 'my_update_entry_id' );
return $update_entry_id ? $update_entry_id : $entry_id;
}
I got some plugins that auto-generates custom fields. Does anyone know how to automatically remove empty custom fields from post when I press "publish"?
A check I can put in my functions.php that does a check and removes the custom field if there is no value?
Here is the solution:
add_action('save_post','my_cf_check');
function my_cf_check($post_id) {
// verify this is not an auto save routine.
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
//authentication checks
if (!current_user_can('edit_post', $post_id)) return;
//obtain custom field meta for this post
$custom_fields = get_post_custom($post_id);
if(!$custom_fields) return;
foreach($custom_fields as $key=>$custom_field):
//$custom_field is an array of values associated with $key - even if there is only one value.
//Filter to remove empty values.
//Be warned this will remove anything that casts as false, e.g. 0 or false
//- if you don't want this, specify a callback.
//See php documentation on array_filter
$values = array_filter($custom_field);
//After removing 'empty' fields, is array empty?
if(empty($values)):
delete_post_meta($post_id,$key); //Remove post's custom field
endif;
endforeach;
return;
}
wp_insert_post_data is a hook that happens before a database write in the admin panel. You can probably use that to call a function that checks for data, then strips out any empty custom field entries.
Or, you could use the_content hooks on the front end to strip out empty custom fields before they get displayed to the user.
Or, if you have control of the theme files, you can just test for data in your custom fields before displaying them.
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'];