How to attach a uploaded video to post from front end

Hello am building a user generated content sharing theme

This is my code for video submission from front end

But this code only allows to add videos from a url

How can i allow them to upload videos and then attach it to post

Heres the code !

 <?php
/*
 Template Name: Submit Content video Template
*/

// if you are not using this in a child of Twenty Eleven, you need to replicate the  html structure of your own theme.

?>
<?php include("Header-submit-vid.php"); ?>




            <div id="primary">
        <div id="content" role="main">
<?php 


if(isset($_POST['new_post']) == '1') {
 $post_title = $_POST['post_title'];
  $post_category = 'todays_post';
 $post_content = $_POST['post_content'];
$tags = $_POST['post_tags'];
$winerating = $_POST['winerating'];






 $new_post = array(
  'ID' => '',
  'post_author' => $user->ID, 
  'post_category' => array($post_category),
  'post_content' => $post_content,
  'tags_input'    =>  array($tags),
  'post_title' => $post_title,
  'post_category' =>   array($_POST['cat']),  //
  'post_status' => 'publish',
  'post_type' => 'post',
   'winerating' => $winerating
);


 $post_id = wp_insert_post($new_post);




    if (!function_exists('wp_generate_attachment_metadata')){
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }
     if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                return "upload error : " . $_FILES[$file]['error'];
            }
            $attach_id = media_handle_upload( $file, $post_id );
        }   
    }
 if ($attach_id > 0){
$post = get_post($post_id,'ARRAY_A');
$image = wp_get_attachment_image_src( $attach_id, 'large' );
 $image_tag = '<p><a href="'.$image[0].'" rel="lightbox"  title="<?php the_title();? >" >   <img src="'.$image[0].'" width="'.$image[1].'" height="'.$image[2].'" /></a></p>';

 //add image under the content
 $post['post_content'] = $image_tag . $post['post_content'];

 //add image above the content
 //$post['post_content'] = $post['post_content'] . $image_tag;



  $post_id =  wp_update_post( $post );

 }





 // This will redirect you to the newly created post
  $post = get_post($post_id);
    wp_redirect( get_permalink($post_id));
  exit();

     }      
   ?>
    <html>
 <head>
 <title>Add Your Funny Video</title>
<script src="/ajax/libs/jquery/1/jquery.min.js"></script>

 <!--[if IE]>
 <script src="/svn/trunk/html5.js"></script>
<![endif]-->
<style>
 article, aside, figure, footer, header, hgroup,
 menu, nav, section { display: block; }
 #x { display:none; position:relative; z-index:200; float:right}
 #previewPane { display: inline-block; }
</style>

<meta charset=utf-8 />

<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

</head>
<body>

  <  form method="post" action="" enctype="multipart/form-data" name="myform">

 <p>
<br />

    <!----------<Video Url>----->




      <span style="font-size: 15px;font-weight: bold;position: relative;right:  -93px;">Enter Your Video Url Here :-</span>
    <input type="url" name="post_content" size="53" required="required" id="text-desc" class="rounded" style="position: relative;right: -86px;">

       <!----------</Video Url>----->


      <br /> <br />
      <!----------<Post Title>----->

   <span style="font-size: 15px;font-weight: bold;position: relative;right: -93px;">Post Title</span><br />
   <input type="text" required="required" name="post_title" size="53" id="input-title" placeholder="Add A Title Here" class="rounded" style="position: relative;right: -86px;">
  <br>
  <!----------</Post Title>----->
  <br/>
     <!--<post tags>-->

<fieldset class="tags">

    <label for="post_tags">
<span style="font-size: 15px;font-weight: bold;position: relative;right: -93px;">Post Tags :-</span></label><br/>

    <input  type="text" class="rounded" placeholder="Add Some Tags To Get Higher  Votes"value="" size="53" tabindex="35" name="post_tags" id="post_tags"  style="position: relative;right: -86px;">

</fieldset>
    <!--</post tags>-->
     </br>
   <!-- <post Category> -->

<fieldset class="category">

    <label for="cat">
    <span style="font-size: 15px;font-weight: bold;position: relative;right: -93px;">Select Your Post Category</span></h4></label><div style="position:  relative;right: -85px;"><?php wp_dropdown_categories( 'tab_index=10&taxonomy=category&hide_empty=0' ); ?></div>


  </fieldset>
<!-- </post Category> -->



     <input type="hidden" name="new_post" value="1" /> <br>
    <div style="Float:right;"><input class="submitbutton" type="submit" name="submit" value="Add"  /><br><br></div>
      </form>

 </body>
      </div></div></div>
   <!--// New Post Form -->
  <div style="display:inline-block;margin-top:-72px;"><?php include("sidebar-submit.php"); ?></div>
     <div style="position:relative"><?php get_footer(); ?></div>

6 Answers
6

You could use wp_insert_attachment to upload a video file into the media library and then attatch it to the newly created post.

See http://codex.wordpress.org/Function_Reference/wp_insert_attachment

But it would be wise to have a few conditional statements in place to at least check for video size and type otherwise your site would be open to abuse.

Leave a Comment