Give attachments an archive page

This is untested, so apologies there, but I’m thinking out loud, have you tried re-registering the post type afterwards? Or flushed rewrite rules after your initial attempt with flush_rewrite_rules();? function change_attachment_post_type() { $args = get_post_type_object(‘attachment’); $args->has_archive = true; $args->rewrite = [ ‘slug’ => ‘media’ ]; register_post_type($args->name, $args); // As a temporary one time, remove after … Read more

How to get attachments for a specific post type?

$posts = get_posts( array( ‘post_type’ => ‘post’, ‘posts_per_page’ => -1 ) ); foreach ( $posts as $post ) { $attachments = get_children( array ( ‘post_parent’ => $post->ID, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘post_per_page’ => 25 ), ARRAY_A ); if( $attachments ) { //continue with what you were doing } }

Theme Customiser Image Control

The solution, needs a custom control object extending the original image control, and does an SQL query to grab the GUID and associated attachment ID on sanitisation. Not nice, kludgey, but it works $wp_customize->add_setting( ‘customimage’, array( ‘default’ => $default, ‘capability’ => ‘edit_theme_options’, ‘type’ => ‘option’, ‘sanitize_callback’ => array( ‘ICIT_Customize_Image_Control_AttID’, ‘attachment_guid_to_id’ ), ‘sanitize_js_callback’ => array( ‘ICIT_Customize_Image_Control_AttID’, … Read more

Unattaching unlinked images

wordpress sucks in keeping media<=>content relationships. Part of the problem is that by default all media are public once they are uploaded and you have no way to know where are they are being used. Just because an image is not referenced anymore in its original post doesn’t mean that it is not referenced at … Read more

delete attachment from front end

ended up using ajax in the end… html; <a class=”remImage” name=”<?php echo $attachment->ID; ?>” href=”#”><?php _e(‘delete’);?></a> <input type=”hidden” id=”att_remove” name=”att_remove[]” value=”<?php echo $attachment->ID; ?>” /> <input type=”hidden” name=”nonce” id=”nonce” value=”<?php echo wp_create_nonce( ‘delete_attachment’ ); ?>” /> jquery $(‘.remImage’).live(‘click’, function() { var attID = jQuery(this).attr(‘name’); jQuery.ajax({ type: ‘post’, url: ‘/wp-admin/admin-ajax.php’, data: { action: ‘delete_attachment’, att_ID: jQuery(this).attr(‘name’), … Read more

Replacing all attachment links in post with media file link

I found a nice snippet at this site for your functions.php that does the trick: add_shortcode( ‘gallery’, ‘file_gallery_shortcode’ ); function file_gallery_shortcode( $atts ) { $atts[‘link’] = ‘file’; return gallery_shortcode( $atts ); } I’d actually been looking unsuccessfully for the same thing, so I’m glad you asked.

Password protect some uploaded files, so only logged-in users can view them

See my question and answer again, you linked it already in your question: simple solution for restricting access to (some) uploads/downloads The solution isn’t that hairy the only necessity – besides configuring the plugin correct – is to create a .htaccess, which you could do programmatically. For that you have to automate the creation of … Read more