Last active
January 20, 2017 21:34
-
-
Save dasbairagya/dfc7edba1a5e13d5598da31e4fb456b7 to your computer and use it in GitHub Desktop.
Custom plugin for file upload and view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: CV | |
Plugin URI: http://Gowebbi.com/ | |
Description: CV | |
Author: | |
Version: 1.0 | |
Author URI: http://Gowebbi.com/ | |
*/ | |
add_action('admin_menu', 'wp_show_summery'); | |
// action function for above hook | |
function wp_show_summery() | |
{ | |
add_menu_page(__('CV', 'menu-task'), __('CV', 'menu-task'), 'edit_pages', 'wp_docs', 'wp_docs'); | |
add_submenu_page('wp_docs', __('View Uploads', 'menu-task'), __('View Uploads', 'menu-task'), 'edit_pages', 'view_uploads', 'view_uploads'); | |
} | |
function wp_docs(){ ?> | |
<div class="wrap"> | |
<div style="" id="new-file-uploader"> | |
<div id="poststuff"> | |
<div id="post-body" class="metabox-holder" style="width:520px"> | |
<div id="post-body-content"> | |
<div id="namediv" class="stuffbox"> | |
<h3>Add new Excel File</h3> | |
<div class="inside"> | |
<form method="post" enctype="multipart/form-data"> | |
<table class="form-table" style="width:500px"> | |
<tr> | |
<td>Name:</td> | |
<td><input type="text" name="file_name" | |
placeholder="Give it a name (required)" required/></td> | |
</tr> | |
<tr> | |
<td>File:</td> | |
<td><input type="file" name="file" style="width:400px" required/>Only *.xlsx | |
Filetypes are supported | |
</td> | |
</tr> | |
<tr> | |
<td>Upload:</td> | |
<td><input type="submit" class="button-primary" name="upload" | |
value="Start upload"/></td> | |
</tr> | |
</table> | |
</form> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
</div> | |
<?php | |
if (isset($_POST['upload'])) { | |
$extension = end(explode(".", $_FILES["file"]["name"])); | |
$fileSize = $_FILES["file"]["size"]; | |
if ($_FILES["file"]["size"] < 200000000000000000) { | |
if (!function_exists('wp_handle_upload')) { | |
require_once(ABSPATH . 'wp-admin/includes/file.php'); | |
} | |
$uploadedfile = $_FILES['file']; | |
$upload_overrides = array('test_form' => false); | |
$movefile = wp_handle_upload($uploadedfile, $upload_overrides); | |
if ($movefile && !isset($movefile['error'])) { | |
echo "File is valid, and was successfully uploaded.\n"; | |
global $wpdb; | |
$id = get_current_user_id(); | |
$fileName = $_POST['file_name']; | |
$fileUrl = $movefile['url']; | |
$wpdb->insert( | |
'soc_dev_excel', | |
array( | |
'user_id' => $id, | |
'sheet_name' => $fileName, | |
'sheet_url' => $fileUrl, | |
'file_size' => $fileSize | |
), | |
array( | |
'%s', | |
'%s', | |
'%s', | |
'%s' | |
) | |
); | |
} else { | |
/** | |
* Error generated by _wp_handle_upload() | |
* @see _wp_handle_upload() in wp-admin/includes/file.php | |
*/ | |
echo $movefile['error']; | |
} | |
} else { | |
echo "Invalid file"; | |
} | |
} | |
?> | |
</div> | |
<?php } | |
add_shortcode('my_short_code', 'wp_docs'); | |
function view_uploads() | |
{ ?> | |
<style>tr:nth-child(even) {background: #B8D1F3} | |
tr:nth-child(odd) {background: #DAE5F4} | |
th { background-color:#0A819C; color:black; text-align:center; height:30px; } | |
table,th,td { border:1px solid green;} | |
table { width:30%; color:black; text-align:center; } | |
</style> | |
<div style="margin-top:30px;width:75%"> | |
<form method="post" action=""> | |
<table class="widefat"> | |
<thead> | |
<!-- <tr>--> | |
<th style="text-align: center; font-weight: bold;">Sl No.</th> | |
<th style="text-align: center;font-weight: bold;">Name</th> | |
<th style="text-align: center;font-weight: bold;">File Size</th> | |
<th style="text-align: center;font-weight: bold;">Edit</th> | |
<th style="text-align: center;font-weight: bold;">Action</th> | |
<!-- </tr>--> | |
</thead> | |
<tbody> | |
<?php | |
$currentUser = get_current_user_id(); | |
global $wpdb; | |
if ($currentUser == 1) { | |
$results = $wpdb->get_results("select id,sheet_name,user_id,sheet_url,file_size | |
from soc_dev_excel "); | |
} else { | |
$sql = "SELECT * from soc_dev_excel where user_id=$currentUser"; | |
$results = $wpdb->get_results($sql); | |
} | |
foreach ($results as $result) { | |
?> | |
<tr> | |
<td><?php echo $result->id ?></td> | |
<td class="row-title"> | |
<a href=""></a> | |
<div id="uploader-"><?php echo $result->sheet_name ?> </div> | |
</td> | |
<td><?php echo $result->file_size ?> kb</td> | |
<td style="text-align: center;"><a href="<?php echo $result->sheet_url ?> ">Download</a></td> | |
<td style="text-align: center; "> | |
<input type="hidden" name="doc_id" value="<?php echo $result->id;?>"> | |
<input type="submit" name="deleteItem" value="Delete" style="cursor:pointer" /> | |
<!-- <a href="--><?php //echo site_url() ?><!--/admin.php?page=view_uploads?id=--><?php //echo $result->id ?><!-- " style="color: red"> Delete </a>--> | |
</td> | |
</tr> | |
<?php | |
if(isset($_POST['deleteItem'])){ | |
global $wpdb; | |
$item = $_POST['doc_id']; | |
$wpdb->delete( 'soc_dev_excel', array( 'id' => $item ) ); | |
} | |
} ?> | |
</tbody> | |
</table> | |
</form> | |
</div> | |
<?php } | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice....clean code