Problem: I have a CRED form where Users can supply the URL of a YouTube video. I would like to store the video ID in a custom field so I can use it later in a shortcode.
Solution:
Use the following cred_save_data function to parse the URL and determine the YT video ID, then save that value into a custom field on the post.
add_action('cred_save_data', 'cred_format_youtube_video_id',10,2); function cred_format_youtube_video_id($post_id, $form_data) { // if a specific form $forms = array(12,9876); if (in_array($form_data['id'], $forms)) { if (isset($_POST['my_custom_video_field'])) { $link = $_POST['my_custom_video_field']; $video_id = explode("?v=", $link); // http://www.youtube.com/watch?v=... if (empty($video_id[1])) { $video_id = explode("/v/", $link); // http://www.youtube.com/watch/v/.. } if (empty($video_id[1])) { $video_id = explode("/youtu.be/", $link); // https://youtu.be/... } $video_id = explode("&", $video_id[1]); // Deleting any other params $video_id = $video_id[0]; // here is your required video ID // update post meta with video ID update_post_meta($post_id, '__my_custom_video_field', $video_id); } } }
Change 12,9876 to be a comma-separated list of all the forms where this custom functionality should be applied.
Relevant Documentation:
http://php.net/manual/en/function.explode.php
http://php.net/manual/en/language.types.array.php
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 3 replies, has 2 voices.
Last updated by 6 years, 10 months ago.
Assisted by: Christian Cox.