• I am using Gravity Forms on my WordPress website where people can share videos, and I want the title, description, etc. to be populated automatically by getting the data from the YouTube video. After browsing Google for a couple of hours, I can’t seem to get it to work. (I have 0 experience in php)

    All I was able to do is get the video ID from the URL by using this code:

    add_filter("gform_save_field_value", "save_field_value", 10, 4);
    function save_field_value($value, $lead, $field, $form){
            //if not the form with fields to encode, just return the unaltered value without checking the fields
            if(absint($form["id"]) <> 1) /* If the form is not ID 1 */
                    return $value;
    
            //array of field ids to encode
            $encode_fields = array(2); /* Field 2 is the video URL from which we need the video ID */
    
        parse_str( parse_url( $value, PHP_URL_QUERY ), $my_array_of_vars );
    
            //see if the current field id is in the array of fields to encode; encode if so, otherwise return unaltered value
            if(in_array($field["id"],$encode_fields))
                    return $my_array_of_vars['v']; /* Return the video ID i.e. the value after '&v=' */
            else
                    return $value;
    }

    I also found this to get the data from the video: https://stackoverflow.com/questions/10896233/how-can-i-retrieve-youtube-video-details-from-video-url-using-php

    Hope someone can help me, thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter xWafflecakes

    (@xwafflecakes)

    I’ve been searching Google for how to get the title from a video using PHP and I found this:

    <?php
    $video_id = 'y8Kyi0WNg40';
    $content = file_get_contents("https://youtube.com/get_video_info?video_id=" . $video_id);
    parse_str($content, $ytarr);
    echo $ytarr['title'];
    ?>

    So I tried to add it to my previous code (2 is the field ID of the video URL and 7 is the title):

    add_filter("gform_save_field_value", "save_field_value", 10, 4);
    function save_field_value($value, $lead, $field, $form){
    	global $id;
    
            //if not the form with fields to encode, just return the unaltered value without checking the fields
            if(absint($form["id"]) <> 1)
                    return $value;
    
            //array of field ids to encode
            $encode_fields = array(2,7);
    
    	parse_str( parse_url( $value, PHP_URL_QUERY ), $my_array_of_vars );
    	$id = $my_array_of_vars['v'];
    
    	$content = file_get_contents("https://gdata.youtube.com/feeds/api/videos/".$id);;
    	parse_str($content, $ytarr);
    	$title = $ytarr['title'];
    
            //see if the current field id is in the array of fields to encode; encode if so, otherwise return unaltered value
            if(in_array($field["id"],$encode_fields)){
    		if(absint($field["id"]) == 2)
                    	return $id;
    		if(absint($field["id"]) == 7)
    			return $videoTitle;
    		}
            else
                    return $value;
    }

    But the title doesn’t work… (The ID does work) What am I doing wrong?

    [ No bumping please. ]

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Gravity Forms is a commercial product and those are not supported in these forums.

    Try this link instead.

    https://www.gravityhelp.com/support

    As you are their customer I am sure they will have no problem supporting you there.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Gravity Forms: automatically populating fields by getting the data of a YouTube’ is closed to new replies.