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:
Get the ID of a youtube video and display it with a shortcode inside of a content template and store it in a another custom post field created with types to turn the ID into a shortcode then display on the page.

Solution:
You can do it using cred_save_data hook something like below:

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><u>hidden link</u></em>...
            if (empty($video_id[1]))
                $video_id = explode("/v/", $link); // For videos like <em><u>hidden link</u></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);
        }
    }
}

==> Please replace '12' to your CRED form ID in the above code.
==> Replace 'my_custom_video_field' to your field slug
==> Replace '__my_custom_video_field' to your field slug where we need to save youtube ID.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Nicholas 7 years ago.

Assisted by: Noman.

Author
Posts
#585787

Hello I'd like to get the ID of a youtube video and display it with a shortcode inside of a content template

I created a custom post field with toolset types called "youtube-video-1". Users paste a youtube url and then submit the form.
This outputs the iframe of the youtube video on the page.

Now I need your help to grab just the ID from the youtube video i.e hidden link
ID = _jb-YrYJPro

and store it in a another custom post field created with types to turn the ID into a shortocode i can then display on the page.

I'd appreciate any help and guidance.

Regards,
Nicholas

#585939

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Nicholas,

Thank you for contacting Toolset support. You can do it using cred_save_data hook something like below:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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><u>hidden link</u></em>...
			if (empty($video_id[1]))
				$video_id = explode("/v/", $link); // For videos like <em><u>hidden link</u></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);
        }
    }
}

==> Please replace '12' to your CRED form ID in the above code.
==> Replace 'my_custom_video_field' to your field slug
==> Replace '__my_custom_video_field' to your field slug where we need to save youtube ID.

Thank you

#585947

Very nice.

Thank you Noman.

Regards,
Nicholas