This is my first Custom WordPress Plugin that I really need to get done. Been at this ALL night long and can’t figure out whats wrong… I’m assuming I’m missing something simple and basic with standard plugin coding.
I wrote a plugin that allows a Top-Level Menu item in the admin dashboard. Its a simple PHP upload form. All I want to do is let the client upload CSV files to a specific folder. I just tested the PHP code OUTSIDE of the wordpress framework and it works… But when I try and upload a file using the actual wordpress admin, I press submit and it doesnt upload the file it also sends me to a “page not found 404″… ON top of that when I activate the plugin on my production site it causes errors and some minor glitches… So I’m assuming I’m just missing some code I need in the plugin php file.
Here is the full code for my plugins main php file – minus the separate php file that handles all the “uploading code”
<?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>';
}
?>
So what do I need to add to this php page to make it a more “standard” operating plugin? When I press the submit button its like I’m missing a “wordpress send” code, and it just 404’s, I don’t know how to make it operate within the Admin screen field. Thank you for any help you can offer, I’m really stumped here. 😉
…ok just in case its needed here is the uploader.php file. Just a sample code I found on the net to test things out.
<?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 :(.
?>
Being fairly new to using custom uploaders in WordPress, I don’t know how much help I will be. but I do know one thing: it’s probably 404-ing because your form action is “uploader.php”. That’s not gonna fly (unless your uploader.php file is located in the root directory of your server) – WordPress doesn’t play nicely with relative paths. I’m pretty sure you need to use something like
plugin_dir_url() . '/uploader.php'
orplugin_dir_path() . '/uploader.php'
You also have to keep in mind that some servers are really picky about whether you use the server path or http path when it comes to uploading files. Many times, when I’m uploading, I have to be sure I’m using the server path to the folder I’m putting the file into (you have a relative path there again = “./upload/”. In some cases, it only like the http path. So it’s a lot of trial and error (for me anyway) as far as that goes.
Like I said, don’t know if it helps much, but maybe it’s just a simple switch from relative to absolute paths?