Please help me to create this function

ibadullah

Junior Member
Joined
Jul 10, 2011
Messages
106
Reaction score
9
Hello,

I'm trying to create a custom field in WordPress to add Vine.co videos.

I found the guide which demonstrate how to add a custom meta box for YouTube videos only.
Code:

http://www.iamclarence.com/blog/201...dding-a-youtube-meta-box-to-pagepost-editing/

Can anyone help me to change that metabox from YouTube Videos to Vine.co Videos?

This is the function for Vine.co Videos

PHP Code:
function vine($id) { // gets the raw .mp4 url from the vine id
$vine = file_get_contents("http://vine.co/v/{$id}");
preg_match('/property="twitter:player:stream" content="(.*?)"/', $vine, $matches);
return ($matches[1]) ? $matches[1] : false;
}
Another function I want to merge is to get the vine featured image and set it automatically in WordPress using the guide below?
https://halgatewood.com/php-get-vine...deo-thumbnail/

If anyone can help it will be greatly appreciated. Thanks in advance.
 
Just copy and paste the code they have there. Make the following changes:

Code:
// original
preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $featuredVideoURL, $youTubeMatch);

if($youTubeMatch[1]){
    // return youtube embed code
}else{
    return null;
}

// new
//   (for readability, replace all instances of "youTubeMatch"
//      with "vineMatch"; leave everything else the same
preg_match('YOUR_REGEX_HERE', $featuredVideoURL, $vineMatch);

if($vineMatch[1]){
    // return vine embed code
}else{
    return null;
}

Don't overthink it, just replace the YouTube output with Vine output and change the variable names to improve readability. That should be enough to get you started on the right track.
 
First of all I would like to say thank you for helping me.

I tried to replace the things as you mentioned above but still something is wrong in my code maybe the vine embed code or displaying output code.

Here is the full function I implemented on my functions.php

PHP:
add_action( 'add_meta_boxes', 'featuredVideo_add_custom_box' );
add_action( 'save_post', 'featuredVideo_save_postdata' );

function featuredVideo_add_custom_box() {
    add_meta_box(  'featuredVideo_sectionid', 'Featured Video', 'featuredVideo_inner_custom_box', 'post', 'side' );
}

function featuredVideo_inner_custom_box( $post ) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'featuredVideo_noncename' );
 
    // show featured video if it exists
    echo getFeaturedVideo( $post->ID, 260, 120);   
 
    echo '<h4 style="margin: 10px 0 0 0;">URL [YouTube Only]</h4>';
     echo '<input type="text" id="featuredVideoURL_field"  name="featuredVideoURL_field" value="'.get_post_meta($post->ID,  'featuredVideoURL', true).'" style="width: 100%;" />';
}

function featuredVideo_save_postdata( $post_id ) {
 
    // check autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return;
 
    // check authorizations
    if ( !wp_verify_nonce( $_POST['featuredVideo_noncename'], plugin_basename( __FILE__ ) ) )
      return;
 
    // update meta/custom field
    update_post_meta( $post_id, 'featuredVideoURL', $_POST['featuredVideoURL_field'] );
}

function getFeaturedVideo($post_id, $width = 680, $height = 360) {
    $featuredVideoURL = get_post_meta($post_id, 'featuredVideoURL', true);
 
     preg_match('/property="twitter:player:stream" content="(.*?)"/', $featuredVideoURL, $vineMatch);

    if ($vineMatch[1])
         return '<iframe width="'.$width.'" height="'.$height.'"  src="http://www.vine.co/v/ '.$vineMatch[1].'></iframe>';
    else
        return null;
}

This is I placed in single.php file where I want to output the video

PHP:
<?php echo $the_video_output = getFeaturedVideo($post->ID, 708, 366); ?>
 
Dump the variables and post the results here.

Please use CODE brackets and not PHP brackets. The colors are hard on my eyes. :D
 
Back
Top