Created
November 7, 2016 04:07
-
-
Save sutandang/e9ea0dfdc71731b83eb346eef666d03c to your computer and use it in GitHub Desktop.
create wp upload in front end wordpres
This file contains 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
1. load media in controller / function.php / plugins | |
``` | |
function enqueue_media_scripts() { | |
if(function_exists('wp_enqueue_media')) { | |
wp_enqueue_media(); | |
} | |
else { | |
wp_enqueue_script('media-upload'); | |
wp_enqueue_script('thickbox'); | |
wp_enqueue_style('thickbox'); | |
} | |
} | |
add_action( 'wp_enqueue_scripts', 'enqueue_media_scripts' ); | |
``` | |
triger to show in single page / view | |
``` | |
<div class="gallery_image"> | |
</div> | |
<div class="image-additional--link"> | |
<a class="add-image" href="#" id="additionalImage2">+ ADDITIONAL IMAGE</a> | |
</div> | |
``` | |
``` | |
js to add triger | |
jQuery('.icon-trash').on('click',function(){ | |
jQuery(this).remove(); | |
}); | |
jQuery("#additionalImage2").on('click', function(e){ | |
e.preventDefault(); | |
var custom_uploader = wp.media({ | |
title: 'Upload Media', | |
button: { | |
text: 'Upload' | |
}, | |
multiple: true // Set this to true to allow multiple files to be selected | |
}) | |
.on('select', function() { | |
var selection = custom_uploader.state().get('selection'); | |
selection.map( function( attachment ) { | |
attachment = attachment.toJSON(); | |
console.log(attachment); | |
var html = "<span class='icon-trash'><img width='150px' height='150px' src=" +attachment.url+"><input name='gallery[]' type='hidden' value=" +attachment.id+"></span>"; | |
jQuery(".gallery_image").append(html); | |
jQuery('.icon-trash').on('click',function(){ | |
jQuery(this).remove(); | |
}); | |
}); | |
}).open(); | |
}); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment