您好,这是我在插件中使用的代码,希望您理解,如果有任何问题,您可以问我:-
if(isset($_FILES))
{
for ($i=1; $i < count($_FILES) ; $i++)
{
$uploadedfile = $_FILES[\'question_\'.$i];
?>
<script type="text/javascript">
$(document).ready(function(){
var nam = <?php echo "question".$i; ?>
var namm = $("."+nam).prev().attr(\'name\');
});
</script>
<?php
$upload_overrides = array( \'test_form\' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && !isset( $movefile[\'error\'] ) ) {
//echo "File is valid, and was successfully uploaded.\\n";
$filename = $movefile[\'file\'];
} else {
/**
* Error generated by _wp_handle_upload()
* @see _wp_handle_upload() in wp-admin/includes/file.php
*/
echo $movefile[\'error\'];
}
// $filename should be the path to a file in the upload directory.
//$filename = \'/path/to/uploads/2013/03/filename.jpg\';
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
$url = $wp_upload_dir[\'url\'] . \'/\' . basename( $filename );
echo $url."<br>";
}
}
这是我的html代码:-
$question =get_post_meta($trivia->ID,\'question\',true);
$c = 0;
$id = uniqid();
if ( count( $question ) > 0 ) {
if(is_array($question)){
foreach( $question as $questions ) {
if ( isset( $questions[\'question\'] ) ) {
printf(\'<p>Question: <input type="text" name="question[%1$s][question]" value="%2$s"> -- <span class="remove_question">%3$s</span><span class="add_answer"> Add Answer</span></p>\', $c,$questions[\'question\'],__(\'Remove Question\'));
for($i = 1; $i<=count($questions[\'answers\']); $i++)
{
if($questions[\'answers\'][$i][\'image\'])
{
printf(\'<p>Option %5$s <input type="text" value="%1$s" name="question[%4$s][answers][%5$s][answer]"> Status <input type="text" value="%2$s" name="question[%4$s][answers][%5$s][status]"> Image <input type="text" name="question[%4$s][answers][%5$s][image]" value="%6$s"><img src="%6$s" height="100" widht="100"> -- <span class="remove_answer">%3$s</span></p>\', $questions[\'answers\'][$i][\'answer\'] ,$questions[\'answers\'][$i][\'status\'], __(\'Remove Answer\'), $c, $i,$questions[\'answers\'][$i][\'image\']);
}
else
{
printf(\'<p>Option %5$s <input type="text" value="%1$s" name="question[%4$s][answers][%5$s][answer]"> Status <input type="text" value="%2$s" name="question[%4$s][answers][%5$s][status]"> -- <span class="remove_answer">%3$s</span></p>\', $questions[\'answers\'][$i][\'answer\'] ,$questions[\'answers\'][$i][\'status\'], __(\'Remove Answer\'), $c, $i);
}
}
$c = $c +1;
}
}
}
}
?>
<span id="question_here"></span>
<span class="add_question"><?php _e(\'Add Question\'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
var countt = 0;
$(".add_question").click(function() {
count = count + 1;
countt = 0;
$(\'#question_here\').append(\'<p>Question: <input type="text" name="question[\'+count+\'][question]" value=""> -- <span class="remove_question">Remove question</span><span class="add_answer"> Add Answer</span></p>\' );
return false;
});
$(".remove_question").live(\'click\', function() {
$(this).parent().remove();
});
$(".add_answer").live(\'click\',function(){
countt = countt + 1;
$(\'#question_here\').append(\'<p>Answer <input type="text" name="question[\'+count+\'][answers][\'+countt+\'][answer]"> Status <input type="text" name="question[\'+count+\'][answers][\'+countt+\'][status]"><input id="question_\'+count+\'_answers_\'+countt+\'_image" type="text" size="36" name="question[\'+count+\'][answers][\'+countt+\'][image]" value="" /><input class="upload_image_button" type="button" value="Upload Image" /><span class="remove_answer">Remove</span></p>\');
})
$(\'.upload_image_button\').live(\'click\',function() {
formfield = $(\'#upload_image\').attr(\'name\');
tb_show(\'\', \'media-upload.php?type=image&TB_iframe=true\');
return false;
});
window.send_to_editor = function(html) {
imgurl = $(\'img\',html).attr(\'src\');
$(\'#question_\'+count+\'_answers_\'+countt+\'_image\').val(imgurl);
tb_remove();
}
$(".remove_answer").live(\'click\', function() {
$(this).parent().remove();
});
});
</script>
</div><?php