Skip to content

Instantly share code, notes, and snippets.

@dcangulo
Last active April 3, 2018 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcangulo/40d2f4a382bb5b78b97b55592b535a1e to your computer and use it in GitHub Desktop.
Save dcangulo/40d2f4a382bb5b78b97b55592b535a1e to your computer and use it in GitHub Desktop.
A simple WordPress plugin that allows you to upload files programatically. Visit https://www.davidangulo.xyz/ for more information.
<?php
/*
Plugin Name: Upload Files Programatically
Plugin URI: https://wordpress.org/plugins/
Description: Just another file uploader plugin.
Version: 1.0.0
Author: David Angulo
Author URI: https://www.davidangulo.xyz/
*/
function myFileUploader() {
if(isset($_POST["submit"])) {
wp_upload_bits($_FILES['fileToUpload']['name'], null, file_get_contents($_FILES['fileToUpload']['tmp_name']));
}
echo '<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>';
}
function myFileUploaderRenderer() {
ob_start();
myFileUploader();
return ob_get_clean();
}
add_shortcode("custom_file_uploader","myFileUploaderRenderer");
//Visit the tutorial for more information
//https://www.davidangulo.xyz/website-development/how-to-upload-files-in-wordpress-programmatically/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment