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.