Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently using WP 3.3.1.

Basically I just want a page for users who login via wp-login.php and see the backend admin panel to be able to see a page that shows a list of files they can download.

  1. The files will either be .zip, .pdf, or .txt.
  2. Would be awesome if the list of downloads had the same exact format as when you view Posts>> All Posts.
  3. To get things started, it doesn't need a sort or filter functionality. Although one would be nice.

Not exactly sure how I will upload the files. But whatever route would make the above easiest is the okay with me.

Any tips or code snippets that will either do the above or get me started in the right direction would be awesome!

Thank you in advance.

share|improve this question
add comment

2 Answers 2

There will be several steps involved in this process, I'll outline a few here to get you started.

Step 1: Set up a Plug-in

You can view some more info about them here. I won't go over the header information in this answer because it's pretty self-explanatory in that article and your values for it will be different.

Step 2: Set up Your Activation and Function

//run when admin menu is being created
add_action('admin_menu','my_files_plugin_menu');
//add the new item to the admin menu
function my_files_plugin_menu() {
     add_menu_page('Files','Files','editor','my-files-plugin','','',10);
     add_submenu_page('my-files-plugin','Download Files','Download Files','editor','my-files-plugin','my_files_plugin_download_page');
     add_submenu_page('my-files-plugin','Upload Files','Upload Files','administrator','my-files-upload','my_files_plugin_upload_page');
}

//run when plug-in is activated
register_activation_hook(__FILE__,'ballots_by_social_set_options');
//set up activation options for plug in
function my_file_upload_plug_in_activate() {
     //check to see if files directory has already been created
     //we check for the directory first so that if the plug-in is being reactivated
     //or updated, we won't bother trying to create the directory again
     if(!is_dir(ABSPATH . '/wp-content/files_plugin')) {
          //create directory
          mkdir(ABSPATH . '/wp-content/files_plugin',0755);
     }
}

I used the editor user level for the downloads page and the administrator user level for the uploads page, but you could set them to whatever you'd like. You could also add a 3rd page with Options and use add_option() in your register_activation_hook() to set some defaults. Then you'll want to use update_option() and get_option() to allow adjustment of the user levels that can access the pages. If you decide to do that, make sure to set up a register_deactivation_hook() where you use delete_option() to remove them.

Step 3: Set up your Uninstall Function

I recommend putting this in an unstall.php file in your plug-in directory. This will make sure that when someone clicks the Delete link on the plug-in it will remove the directory and all of it's contents. If you set up sub-directories in the main directory for different file types you can modify the function to remove those, too.

//remove /wp-content/files_plugin/
$dir = ABSPATH . '/wp-content/files_plugin';
chmod($dir,0777);
function remove_files_plugin_dir($dir) {
     //see if directory exists
     if(is_dir($dir)) {
     //open directory
     $handle = opendir($dir);
     //iterate through objects in directory and remove them
     while($obj = readdir($handle)) {
          if($obj != "." && $obj != '..') {
               if(!unlink($dir . $obj)) {
                    break;
               } 
          }
     }
     closedir($handle);
     rmdir($dir);
}
//run remove directory function
remove_files_plugin_dir($dir);

Step 4: Set up Your Uploads Page

You'll want to set up a simple page with a file input and an upload button, you can set up the form to accept multiple files if you'd like to. You'll want to reference the $_FILES array and write the uploaded file to your new /wp-content/files_plugin/ directory.

Step 5: Set up Your Downloads Page

This is pretty much up to how you'd like to do it. You could easily just open the directory and iterate through the files, much like the uninstall function and then output them to the page.

Or you could use something like the jQuery File Tree to present a file browser to your users.

EDIT 2: I was intensely wrong about the way to make it appear like the posts page. It looks like you can use the WP_List_Table class to achieve this in a much better way. I have no experience with this class, but it looks very snazzy.

I've just been setting up the output to mirror the typical admin area tables in terms of structure and classes. I'll definitely be using WP_List_Table from now on, though.

Step 6: Set Up a Delete Function

You'll probably want to have a link specific to each file that only administrators can see that allows you to delete the file.

You might want to set it up as an Ajax call to a deletion function, sending it the file name as a parameter then use the unlink() function, like we did in the uninstall function to remove that file.

Obviously you may want to tweak any of this code to suit your needs, but this should provide a decent starting point for you.

And, don't forget that you could use a table in the database to control all of this, as well, but you'll still need to add and remove the actual files and just store filenames in the DB.

If you need any help or have any questions, post back and I'll try to provide assistance.

share|improve this answer
    
Wow, thank you for taking such time to write this up. I will report back once I try everything out and report my results :)! –  Damainman Apr 22 '12 at 10:20
    
@Damainman So, we got some month passing by - what happened? Are you already able to report back? –  kaiser Sep 12 '12 at 23:30
add comment

I was searching for another argument but came across this topic. Even if it's quite old, I'd like to point out that probably there are more things you can do to accomplish your task.

If I were you, for the upload thing I would have made a custom post type with the relevant metaboxes (wpalchemy here).

Then for the download thing, it depends on what you really need. If you really need to show the list INSIDE the admin area, WP_List_Table is the way (and check here). Else, I think you could setup a generic custom-post-type archive page and then bind it with the relevant user profile or capability.

Hope it helps some other guy in the future.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.