Skip to content

Instantly share code, notes, and snippets.

@ricardobrg
Created May 30, 2016 11:59
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 ricardobrg/e4753912bea1cdda0fd75f2d030af87d to your computer and use it in GitHub Desktop.
Save ricardobrg/e4753912bea1cdda0fd75f2d030af87d to your computer and use it in GitHub Desktop.
<?php
define ( 'BP_ENABLE_ROOT_PROFILES', true );
define( 'BP_DEFAULT_COMPONENT', 'settings' );
//dropzone
define( 'DROPZONEJS_PLUGIN_URL', get_stylesheet_directory_uri() );
define( 'DROPZONEJS_PLUGIN_VERSION', '0.0.1' );
add_action( 'init', 'dropzonejs_init' );
function dropzonejs_init() {
add_action( 'wp_enqueue_scripts', 'dropzonejs_enqueue_scripts' );
add_shortcode( 'dropzonejs', 'dropzonejs_shortcode' );
}
function dropzonejs_enqueue_scripts() {
wp_enqueue_script(
'dropzonejs',
'https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/min/dropzone.min.js',
array(),
DROPZONEJS_PLUGIN_VERSION
);
// Load custom dropzone javascript
wp_enqueue_script(
'customdropzonejs',
DROPZONEJS_PLUGIN_URL. '/js/customize_dropzonejs.js',
array( 'dropzonejs' ),
DROPZONEJS_PLUGIN_VERSION
);
wp_enqueue_style(
'dropzonecss',
'https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.2.0/min/dropzone.min.css',
array(),
DROPZONEJS_PLUGIN_VERSION
);
}
// Add Shortcode
function dropzonejs_shortcode( $atts ) {
$url = admin_url( 'admin-ajax.php' );
$nonce_files = wp_nonce_field( 'sis_dropzone_upload', 'dzone_nonce_field' );
$cadastro_id = $atts['cadastro_id'];
return <<<ENDFORM
<div id="dropzone-wordpress"><form action="$url" class="dropzone needsclick dz-clickable" id="dropzone-wordpress-form">
$nonce_files
<div class="dz-message needsclick">
Clique ou arraste os arquivos aqui para salvar.<br>
</div>
<input type='hidden' name='action' value='submit_dropzonejs'>
<input type='hidden' name='cadastro_id' value='$cadastro_id'>
</form></div>
ENDFORM;
}
add_action( 'wp_ajax_nopriv_submit_dropzonejs', 'dropzonejs_upload' ); //allow on front-end
add_action( 'wp_ajax_submit_dropzonejs', 'dropzonejs_upload' );
function dropzonejs_upload() {
if ( !empty( $_FILES ) && wp_verify_nonce( $_REQUEST['dzone_nonce_field'], 'sis_dropzone_upload' ) ) {
$dono = $_REQUEST['dono'];
$cadastro_id = $_REQUEST['cadastro_id'];
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
$attachment_id = media_handle_upload( 'file', $cadastro_id );
if ( is_wp_error( $attachment_id ) ) {
echo "Erro ao salvar a imagem";
} else {
echo "Imagem salva com sucesso";
update_post_meta($attachment_id, 'dono', $dono);
}
}
die();
}
Dropzone.options.dropzoneWordpressForm = {
acceptedFiles: "image/*", // all image mime types
maxFiles: 6,
uploadMultiple: false,
maxFilesize: 5, // 5 MB
//addRemoveLinks: true,
//dictRemoveFile: 'X (remove)',
init: function() {
this.on("sending", function(file, xhr, formData) {
formData.append("name", "value"); // Append all the additional input data of your form here
});
this.on("success", function (file,response){
alert(response);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment