WordPress.org

Ready to get started?Download WordPress

Forums

Custom Fields PHP NOOB! (8 posts)

  1. jemsz
    Member
    Posted 1 year ago #

    Gretings all.

    Right now i use wordpress and in my post i can add a Video by entering the Youtube address into a Custom field called "video_url" and then it outputs the video on my site wich works easy, here it is:

    <div class="video">
    
          <html><center>
    <?php
    $meta_value = get_post_meta($post->ID, 'video_url', true);
    if($meta_value){
    $url = parse_url($meta_value);
    $id = 0;
    if($url['host'] == 'youtu.be'){$id = ltrim($url['path'],'/');}
    else if(strpos($url['path'],'embed') == 1){$id = end(explode('/',$url['path']));}
    else{parse_str($url['query']);$id = $v;}
    echo '<p><iframe width="540" height="355" frameborder="1" allowfullscreen="true"  src="http://www.youtube.com/embed/'.$id.
    '?version=3&theme=light&fs=1&rel=0&cc_load_policy=1&iv_load_policy=1&modestbranding=1" type="application/x-shockwave-flash" allowScriptAccess="always" allowfullscreen="true" width="600" height="355">
    </iframe></p>';
    }
    
    ?>
    
    </div>

    However i am tring to add this Modification here in wich adding the video link custom field will also bring back the youtube video title and descripiton,

    <?php
    $video_id = ’48J_DIZBNyE’;
    $video_info = simplexml_load_file(‘http://gdata.youtube.com/feeds/api/videos/’.$video_id.’?v=1′);
    echo $video_info->title . ‘<hr>’; // title
    echo $video_info->content; // description
    ?>

    Could anyone please help me implement the bottom code with the top piece of code !!

    Mucg respect

    Thanks

  2. Shaun Scovil
    Member
    Posted 1 year ago #

    If I understand correctly, you want your post to also show the video title and description if the custom field video_link has ANY value?

    Assuming that is correct, and assuming your method of retrieving the title and description is correct, this should work:

    <?php
    $meta_value = get_post_meta( $post->ID, 'video_url', true );
    if ( $meta_value ){
        $url = parse_url( $meta_value );
        $video_id = 0;
        if ( $url['host'] == 'youtu.be' ) {
            $video_id = ltrim( $url['path'], '/' );
        } elseif ( strpos( $url['path'], 'embed' ) == 1 ) {
            $video_id = end( explode( '/', $url['path'] ) ) ;
        } else {
            parse_str( $url['query'] );
            $video_id = $v;
        }
        echo '<p><iframe width="540" height="355" frameborder="1" allowfullscreen="true"  src="http://www.youtube.com/embed/' . $video_id .
            '?version=3&theme=light&fs=1&rel=0&cc_load_policy=1&iv_load_policy=1&modestbranding=1" type="application/x-shockwave-flash" allowScriptAccess="always" allowfullscreen="true" width="600" height="355">
            </iframe></p>';
    }
    
    $video_link = get_post_meta( $post->ID, 'video_link', true );
    if ( $video_link && isset( $video_id ) ) {
        $video_info = simplexml_load_file( 'http://gdata.youtube.com/feeds/api/videos/' . $video_id . '?v=1' );
        echo $video_info->title . '<hr>'; // title
        echo $video_info->content; // description
    }
    ?>
  3. jemsz
    Member
    Posted 1 year ago #

    Shaun, cheers for the reply. I live in Australia so the times reply time is a bit off :P Yes you understand my idea clearly however instead of video_link showing the Title and Description its video_url that i post into the you-tube web address and then i would like it to show the title and description in the appropriate fields, thanks alot mate ill try work it out

  4. jemsz
    Member
    Posted 1 year ago #

    Okay maybe what i am trying to achieve is abit outta my league lol.

    $video_url = get_post_meta( $post->ID, 'video_url', true );
    if ( $video_url && isset( $video_id ) ) {
        $video_info = simplexml_load_file( 'http://gdata.youtube.com/feeds/api/videos/' . $video_id . '?v=1' );
        echo $video_info->title . '<hr>'; // title
        echo $video_url->content; // description
    }

    It all works fine i changed video_link to video_url and it works, it automatically gets the video description and title and puts it into the CSS area were my Youtube video is placed.... Instead i just want the video Title to go to the word press post Title area and the youtbe description to go to the Post main article area...(content)... however the title and description just go to the video div if this makes sense, cheers mate if you have any info on how to achieve it

  5. jemsz? Off topic but could you please not create any more duplicate threads? Those get deleted when found.

  6. Shaun Scovil
    Member
    Posted 1 year ago #

    I see what you are trying to achieve. That is a little bit more complicated, as you are trying to change the values of other input fields in WP-Admin, based on the value of your custom field.

    To do that, you would need to create a custom meta box...and I would recommend creating a custom post type called 'Videos', so you can have more control over the interface while still keeping your standard Post editor in tact.

    Unfortunately, this is beyond the scope of the help forums. You would either need to do some reading and learn how to create what you are looking for, or hire someone who can. But before you do that, I would recommend searching the plugin repository for an out-of-the-box solution.

  7. Shaun Scovil
    Member
    Posted 1 year ago #

    In particular, this plugin might be of interest.

  8. jemsz
    Member
    Posted 1 year ago #

    Thanks very much for taking the time to respond!
    I shall learn abit more and look into the following plugins, cheers

Topic Closed

This topic has been closed to new replies.

About this Topic