2

这是我真正需要完成的第一个自定义 Wordpress 插件。整晚都在这里,无法弄清楚出了什么问题……我假设我缺少标准插件编码的一些简单和基本的东西。

我编写了一个插件,允许在管理仪表板中使用顶级菜单项。它是一个简单的 PHP 上传表单。我要做的就是让客户端将 CSV 文件上传到特定文件夹。我刚刚在 wordpress 框架之外测试了 PHP 代码并且它可以工作......但是当我尝试使用实际的 wordpress 管理员上传文件时,我按下提交并且它没有上传文件它也将我发送到“找不到页面404"... 最重要的是,当我在生产站点上激活插件时,它会导致错误和一些小故障...所以我假设我只是在插件 php 文件中丢失了一些我需要的代码。

这是我的插件主 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 页面中添加什么来使它成为一个更“标准”的操作插件呢?当我按下提交按钮时,就像我错过了“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 :(.

?>
4

1 回答 1

1

作为在 WordPress 中使用自定义上传器的新手,我不知道我会得到多少帮助。但我确实知道一件事:它可能是 404-ing,因为您的表单操作是“uploader.php”。这不会飞(除非您的 uploader.php 文件位于服务器的根目录中) - WordPress 不能很好地使用相对路径。我很确定您需要使用类似plugin_dir_url() . '/uploader.php'plugin_dir_path() . '/uploader.php'

您还必须记住,有些服务器在上传文件时对于使用服务器路径还是 http 路径非常挑剔。很多时候,当我上传时,我必须确保我使用的是服务器路径到我将文件放入的文件夹(你在那里又有一个相对路径=“./upload/”。在某些情况下, 它只喜欢 http 路径。因此,就这一点而言,这是很多试验和错误(无论如何对我来说)。

就像我说的,不知道它是否有很大帮助,但也许这只是从相对路径到绝对路径的简单切换?

于 2012-05-02T17:32:45.807 回答