• Is there a way to 1. detach and 2. reattach attachment-images from one post to another post?

    There are two very common ways of uploading an image to WordPress to place it into the Media Gallery.

    1. Via “add New” under the Media Tab in the admin.
    2. Inserting an image when writing a post and uploading the image from the overlay Media window/layer.

    On upload alone, the first method does NOT attach the uploaded image to any particular post, but one manually attaches it later.
    The second method attaches the uploaded image to the post it was uploaded through.

    When I use the first method, there is a way to sort the listing for “unattached images” which you can select and then attach to a particular post. However, there is no way afterwards, to detach and re-attach an image, without altogether deleting that image from the Media gallery and re-uploading it and attaching it to a different post.

    This is an entirely inefficient process, and while I appreciate all of the hard work that the WordPress team is putting in to making WordPress have media-gallery abilities, I think that a native WordPress option to detach and re-attach images to posts should be inserted into the base code to complete this functionality already half-offered.

    In the meantime, does anyone know of a plugin, php script or coding approach to attach, detach and reattach/re-assign images within the Media gallery that WordPress already has built into it?

Viewing 5 replies - 16 through 20 (of 20 total)
  • +1 for detaching/reattaching and attaching to multiple posts – this should be native. Why can’t I attach one image to multiple posts?

    Post this into your functions.php file, or a plugin file, and a new column will be created on the media page that will allow you to “re-attach”.

    add_filter("manage_upload_columns", 'upload_columns');
    add_action("manage_media_custom_column", 'media_custom_columns', 0, 2);
    
    function upload_columns($columns) {
    
    	unset($columns['parent']);
    	$columns['better_parent'] = "Parent";
    
    	return $columns;
    
    }
     function media_custom_columns($column_name, $id) {
    
    	$post = get_post($id);
    
    	if($column_name != 'better_parent')
    		return;
    
    		if ( $post->post_parent > 0 ) {
    			if ( get_post($post->post_parent) ) {
    				$title =_draft_or_post_title($post->post_parent);
    			}
    			?>
    			<strong><a href="<?php echo get_edit_post_link( $post->post_parent ); ?>"><?php echo $title ?></a></strong>, <?php echo get_the_time(__('Y/m/d')); ?>
    			<br />
    			<a class="hide-if-no-js" onclick="findPosts.open('media[]','<?php echo $post->ID ?>');return false;" href="#the-list"><?php _e('Re-Attach'); ?></a></td>
    
    			<?php
    		} else {
    			?>
    			<?php _e('(Unattached)'); ?><br />
    			<a class="hide-if-no-js" onclick="findPosts.open('media[]','<?php echo $post->ID ?>');return false;" href="#the-list"><?php _e('Attach'); ?></a>
    			<?php
    		}
    
    }

    Let me know if there are any problems with that code, I wrote it just now, didn’t fully test it. Good luck!

    Wow, thanks Andy. You are my hero ??

    We are working on a new photography theme and this is *exactly* what I was looking for. Well done!

    Very helpful – thanks Andy. I think you have a stray ‘<td>’ at the end of this line:-

    <a class="hide-if-no-js" onclick="findPosts.open('media[]','<?php echo $post->ID ?>');return false;" href="#the-list"><?php _e('Re-Attach'); ?></a></td>

    It would be great to also have a link to completely detach an image from a post. Any idea what the best way to do that would be?

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘Detach & Re-Attach Media Attachment Images from Posts’ is closed to new replies.