Make attachment pages require a base url

Endpoints are for adding extra query vars to existing routes, I think you just want a vanilla rewrite rule here: function wpd_media_image_rewrite_rule() { add_rewrite_rule( ‘^image/([^/]*)/?’, ‘index.php?post_type=attachment&name=$matches[1]’, ‘top’ ); } add_action( ‘init’, ‘wpd_media_image_rewrite_rule’ ); You might want to loop over valid mime-types there and add a rule for each. You also need to flush rewrites for … Read more

Rename image during upload using date and time stamp?

You could e.g. check the filename and extension from the pathinfo, after your custom sanitization. Example: If the filename is empty and extension non-empty, then add the formatted current time as the filename part: $info = pathinfo( $file[‘name’] ); if( empty( $info[‘filename’] ) && ! empty( $info[‘extension’] ) ) $file[‘name’] = sprintf( ‘%s.%s’, current_time( ‘Y-m-d-H-i-s’ … Read more

Any way to define WP_UPLOADS_URL (like WP_CONTENT_URL)?

Since version 3.5 you can define constant ‘UPLOADS’ like: define(‘UPLOADS’,’myfiles’); where ‘myfiles’ is a folder location relative to ABSPATH. If the folder does not exist, it will be automatically created. EDIT: It will only work, if a new folder is located below website’s Document Root folder ( ABSPATH ). To put it outside of ABSPATH … Read more

How to upload all media to one folder, with no year/month subfolders

Pop this tiny code snippet into a file located here: wp-content/mu-plugins/upload-dir.php (a must use plugin file). Create the mu-plugins directory if it does not exist already. <?php add_filter( ‘pre_option_uploads_use_yearmonth_folders’, ‘__return_zero’); What you’re doing here is filtering an option value at runtime, which is picked up internally by _wp_upload_dir() and therefore uploads are no longer nested … Read more

Add media attachment filter to custom wp.media frame

You need to make use of filterable property. To do that, you can extend the library and use that as a custom state. // Create state var myCustomState = wp.media.controller.Library.extend({ defaults : _.defaults({ id: ‘my-custom-state’, title: ‘Upload Image’, allowLocalEdits: true, displaySettings: true, filterable: ‘all’, // This is the property you need. Accepts ‘all’, ‘uploaded’, or … Read more

why when I try to insert an image attachment along with a post does wp_get_attachment_url give me a very wrong file path?

It turns out I was barking up the wrong (or at least a slightly different) tree by using wp_insert_attachment. media_sideload_image managed to pull attachments from other blogs on the same multisite install, copy them to the aggregating blog’s uploads directory, and generate thumbnails, while wp_insert_attachment was doing what it was supposed to, which just happened … Read more