add_action('frm_after_create_entry', 'add_uploaded_file_alt', 30, 2);
add_action('frm_after_update_entry', 'add_uploaded_file_alt', 10, 2);
function add_uploaded_file_alt( $entry_id, $form_id ) {
if ( $form_id == 3 ) {
//(works) $media_ids = array ('64767','64764');
//(works) $alt_text = array ('test 1','test 2');
//however, $media_ids and $alt_text are empty if I
//try to get them using ($_POST['item_meta'][(field_id)])
//as per below
$media_ids = array ($_POST['item_meta'][88]);
$alt_text = array ($_POST['item_meta'][91]);
$img_meta = array_combine($media_ids, $alt_text);
foreach ($img_meta as $id => $alt) {
update_post_meta( $id, '_wp_attachment_image_alt', $alt );
}
}
}
The '(field_id)' is not a value. It's a placeholder that you substitute with the actual field id of your target field. The first character of PHP variables is always a dollar sign ($variable_name).
You can examine the contents of the PHP $_POST associative array using vardump() or the Kint debugger.
Another way to get the actual input name is to examine the page's source code in the browser using its built-in inspection tool.
Look at this example from the Firefox inspection tool:
The circled content is the input name. In this example the input name is item_meta[967]. When captured in the $_POST array, this will be $_POST[item_meta][967]. As a tip, always look for the name to determine what to extract from the $_POST. If this was a checkbox, it would have a third dimension. The input name would be item_meta[967][]. The $_POST variable would then be $_POST[item_meta][967][].
Ah, thank you Victor - the tip to look at the source code item_meta gave me a hint of what I am dealing with...
I've got a text input in a repeating field, so the item_meta details are like:
item_meta[89][i452][91]
It looks like the 89 is the repeater, 91 is the text field... that i452 IDs the specific field that's been repeated.
If I want to get an array of all of the values added to that text field (91) in the repeated field, (for example, if '1' is entered into the first text field, '2' in the repeated text field, and I want to get an array containing 1,2) how would I go about that since I don't know what the 'i' number will be?
Thanks again!
Chris
You have to loop through the $_POST array and add matching elements to a new array. Here's an example: https://stackoverflow.com/questions/21750478/retrieve-post-array-values
add_filter('frm_validate_field_entry', 'add_alt_text_form_images', 10, 4);Any additional help with getting this finished would be appreciated!
function add_alt_text_form_images($errors, $posted_field, $posted_value, $args){
if ( $posted_field->id == 14548 ) { // Please replace 14548 with the ID of your repeater field
$image_field_id=14550; // change 5844 to the ID of the file upload field $alt_text_field_id=14551; // change 14551 to the ID of the alt text field $vals= array(); if ( ! isset( $_POST['item_meta'][ $posted_field->id ] ) ) { return $errors; } foreach ( $_POST['item_meta'][ $posted_field->id ] as $row_num => $section_vals ) { if ( $row_num === 'form' ) { continue; } $uploadedimage = $_POST['item_meta'][ $posted_field->id ][$row_num][$image_field_id]; $alttext = $_POST['item_meta'][ $posted_field->id ][$row_num][$alt_text_field_id]; foreach ( (array)$uploadedimage as $id ) { if ( ! $id ) { continue; } update_post_meta( $id, '_wp_attachment_image_alt', $alttext ); } } } return $errors; }
Please login or Register to submit your answer