使用前端POST表单将不同的文件作为自定义域上传时出现问题

时间:2012-08-29 作者:Pollux Khafra

我有一个前端帖子表单,在获取媒体的正确路径以另存为自定义字段时遇到了一些问题。文件的路径将另存为mysite。com/home/betauser/public\\u html/betatheme/wp\\u content/。。。。文件路径。如何获得正确保存的路径?

<?php if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {
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\');
}
$file=$_FILES;
// Do some minor form validation to make sure there is content
if (isset ($_POST[\'title\'])) {
    $title =  $_POST[\'title\'];
} else {
    echo \'Please enter a game  title\';
}
if (isset ($_POST[\'description\'])) {
    $description = $_POST[\'description\'];
} else {
    echo \'Please enter the content\';
}
$tags = $_POST[\'post_tags\'];

// Add the content of the form to $post as an array
$new_post = array(
    \'post_title\'    => $title,
    \'post_content\'  => $description,
    \'post_category\' => 11,
    \'tags_input\'    => array($tags),
    \'post_status\'   => \'publish\',           // Choose: publish, preview, future, draft, etc.
    \'post_type\' => fod_music  // Use a custom post type if you want to
);
$overrides = array( \'test_form\' => false);
$uploaded_music = wp_handle_upload( $file[\'music_file\'], $overrides );
$uploaded_music_art = wp_handle_upload( $file[\'music_image\'], $overrides ); 
//save the new post and return its ID
$pid = wp_insert_post($new_post);
update_post_meta($pid,\'music_code\',$uploaded_music[\'file\']);
update_post_meta($pid,\'music_art\',$uploaded_music_art[\'file\']);
wp_redirect( get_permalink($pid)); 
exit();

} 
do_action(\'wp_insert_post\', \'wp_insert_post\');
?>

<div id="postbox">
 <form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
  <p><label for="title">Song Title</label><br />
   <input type="text" id="title" value="" tabindex="1" size="20" name="title" />
  </p>
  <p><label for="music_file">Upload a song</label><br />
   <input type="file" name="music_file" id="music_file"/>
  </p>
  <p><label for="description">Description</label><br />
   <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>
  </p>
  <p><label for="music_image">Upload a music art</label><br />
   <input type="file" name="music_image" id="music_image"/>
  </p>
  <p><label for="post_tags">Tags</label>
   <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>
  <p align="right"><input type="submit" value="Publish" tabindex="6" id="submit" name="submit" /></p>
  <input type="hidden" name="action" value="new_post" />
  <?php wp_nonce_field( \'new-post\' ); ?>
 </form>
</div>

1 个回复
最合适的回答,由SO网友:Eric Holmes 整理而成

对于初学者,“post\\u category”参数只接受类别ID,因此您必须通过数据库或通过get\\u category\\u by\\u slug找到:

$cat = get_category_by_slug( \'music\' ); 
$cat_id = $cat->term_id;
其次,查看wp\\u handle\\u upload()部分。在这两种情况下,您都在传递$file[\'file\'],其中“file”不是您的任何上载字段的名称。我认为应该是:

$uploaded_music = wp_handle_upload( $file[\'music_file\'], $overrides );
$uploaded_music_art = wp_handle_upload( $file[\'music_image\'], $overrides ); 

结束

相关推荐

Custom Field & Gravity Forms

我有以下自定义函数代码,用于为帖子类型创建自定义字段。问题是,我需要自定义字段单独显示,此时它们只显示为“键”,这意味着当我尝试将表单字段(重力表单)链接到自定义字段时,我只能链接到“键”字段。如何修改此选项以单独显示每个自定义字段?/* Custom Fields */ $key = \"key\"; $meta_boxes = array( \"cf_link\" => array( \"name\" => \"cf_link\", \"title\