如何将上传的视频附加到前端发布

时间:2013-01-22 作者:vaibhav

Hello am构建用户生成的内容共享主题

这是我从前端提交视频的代码

但此代码仅允许从url添加视频

我如何允许他们上传视频,然后将其附加到帖子

这是密码!

 <?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 个回复
SO网友:Jason

您可以使用wp\\u insert\\u attachment将视频文件上载到媒体库,然后将其附加到新创建的帖子。

看见http://codex.wordpress.org/Function_Reference/wp_insert_attachment

但明智的做法是,至少要有一些条件声明来检查视频大小和类型,否则你的网站就会被滥用。

SO网友:Blowsie

这个advancedcustomfields 插件为创建自定义表单和为其创建前端编辑器提供了一个很好的解决方案

http://www.advancedcustomfields.com/docs/tutorials/creating-a-front-end-form/

只需创建您的表单,然后像这样创建模板即可。

<?php

/**
 * Template Name: Page with ACF form
 */

acf_form_head();

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

SO网友:do7d

西蒙·布莱克本在这方面是百分之百正确的。我相信这是最简单和最容易定制的选择。我的日常工作要求我经常创造这样的东西。我不允许公开发布工作网站的链接,但这里有一个使用重力表单的个人项目的例子:

http://usadrifttrikes.org/contact-3/media-upload/

正如simon所说,还要确保检查您的php。ini设置,如他的示例所示。

然而,遗漏的是,通过使用重力表单,您可以轻松创建一个帖子模板,该模板可以使用其他输入数据自定义帖子,并且视频设置已经保存在模板中,如果需要,还可以使用条件逻辑。然后让重力表单将帖子保存为“等待批准”,这样你就可以根据需要进行调节。此外,让it电子邮件通知您新提交的内容,以便您可以轻松地按需登录到Medium。完成zo!

SO网友:Simon Blackbourn

你考虑过吗Gravity Forms? 它的“帖子字段”功能为您提供了从前端创建帖子的所有功能,包括能够为上传指定允许的文件类型,然后将其设置为帖子附件。它还使您无需编写所有自己的数据验证/清理例程等。

如中所述this GF forum post, 您需要确保您的服务器能够处理大型上载,因此您可能需要增加PHP设置as detailed here 无论你发现什么都是视频文件所必需的。

SO网友:Vijay Lal

下面是简单答案的核心代码。使用此代码将视频附加到帖子。我给了一个静态$listing\\u post\\u id post id等于7。您可以在此处使用动态post id。其他wise代码正在运行并经过测试。这里没有进行验证,您可以按照自己的意愿进行验证。

<?php
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);
                        if(wp_mkdir_p($upload_dir[\'path\']))
                            $file = $upload_dir[\'path\'] . \'/\' . $filename;
                        else
                            $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>
</form>

SO网友:Hasin Hayder

wordpress背后的人Automatic为上传视频和将视频嵌入wordpress博客提供了一个极好的解决方案。因为有时您需要使用适当的编解码器转换视频,使其可以从web上播放,并且可以流式传输。不幸的是,它不是免费的。你可以从他们的官方网站访问这个名为VideoPress的插件/服务

VideoPress

除此之外,如果您想使用免费的替代品,请使用以下插件

Video With Links

谢谢

结束