2012-05-02 20 views
2

這是我第一個需要完成的自定義WordPress插件。一直在這整晚,無法弄清楚什麼是錯的......我假設我錯過了一些簡單和基本的標準插件編碼。WordPress自定義插件 - 頂級管理菜單中的PHP文件上傳器 - 找不到頁面404

我寫了一個插件,允許管理員儀表板中的頂級菜單項。它是一個簡單的PHP上傳表單。我想要做的就是讓客戶端將CSV文件上傳到特定的文件夾。我只是測試了wordpress框架外部的PHP代碼,它的工作原理......但是當我嘗試使用實際的WordPress管理員上傳文件時,我按提交,它不會上傳文件,它也會將我發送到「找不到頁面404「...最重要的是,當我激活我的生產網站上的插件時,它會導致錯誤和一些小故障...所以我假設我只是缺少一些我需要的插件php文件中的代碼。

這裏是我的插件主要的PHP文件的完整代碼 - 減去用於處理所有「上傳代碼」

<?php 
/* 
Plugin Name: Points Uploader 
*/ 
?> 

<?php 
add_action('admin_menu', 'sheets_plugin_menu'); 

function sheets_plugin_menu() { 
add_menu_page('Upload Points Sheets', 'Upload Points', 'manage_options', 'upload- points-sheets', 'sheets_plugin_page', '', 10); 
} 

function sheets_plugin_page() { 
if (!current_user_can('manage_options')) { 
wp_die(__('You do not have sufficient permissions to access this page.')); 
} 
echo '<div class="wrap">'; 
echo '<h1>Upload Points Sheets</h1>'; 
?> 

<form action="uploader.php" method="post" enctype="multipart/form-data"> 
<label for="file">Select a file:</label> <input type="file" name="userfile" id="file">  
<br /> 
<button>Upload File</button> 
</form> 

<?php 
echo '</div>'; 
} 
?> 

那麼什麼纔是我需要添加到這個PHP頁面,使之成爲獨立的PHP文件一個更「標準」的操作插件?當我按提交按鈕,就像我錯過了一個「wordpress發送」代碼,它只是404的,我不知道如何使其在管理屏幕字段內操作。謝謝你提供的任何幫助,我真的很難過。 ;)

...好吧,以防萬一它在這裏需要的是uploader.php文件。只是我在網上找到的示例代碼來測試事情。

<?php 
// Configuration - Your Options 
$allowed_filetypes = array('.csv'); // These will be the types of file that will pass the validation. 
$max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB). 
$upload_path = './upload/'; // The place the files will be uploaded to (currently a 'files' directory). 

$filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension). 
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. 

// Check if the filetype is allowed, if not DIE and inform the user. 
if(!in_array($ext,$allowed_filetypes)) 
die('The file you attempted to upload is not allowed.'); 

// Now check the filesize, if it is too large then DIE and inform the user. 
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) 
die('The file you attempted to upload is too large.'); 

// Check if we can upload to the specified path, if not DIE and inform the user. 
if(!is_writable($upload_path)) 
die('You cannot upload to the specified directory, please CHMOD it to 777.'); 

// Upload the file to your specified path. 
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) 
echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked. 
else 
echo 'There was an error during the file upload. Please try again.'; // It failed :(. 

?> 

回答

1

對於在WordPress中使用自定義上傳者來說相當新穎,我不知道我會有多少幫助。但我確實知道一件事:它可能是404因爲你的表單動作是「uploader.php」。這不會飛(除非你的uploader.php文件位於你的服務器的根目錄中) - WordPress不能很好地運行相對路徑。我敢肯定,您需要使用類似plugin_dir_url() . '/uploader.php'plugin_dir_path() . '/uploader.php'

您還必須記住,有些服務器對於上傳文件時是使用服務器路徑還是http路徑真的很挑剔。很多時候,當我上傳時,我必須確定我正在使用服務器路徑來存放文件夾(你有一個相對路徑再次=「./upload/」。在某些情況下,它只是像http路徑一樣,所以它是很多反覆試驗(對我來說),儘管如此。

就像我說的,不知道它是否有很大的幫助,但也許它只是一個簡單的從相對路徑切換到絕對路徑?

+0

哇......我無法相信所有我嘗試和測試過的東西,我從來沒有搞錯實際表單行爲的路徑......感謝您的建議 - 不再404;)我覺得愚蠢,但那沒關係,我是。大聲笑......儘管插件本身打開時會使其他插件出現錯誤 - 很少出現問題...猜猜我現在可以開始專注於這些插​​件了。感謝您的幫助! – randyttp

相關問題