所以,我有一个前端post表单如下:
<?php
global $current_user;
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] )) {
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter a title';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter the content';
}
$tags = $_POST['post_tags'];
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $_POST['cat'],
'tags_input' => $tags,
'post_status' => 'publish',
'post_type' => $_POST['post_type']
);
wp_redirect( home_url() );
}
do_action('wp_insert_post', 'wp_insert_post');
?>
<!-- New Post Form -->
<div class="rh_item_upload">
<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
<input class="input" type="text" id="title" name="title"/>
<textarea class="input" type="text" rows= "3" id="description" name="description" ></textarea>
<input type="file" name="file[]" id="file" multiple="">
<input type="submit" value="Publish" tabindex="6" id="submit" name="submit" />
<input type="hidden" name="post_type" id="post_type" value="post" />
<input type="hidden" name="action" value="post" />
<?php wp_nonce_field( 'new-post' ); ?>
</form>
</div>
现在,它有一个基本的文件(img)上传按钮。
然而,我想使用Plupload上传多个文件/client-sie resize/ajax函数到它。
有谁知道我可以遵循的教程或指南吗?
谢谢!
<form id="new_post" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="action" value="custom_send_file"/>
<input class="input" type="text" id="title" name="title"/><br/>
<textarea class="input" type="text" rows= "3" id="description" name="description" ></textarea><br/>
<input type="file" name="file" id="file" multiple="false" />
<input type="submit" value="Publish" tabindex="6" id="submit" name="submit" />
</form>
我认为你在一个主题中创建你的前端表单。然后将其添加到functions.php中如果您不使用主题,则将此代码推送到页面的开头。
function custom_send_file()
{
$my_post = array(
'post_title' => $_POST["title"],
'post_content' => $_POST["description"],
'post_status' => 'publish'
);
// this line create a new post with your content form
$id_post = wp_insert_post( $my_post );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
// upload the file and link it to the post
media_handle_upload( $key, $id_post );
// i use this next line to cancel a refresh which send again the form
wp_redirect( $_SERVER['HTTP_REFERER'] );
exit;
}
if( isset($_POST["action"]) && $_POST["action"] == "custom_send_file" )
add_action("init", "custom_send_file");
希望这对你有帮助。
ps:实际上它只发送一个文件。也许每个文件只使用一个输入(我将把这个教程添加到我的网站,谢谢这个想法;)