-
-
Save thedapifer/90ed38d19c3b131d7ac595f491da8036 to your computer and use it in GitHub Desktop.
Ninja Image Upload WordPress Plugin
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 | |
| /** | |
| * @package ninja_image_upload | |
| * @version 1.0 | |
| */ | |
| /* | |
| Plugin Name: Ninja Image Upload | |
| Plugin URI: http://authlab.io/ | |
| Description: This plugin allow users to upload image via a form can be created by shortcode | |
| Version: 1.0 | |
| Author: Sudipto Chowdhury Dip | |
| Author URI: http://dipchy.azurewebsites.net/ | |
| */ | |
| if( ! defined( 'ABSPATH' ) ) { | |
| die(); | |
| } | |
| if ( ! class_exists('ninjaImageUpload') ) { | |
| class ninjaImageUpload | |
| { | |
| public function __construct () | |
| { | |
| $this->hooks(); | |
| } | |
| public function niu_form_html() | |
| { | |
| ob_start(); | |
| ?> | |
| <?php if (is_user_logged_in()): ?> | |
| <p>Please use the form below to upload your image. Good Luck!</p> | |
| <form action="" method="post" class="image-form"> | |
| <p><input type="file" name="async-upload" class="image-file" accept="image/*" required></p> | |
| <p><input type="submit" value="Submit"></p> | |
| </form> | |
| <p class="info-preview"></p> | |
| <?php else: ?> | |
| <p> | |
| Please, <a href="<?php echo esc_url(wp_login_url(get_permalink())); ?>">login</a> | |
| first to submit your image. | |
| </p> | |
| <?php endif; ?> | |
| <?php | |
| $output = ob_get_clean(); | |
| return $output; | |
| } | |
| public function niu_load_scripts() | |
| { | |
| wp_enqueue_script( | |
| 'image-form-js', | |
| plugin_dir_url(__FILE__).'/js/script.js', | |
| array('jquery'), | |
| '0.1.0', | |
| true | |
| ); | |
| $data = array( | |
| 'upload_url' => admin_url('async-upload.php'), | |
| 'ajax_url' => admin_url('admin-ajax.php'), | |
| 'nonce' => wp_create_nonce('media-form') | |
| ); | |
| wp_localize_script('image-form-js', 'niu_config', $data); | |
| } | |
| public function hooks() | |
| { | |
| add_action( | |
| 'wp_enqueue_scripts', | |
| array($this, 'niu_load_scripts') | |
| ); | |
| add_shortcode( | |
| 'ninja_image_upload', | |
| array($this, 'niu_form_html') | |
| ); | |
| } | |
| } | |
| } | |
| $pluginInitiate = new ninjaImageUpload(); |
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
| var imgForm = jQuery('.image-form'); | |
| var imgFile = imgForm.find('.image-file'); | |
| var infoPreview = jQuery('.info-preview'); | |
| imgForm.on('submit', function (event) { | |
| event.preventDefault(); | |
| var formData = new FormData(); | |
| formData.append('action', 'upload-attachment'); | |
| formData.append('async-upload', imgFile[0].files[0]); | |
| formData.append('name', imgFile[0].files[0].name); | |
| formData.append('_wpnonce', niu_config.nonce); | |
| jQuery.ajax({ | |
| url: niu_config.upload_url, | |
| data: formData, | |
| processData: false, | |
| contentType: false, | |
| dataType: 'json', | |
| type: 'POST', | |
| success: function(response) { | |
| if (response.success) { | |
| imgForm.hide(); | |
| infoPreview.css('color', 'green'); | |
| infoPreview.html( | |
| 'Image Uploaded Successfully!' | |
| ).show(); | |
| } else { | |
| console.log('got error'); | |
| } | |
| } | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment