Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Trying to add Custom Field data into a PHP function, but isn’t working.

  • This topic is empty.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #45479
    asiek
    Participant

    I have a custom field for video format posts, where you paste the video’s ID in. Then I have a PHP function to grap the View count for youtube videos.

    I tried placing:

    ID, ‘v_url’, true); ?>

    into

    $video_ID = ‘your-video-ID’;
    $JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json”);
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{‘entry’}->{‘yt$statistics’}->{‘viewCount’};
    echo $views;
    ?>

    Where “your-video-ID” is located.

    Here is what the full code looked like afterwards

    $video_ID = ‘ID, ‘v_url’, true); ?>’;
    $JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json”);
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{‘entry’}->{‘yt$statistics’}->{‘viewCount’};
    echo $views;
    ?>

    Then I get this error message:

    “**Parse error: syntax error, unexpected T_STRING in /home/example/public_html/test/wp-content/themes/example/format-video.php on line 23**”

    What exactly is the issue here?
    I’m not too familiar with php so I’m lost.
    Please help.

    #138509
    chrisburton
    Participant

    I’m no expert but perhaps it is this:

    `’ID, ‘v_url’, true); ?>’`

    Shouldn’t it be:

    `”ID, ‘v_url’, true); ?>”`

    #138513
    asiek
    Participant

    @chrisburton I’ve tried that :/
    The error disappears but the View Count doesn’t appear at all.

    #138514
    chrisburton
    Participant

    Then I think something is wrong with your code that’s trying to retrieve that data. Unfortunately this isn’t my area so I’m not sure I can help further. At least removing the error helps.

    Can you post a link?

    #138515
    asiek
    Participant

    @chrisburton Appreciate your help.

    #138516
    Alen
    Participant

    You are already within php code block there is no need to nest `ID, ‘v_url’, true);
    $JSON = file_get_contents(“https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json”);
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{‘entry’}->{‘yt$statistics’}->{‘viewCount’};
    echo $views;

    ?>

    #138517
    Alen
    Participant

    Also try printing out the data that’s returned to see if anything is in fact being returned. So do something like:

    ID, ‘v_url’, true);
    print_r($video_ID);

    ?>

    It is possible that `$post` is not defined, try using `get_post_meta( get_the_ID(), ‘v_url’, true);`

    #138508
    asiek
    Participant

    @alenabdula It works now :D
    And due to my lack of php knowledge I wouldn’t even know where to place the 2nd snippet you’ve posted :/
    But thank you so much for your help!

    #138518
    Alen
    Participant

    You would use it where you have it now on the first line, the only diff. is the first argument! `$post->ID` to `get_the_ID()`. But if it works now, don’t fix what’s not broken :)

    #138522
    asiek
    Participant

    @alenabdula Thank you so much! :)
    I will try the

    print_r($video_ID);

    , Just to check it out.I dislike the fact that the PHP function doesn’t add a coma within the views when needed though lol :( But I’m glad it works now!

    #138523
    asiek
    Participant

    @alenabdula Quick question. Does that line of code make sure the video id is in place? I just had a small issue where the view count didn’t appear at all. I refreshed it a few times then the view count appeared again..

    #138524
    Alen
    Participant

    [From php.net](http://php.net/manual/en/function.print-r.php) –> print_r — Prints human-readable information about a variable.

Viewing 12 posts - 1 through 12 (of 12 total)
  • The forum ‘Back End’ is closed to new topics and replies.