How to get attachment id as soon as it is uploaded through media uploader in jquery?

You are close, you just need to hook into the add event instead of the reset event. (In case you did not know, these are standard events provided by Backbone collections. So familiarizing yourself with that will be helpful when developing things around stuff where WordPress employs Backbone.js.) So basically you’d modify your code like … Read more

Rename files on upload

no need to use a custom table, use an option, and the add_attachment hook: function wpa59168_rename_attachment( $post_ID ) { $post = get_post( $post_ID ); $file = get_attached_file( $post_ID ); $path = pathinfo( $file ); $count = get_option( ‘wpa59168_counter’, 1 ); // change to $new_name = $count; if you want just the count as filename $new_name … Read more

Using same directory for storing all uploaded images on a WordPress network

You can pretty simple add some actions to archive this. function wpse_16722_main_uploads() { switch_to_blog(1); } add_action(‘load-media-new.php’, ‘wpse_16722_main_uploads’); add_action(‘load-media-upload.php’, ‘wpse_16722_main_uploads’); add_action(‘load-media.php’, ‘wpse_16722_main_uploads’); add_action(‘load-upload.php’, ‘wpse_16722_main_uploads’); add_action(‘admin_init’, ‘wpse_16722_main_uploads’); This will change the current sub-blog to user your main-site with the help of switch_to_blog and of course you want the main side by the id 1. You can also … Read more

How to wp_upload_bits() to a sub-folder?

wp_upload_bits() uses wp_upload_dir() which fires the upload_dir filter allowing you to modify the upload directory’s path, sub-directory and URL. So you can use that filter to “force” wp_upload_bits() to use a custom path by doing something like: // Demo to get the image data. $url=”https://logo.clearbit.com/starcomww.com”; $content = file_get_contents( $url ); $_filter = true; // For … Read more

Can I use the wp media uploader for my own plugin?

You can use wp_enqueue_media() in your admin_enqueue_scripts hook. In a javascript file hook it into a button and use insert event to capture the selected image’s details $(‘.media-button’).click(function() { var media_uploader = wp.media({ frame: “post”, text : “Add image”, state: “insert”, multiple: false }); media_uploader.on(“insert”, function(){ var json = media_uploader.state().get(“selection”).first().toJSON(); var image_name = json.filename; var … Read more

Protecting direct access to PDF and ZIP unless user logged in (without plugin)

RewriteCond %{REQUEST_FILENAME} -s RewriteRule ^wp-content/uploads/([^/]*\.(pdf|zip))$ filecheck.php?file=$1 [QSA,L] This actually looks OK, except if you have additional subdirectories within the /uploads directory? An alternative is to include an additional condition on the original rule that only rewrites the request if the request ends in .pdf or .zip. For example: RewriteCond %{REQUEST_URI} \.(pdf|zip)$ [NC] RewriteCond %{REQUEST_FILENAME} -s … Read more

Add inline uploader to plugin option page

Okay, here is what I came up with: It’s all about using the plupload library that comes shipped with WP. 1. Add a <div> to your plugin’s option page that later becomes the drag’n’drop area <div class=”your-plugin-uploader multiple”> <input id=”your-plugin-uploader-button” type=”button” value=”<?php esc_attr_e( ‘Select Files’ ); ?>” class=”your-plugin-uploader-button button”> <span class=”ajaxnonce” id=”<?php echo wp_create_nonce( __FILE__ … Read more

retrieve custom image sizes from media uploader javascript object

thanks to a friend on Twitter, I was able to get this working. Below is the code. function wpse_110060_image_sizes_js( $response, $attachment, $meta ){ $size_array = array( ‘custom_size_one’, ‘custom_size_two’) ; foreach ( $size_array as $size ): if ( isset( $meta[‘sizes’][ $size ] ) ) { $attachment_url = wp_get_attachment_url( $attachment->ID ); $base_url = str_replace( wp_basename( $attachment_url ), … Read more

Image upload callback in new 3.5 media

There is a FileUploaded event being fired in wp-includes/js/plupload/wp-plupload.js. Alternatively (and propably the better way) you may want extend wp.Uploader with your own success callback . (function($){ $.extend( wp.Uploader.prototype, { success : function( file_attachment ){ console.log( file_attachment ); } }); })(jQuery);