WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 !

 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">

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));

 <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>
 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; }

<meta charset=utf-8 />

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


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

<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;">
  <!----------</Post Title>----->
     <!--<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;">

    <!--</post tags>-->
   <!-- <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>

<!-- </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>

   <!--// 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>
share|improve this question
here is a tutorial to how to make it work done it with my site and working great voodoopress.com/review-of-posting-from-front-end-form – Shady M Rasmy Jan 29 '13 at 2:49
i see you didn't reward your bounty or accept an answer. You should give people feedback or questions or their answers, they might just be able to help you out. – Blowsie Jan 31 '13 at 16:54

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.

share|improve this answer

The advancedcustomfields plugin offers a great solution to creating custom forms and creating a front end editor for them


Simply create your form and then template it like so.


 * Template Name: Page with ACF form


get_header(); ?>

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

            <?php the_post(); ?>

            <?php acf_form( $options ); ?>

        </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
share|improve this answer
So as am building a theme I can't opt for using plugins – vaibhav Feb 1 '13 at 4:57
These form is very easy to style. Its just an input. – Blowsie Feb 1 '13 at 12:19

simon blackburn is 100% correct on this. i believe this to be the easiest and most easily customized option. my day job requires me to create things like this very frequently. i am not permitted to post links to work sites publicly, but here's an example from a personal project using gravity forms:


as simon said, also be sure to check your php.ini settings as shown in his example.

something that was left out, however, is that by using gravity forms, you will be able to easily create a post template that can use other input data to customize the post, and have the video settings already saved in the template, as well as use conditional logic if needed. then have gravity forms simply save the post as "awaiting approval", so you'll be able to moderate as needed. also, have it email notify you of new submission so you can easily login to moderate on demand. done-zo!

share|improve this answer
So as am building a theme I can't opt for using plugins – vaibhav Feb 1 '13 at 5:00

Have you considered Gravity Forms? Its 'post fields' features give you all the functionality for creating posts from the front-end, including being able to specify allowable file types for uploads which are then set as post attachments. It also saves you having to write all your own data validation/sanitisation routines etc.

As mentioned in this GF forum post, you'll need to make sure your server can handle large uploads, so you may need to bump up your PHP settings as detailed here to whatever you discover is necessary for video files.

share|improve this answer
So as am building a theme I can't opt for using plugins – vaibhav Feb 1 '13 at 4:59

Here is the hard core coding for the simple answer. Use this code to attach a video to the post. I have given a static $listing_post_id post id equal to 7. You can use dynamical post id here. Other wise code is working and tested. Validation is not done here you can do it as you wish.

if (isset($_POST['uploadvideo'])) {

 if ( ! function_exists( 'wp_handle_upload' ) ) {

    require_once( ABSPATH . 'wp-admin/includes/image.php' );
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
    require_once( ABSPATH . 'wp-admin/includes/media.php' ); }

                $uploadedfile = $_FILES['photoContent'];
                $upload_overrides = array( 'test_form' => false );
                $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );

                 if ( $movefile )
                         $image_url = $movefile["url"];
                        $upload_dir = wp_upload_dir();
                        $image_data = file_get_contents($image_url);
                        $filename = basename($image_url);
                            $file = $upload_dir['path'] . '/' . $filename;
                            $file = $upload_dir['basedir'] . '/' . $filename;
                        file_put_contents($file, $image_data);

                        $wp_filetype = wp_check_filetype($filename, null );
                        $attachment = array(
                            'post_mime_type' => $wp_filetype['type'],
                            'post_title' => sanitize_file_name($filename),
                            'post_content' => '',
                            'post_status' => 'inherit'

                        $listing_post_id = 7 ; //your post id to which you want to attach the video
                        $attach_id = wp_insert_attachment( $attachment, $file, $listing_post_id);

                        $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
                        wp_update_attachment_metadata( $attach_id, $attach_data );

                        /*end file uploader*/




<form method ="post" action="" name="uploadvideo"  enctype="multipart/form-data">
<label for="ug_photo">Your Video Files
<input type="file" value="" name="photoContent" id="ug_photo" class="" multiple="multiple"></label>
<label for="ug_submit_button">
<input type="submit" value="uploadvideo" name="uploadvideo" id="ug_submit_button" class="btn"></label>
share|improve this answer

Automattic, the people behind wordpress proveides a fantastic solution for uploading and embedding videos into your wordpress blogs. Because sometime you need to convert the video with appropriate codec to make it playable from the web, and stream-able. Unfortunately it doesn't come for free. You can access this plugin/service called VideoPress from their official site


Beside that, if you want to go with free alternatives then use the following plugin

Video With Links


share|improve this answer
Do not install the Video With Links plugin, it hasn't been updated for years and it has a security issue and will very much risk your site being hacked, please check: packetstormsecurity.com/files/view/96787/evwp-xss.txt – Christine Cooper Jan 26 '13 at 0:33
videopress is for site owners to upload video via the admin interface - the question is about front-end uploading. – Simon Blackbourn Jan 27 '13 at 0:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.