Skip Navigation

[Resolved] Get the ID of a youtube video and display it with a shortcode

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 6 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

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 Nicholas 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#610405

Hello I created this ticket
https://toolset.com/forums/topic/get-the-id-of-a-youtube-video-and-display-it-with-a-shortcode/
some time ago.

Today I noticed that this code doesn't work for all links coming from the youtube mobile app and the youtube mobile website

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==12)
    {
        if (isset($_POST['my_custom_video_field']))
        {
            $link = $_POST['my_custom_video_field'];
            $video_id = explode("?v=", $link); // For videos like <em>hidden link</em>...
            if (empty($video_id[1]))
                $video_id = explode("/v/", $link); // For videos like <em>hidden link</em>..
              
            $video_id = explode("&", $video_id[1]); // Deleting any other params
            $video_id = $video_id[0]; // here is your required video ID
              
            // add it to saved post meta
            add_post_meta($post_id, '__my_custom_video_field', $video_id, true);
        }
    }
}

Mobile website: hidden link
Mobile app: hidden link

Do you know how to solve this problem?

Please help.

Nick

#610415

And I'd also like to know how to update the types custom field, in case the user changes the youtube URL with the CRED edit form.

Right now the ID stays the same and is not updating.

#610497

First, make sure the video ID is being saved correctly into the custom field. You will have to inspect the database to check this. My guess is that it is not, based on the algorithm Noman gave you. Adjust the code to interpret the additional URL formats, call update_post_meta instead of add_post_meta, and extend the conditional to apply this code to multiple CRED forms:

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);
        }
    }
}

Replace 9876 with the ID of the edit CRED form. Then test with the various URL formats and check to see if the custom field value is set correctly in the database. If so, then the CRED code is working correctly. You may need to adjust the front-end shortcode to display the video correctly based on that ID.

#610525

Thank you Christian. This did the trick 😉