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

Changing attachment urls?

You can do the following: /* add new rewrite rule */ function attachment_rewrite( $wp_rewrite ) { $rule = array( ‘media/(.+)’ => ‘index.php?attachment=” . $wp_rewrite->preg_index(1) ); $wp_rewrite->rules = $rule + $wp_rewrite->rules; } add_filter( “generate_rewrite_rules’, ‘attachment_rewrite’ ); /* redirect standard wordpress attachments urls to new format */ function redirect_old_attachment() { global $wp; if( !preg_match( ‘/^media\/(.*)/’, $wp->request ) … Read more

Attaching media to custom posts without editor

At the top of wp-admin/edit-form-advanced.php I see the following code that seems related to the media uploader: if ( post_type_supports($post_type, ‘editor’) || post_type_supports($post_type, ‘thumbnail’) ) { add_thickbox(); wp_enqueue_script(‘media-upload’); } You’ll need to add these yourself. add_thickbox() enqueues both a script and a style, so make sure you hook into print_styles, as print_scripts will be too … Read more

How to cleanly delete attachment but retain original file?

Year and some later with much improved skills, behold: Keep_Deleted_Attachment_File::on_load(); /** * Keep original file when deleting attachments. */ class Keep_Deleted_Attachment_File { static private $file; static function on_load() { add_action( ‘init’, array( __CLASS__, ‘init’ ) ); } static function init() { add_action( ‘delete_attachment’, array( __CLASS__, ‘delete_attachment’ ) ); } /** * @param int $post_id attachment … Read more

Check if post has attachments (not image)

You can use the following within a loop: $files = get_attached_media( $type, $post_id ); Just define the attachment MIME type on $type. Second parameter is optional. Example from the Codex page: $media = get_attached_media( ‘audio’, 102 ); With the retrieved array, you can do something like: if( $media ) { //Show the post attachments that … Read more

Stop WordPress from reserving slugs for media items?

Thank you for the response everyone. I played around with macemmek’s solution and I think it led me to an even better solution: add_filter( ‘wp_unique_post_slug_is_bad_attachment_slug’, ‘__return_true’ ); That is all that is needed. This will automatically ‘skip’ the default assigned slug on any attachment. So an attachment that might normally get the slug “services” will … Read more

wp_get_attachment_image_src and server path

WordPress doesn’t store path of generated sizes anywhere, you need to build it. As suggested by @kraftner in comments, wp_get_attachment_metadata() can be used to obtain some of the pieces you need to build the path. An alternative is image_get_intermediate_size(). The missing piece is the absolute path of upload folder. In theory, that can be retrieved … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)