I currently have a Gravity Form on the frontend of my site. Upon submission, a post is created.
The form includes the youtube uploader from the Gravity Forms Advanced File Uploader.
Once uploaded, I need the youtube video ID saved as a custom field on the post.
Testing Environment: http://beamsco.com/videouploader/uploader/
Helpful links:
- https://wordpress.org/support/topic/adding-video-id-to-custom-field-gravity-forms-advanced-file-uploader/
- https://wordpress.org/support/topic/get-youtube-video-id-hook/
After uploading, this is the value in the db table wp_rg_lead_detail_long
a:1:{i:0;a:7:{s:17:"wp_attachement_id";i:7;s:9:"mime_type";s:9:"video/mp4";s:9:"file_path";s:103:"/home/beamrmc/public_html/videouploader/wp-content/uploads/2017/02/1e1a3d4ab45d855efb997a5259fab7ba.mp4";s:5:"title";s:38:"Uploaded File: Form 1, Entry 1, File 0";s:11:"description";s:88:"This video was uploaded from your website, here are the details: Form 1, Entry 1, File 0";s:8:"video_id";s:11:"9k6lRoqQzBs";s:9:"video_url";s:43:"https://www.youtube.com/watch?v=9k6lRoqQzBs";}}
Francisco Javier Carazo Gil answers:
Hello,
Could you send me a link to see how the video is uploaded?
Maybe we can take it from the string with some regular expression.
mywpgirl comments:
Sorry for the delay. Had to set up a testing environment.
http://beamsco.com/videouploader/uploader/
Francisco Javier Carazo Gil comments:
Ok but you upload the movie to your site, not to YouTube.
Isn't it?
mywpgirl comments:
No. It uploads directly to youtube and (via plugin options) is not saved to my site. The video ID is saved to the database and can be retrieved using the code in the Helpful Links I posted. My problem is, I'm unsure of where, within those variable arrays, the video's ID lies. If you can figure THAT part out, I can update the meta data with the ID.
Francisco Javier Carazo Gil comments:
add_action(‘prso_gform_youtube_uploader_pre_update_meta’, 'cod_prso_gform_youtube_uploader_pre_update_meta', 10, 2 );
function cod_prso_gform_youtube_uploader_pre_update_meta( $field_values, $data){
var_dump( $field_values );
var_dump( $data );
}
Could you show me the values present in field_values?
mywpgirl comments:
Unsure if this notifies you but I've edited my original question and I believe the info you asked for is now in the question.
Not sure if there should be but, there is no output for the code in your comment.
Francisco Javier Carazo Gil comments:
There is no output because this code does not render in front-end.
Coud you show me this values? Maybe you can write them in a log.
Or try it:
function cod_prso_gform_youtube_uploader_pre_update_meta( $field_values, $data){
var_dump( $field_values );
var_dump( $data );
die();
}
mywpgirl comments:
Sorry. Don't know what you want me to do with this code.
Francisco Javier Carazo Gil comments:
When you execute this code, the trigger will fire and then we will see the data you are asking for...
mywpgirl comments:
Where do I put it and view it if it doesn't output on the front end? (Again, I apologize for the delay. This system is no longer sending me notifications of your comments.)
Francisco Javier Carazo Gil comments:
If you put it in your code and you make a form submit, with the "die()" maybe it is shown.
If not, use a debug.log please: https://www.elegantthemes.com/blog/tips-tricks/using-the-wordpress-debug-log
mywpgirl comments:
Added code. Successfully uploaded video. Received no output.
Debug:
[12-Feb-2017 18:09:26 UTC] WordPress database error Table 'beamrmc_wp419.wpxa_rg_lead_meta' doesn't exist for query SELECT meta_value FROM wp_rg_lead_meta WHERE lead_id=3 AND meta_key='_encrypted_fields' made by require('wp-blog-header.php'), wp, WP->main, do_action_ref_array, WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, GFForms::maybe_process_form, GFFormDisplay::process_form, GFFormDisplay::handle_submission, GFFormsModel::get_lead, GFFormsModel::build_lead_array, GFFormsModel::is_encrypted_field, GFFormsModel::get_encrypted_fields, gform_get_meta
Francisco Javier Carazo Gil comments:
Yes but, have you prepared the log? Have you done the write_log?
I cannot see any var dumped there.
function cod_prso_gform_youtube_uploader_pre_update_meta( $field_values, $data){
write_log( $field_values );
write_log( $data );
}
mywpgirl comments:
I put your latest code into function.php. Still no output.
Debug:
[12-Feb-2017 18:20:58 UTC] WordPress database error Table 'beamrmc_wp419.wpxa_rg_lead_meta' doesn't exist for query SELECT meta_value FROM wpxa_rg_lead_meta WHERE lead_id=4 AND meta_key='_encrypted_fields' made by require('wp-blog-header.php'), wp, WP->main, do_action_ref_array, WP_Hook->do_action, WP_Hook->apply_filters, call_user_func_array, GFForms::maybe_process_form, GFFormDisplay::process_form, GFFormDisplay::handle_submission, GFFormsModel::get_lead, GFFormsModel::build_lead_array, GFFormsModel::is_encrypted_field, GFFormsModel::get_encrypted_fields, gform_get_meta
Francisco Javier Carazo Gil comments:
Maybe your hosting is directing the log to another file.
I would need more time (and direct access to your site) to see it.
Sorry but withot this data we can't do more.
mywpgirl comments:
Made some code changes. Here's what it wrote to the log.
Array
(
[2] => Array
(
[0] => Array
(
[wp_attachement_id] => 23
[mime_type] => video/mp4
[file_path] => /home/beamrmc/public_html/videouploader/wp-content/uploads/2017/02/50142c5e7ed14a7d05e7533490a42b9d.mp4
[title] => Uploaded File: Form 1, Entry 6, File 0
[description] => This video was uploaded from your website, here are the details: Form 1, Entry 6, File 0
[video_id] => F94NxDh09cM
[video_url] => https://www.youtube.com/watch?v=F94NxDh09cM
)
)
)
Francisco Javier Carazo Gil comments:
Ok so you have there the video ID in YouTube: [video_id] => F94NxDh09cM
Do you know how to get it into a variable?
mywpgirl comments:
No. And I need to make this ID save to custom fields of the post that was created when uploading the video & submitting the form.
mywpgirl comments:
This is the code I'd like to use. If you can fix the ??? I think it will work.
add_action('prso_gform_youtube_uploader_pre_update_meta', 'prso_get_youtube_id');
function prso_get_youtube_id( $field_values, $form_data ) {
$postID = ???;
$videoID = ???;
update_post_meta($postID, 'vidid', $videoID);
}
*********************
This is field_values
Array
(
[2] => Array
(
[0] => Array
(
[wp_attachement_id] => 23
[mime_type] => video/mp4
[file_path] => /home/beamrmc/public_html/videouploader/wp-content/uploads/2017/02/50142c5e7ed14a7d05e7533490a42b9d.mp4
[title] => Uploaded File: Form 1, Entry 6, File 0
[description] => This video was uploaded from your website, here are the details: Form 1, Entry 6, File 0
[video_id] => F94NxDh09cM
[video_url] => https://www.youtube.com/watch?v=F94NxDh09cM
)
)
)
***********
This is form_data
Array
(
[gforms_entry] => Array
(
[id] => 6
[form_id] => 1
[date_created] => 2017-02-12 18:40:08
[is_starred] => 0
[is_read] => 0
[ip] => 192.183.79.130
[source_url] => http://beamsco.com/videouploader/uploader/
[post_id] => 22
[currency] => USD
[payment_status] =>
[payment_date] =>
[transaction_id] =>
[payment_amount] =>
[payment_method] =>
[is_fulfilled] =>
[created_by] => 1
[transaction_type] =>
[user_agent] => Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
[status] => active
[1] => test6
[2] =>
)
[gforms_form] => Array ........................... (removed)
Francisco Javier Carazo Gil comments:
Not sure which post_id are you referring, in this code this is not clear.
To take videoID:
foreach( $field_values as $field ){
foreach( $field as $f ){
foreach( $f as $key => $value ){
if( $key == 'video_id' )
$videoID = $value;
break;
}
}
}
mywpgirl comments:
This is my current code, which writes the videoID to the debug log:
add_action('prso_gform_youtube_uploader_pre_update_meta', 'prso_get_youtube_id', 10, 2);
function prso_get_youtube_id( $field_values, $form_data ) {
foreach( $field_values as $field ){
foreach( $field as $f ){
foreach( $f as $key => $value ){
if( $key == 'video_id' )
$videoID = $value;
break;
}
}
}
write_log( $videoID );
write_log( $postID );
}
********************
Still need this part to set the $postID
foreach( $form_data as $post_data ){ ?? }
************************
write_log($form_data) gives me the following (which includes [post_id] => 22, which is the $postID)
Array
(
[gforms_entry] => Array
(
[id] => 6
[form_id] => 1
[date_created] => 2017-02-12 18:40:08
[is_starred] => 0
[is_read] => 0
[ip] => 192.183.79.130
[source_url] => http://beamsco.com/videouploader/uploader/
[post_id] => 22
[currency] => USD
[payment_status] =>
[payment_date] =>
[transaction_id] =>
[payment_amount] =>
[payment_method] =>
[is_fulfilled] =>
[created_by] => 1
[transaction_type] =>
[user_agent] => Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
[status] => active
[1] => test6
[2] =>
)
[gforms_form] => Array ........................... (removed)
Francisco Javier Carazo Gil comments:
$postID = $form_data[ 'gforms_entry' ]['post_id'];
This should work.
mywpgirl comments:
Thank you. Your last comment was the exact thing I needed in order to get my brain to work and understand.
Final, working code:
add_action('prso_gform_youtube_uploader_pre_update_meta', 'prso_get_youtube_id', 10, 2);
function prso_get_youtube_id( $field_values, $form_data ) {
$postID = $form_data[ 'gforms_entry' ]['post_id'];
$videoID = $field_values[2][0]['video_id'];
update_post_meta($postID, 'vidid', $videoID);
}