Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. /*
  3. Plugin Name: Test plugin
  4. Description: A test plugin to demonstrate wordpress functionality
  5. Author: Simon Lissack
  6. Version: 0.1
  7. */
  8. add_action('admin_menu', 'test_plugin_setup_menu');
  9.  
  10. function test_plugin_setup_menu(){
  11.     add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
  12. }
  13.  
  14. function test_init(){
  15.     test_handle_post();
  16. ?>
  17.     <h1>Hello World!</h1>
  18.     <h2>Upload a File</h2>
  19.     <!-- Form to handle the upload - The enctype value here is very important -->
  20.     <form  method="post" enctype="multipart/form-data">
  21.         <input type='file' id='test_upload_pdf' name='test_upload_pdf'></input>
  22.         <?php submit_button('Upload') ?>
  23.     </form>
  24. <?php
  25. }
  26.  
  27. function test_handle_post(){
  28.     // First check if the file appears on the _FILES array
  29.     if(isset($_FILES['test_upload_pdf'])){
  30.         $pdf = $_FILES['test_upload_pdf'];
  31.  
  32.         // Use the wordpress function to upload
  33.         // test_upload_pdf corresponds to the position in the $_FILES array
  34.         // 0 means the content is not associated with any other posts
  35.         $uploaded=media_handle_upload('test_upload_pdf', 0);
  36.         // Error checking using WP functions
  37.         if(is_wp_error($uploaded)){
  38.             echo "Error uploading file: " . $uploaded->get_error_message();
  39.         }else{
  40.             echo "File upload successful!";
  41.         }
  42.     }
  43. }
  44.  
  45. ?>