Skip Navigation

[Closed] Youtube Video ID from any style of Youtube URL

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

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 4 years, 3 months ago.

Author
Posts
#1835129

Tell us what you are trying to do?
I have a project that requires getting the Youtube ID from the URL.

Is there any documentation that you are following?
Following this...
https://toolset.com/forums/topic/get-the-id-of-a-youtube-video-and-display-it-with-a-shortcode-2/

I also read all the examples in the cred_save_data hook documentation.

I can't get it working...

My field slug is simply "youtube-video", and I want to make it so any post containing the field "youtube-video" will check and convert the value of that field into the Youtube ID from the url. I want this to be done from any form or in backend post creation and edits.

Is there a similar example that we can see?
I have tried the augmentation of Christian's function below, and I don't get any php errors in the custom code but, it doesn't produce any result when outputting the field. Perhaps I'm not thinking about it correctly.

add_action('cred_save_data', 'cred_format_youtube_video_id',10,2);
function cred_format_youtube_video_id($post_id, $form_data)
{
if (isset($_POST['youtube-video']))
{
$link = $_POST['youtube-video'];
$video_id = explode("?v=", $link); // hidden link...
if (empty($video_id[1])) {
$video_id = explode("/v/", $link); // hidden link..
}
if (empty($video_id[1])) {
$video_id = explode("/youtu.be/", $link); // hidden link...
}

$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, '__youtube-video', $video_id);
}
}

I've tried a ton of variations on this, but the code above is the closest to the original, so we can start there. Again the only goal is to convert any conceivable Youtube URL into the ID. I'm open to any method, even if it's using an add_shortcode for outputting the value or passing it into another field.

#1835491

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You say that you want this to apply both to creating/editing posts from the front-end and from the backend, so the code you shared above wouldn't have any effect when saving posts on the backend as it uses the cred_save_data hook which fires when a front-end form is submitted.

I see two ways of implementing this.

The first is similar to the solution you shared above, in that when a post is saved with some value for the URL custom field, the code tries to extract the ID of the video and then saves it in another custom field (in the above example it's a hidden custom field with metakey '__youtube-video' rather than in a visible custom field shown in the backend editor). But rather than the cred_save_data hook, you should use the save_post hook which would be triggered regardless of whether the post was saved from the front-end or back-end: https://developer.wordpress.org/reference/hooks/save_post/

An alternative would be not to try this at the time a post is saved, but instead register a custom shortcode to output your existing URL field which does the ID extraction on demand and returns the ID instead of the URL. The logic would be largely similar to that in the above code.

The topic ‘[Closed] Youtube Video ID from any style of Youtube URL’ is closed to new replies.