How can I hide media library images from general users?

To let the current users only view his/her uploaded attachments, add the following code to your themes actions: add_filter( ‘posts_where’, ‘devplus_wpquery_where’ ); function devplus_wpquery_where( $where ){ global $current_user; if( is_user_logged_in() ){ // logged in user, but are we viewing the library? if( isset( $_POST[‘action’] ) && ( $_POST[‘action’] == ‘query-attachments’ ) ){ // here you … Read more

How do I exclude all images from a wp_query?

Pretty much the solution is to include all mimes except images. WordPress has a nifty little function where it keeps all it’s accepted mime-types called get_allowed_mime_types() ( cleverly named ) which returns an Array() of mimes. All we need to do is get the difference between the returned array and the array of mime-types we … Read more

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.

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