• Hi to all
    I’m looking for a way to automatically attach images to the post where they are (were) inserted.
    To be clear, I migrated a photoblog from cutenews to WordPress. I left all images in the directory where they were uploaded. I populated the media library with them through the plugin “Add from server”. But I can’t find a way to automatically attach them to the post where they are, because all cutenews post contents were simply like
    <img alt="" src="https://site.org/cutenews/data/upimages/image.gif" />
    while now all media inserted through the “Add media” are like
    <img class="alignnone size-full wp-image-12530" alt="" src="/cutenews/data/upimages/image.jpg" width="550" height="652" />
    I see that the plugin “Media Library Assistant” can detect were every unattached image in inserted in…
    It would be enough even if I can edit the post and attach its images to the post I’m editing, without having to search manually for the post in media library with the “attach” command.

    Thanks a lot,
    Fede.

Viewing 15 replies - 1 through 15 (of 19 total)
  • Perhaps this might be better asked on the plugins support page?

    https://www.ads-software.com/support/plugin/add-from-server

    Thread Starter gnucco2

    (@gnucco2)

    Hi Josh
    I don’t think that this is an issue of “Add from server” plugin…it did what he had to do, populate my media library with all images…it can’t attach media to the post where they are inserted.

    Thanks

    After a lot of research through Internet, I’ve found that basically what I need is an automatic tool that would set the parent id of media to the post where they are inserted through the <img src…> tag…Media Library Assistant plugin can get the id of the post where unattached media are inserted, but it would need so much time to set manually the parent id to every media (I have about 6000 pictures to correct…)

    Having the same issue. Any luck Fede?

    Hi nberke
    I’ve just found by chance what I’m exactly trying to do…but since I can’t code, I don’t know where to put those pieces of code…

    https://wordpress.stackexchange.com/questions/78266/how-can-i-attach-hotlinked-images-in-posts-pages-within-the-same-server

    In short, you would need to modify the wordpress database, searching for unattached media and setting their parent_post field in database to the ID of the post where they are inserted…

    Anyone can help me make a plugin out of those? Thanks a lot…

    Fede

    Yeah, that looks like the right thing. Definitely would like a plugin, and would consider giving a small donation to anyone who makes it. Thanks!

    Hi
    I’m trying to make a little plugin out of it, but I would not use in a “production” environment because I’m not a skilled programmer so it would screw up a wordpress database.
    Anyway, there’s an error in the link I provided…of course the 4th line of the first piece of code is

    WHERE post_type = ‘attachment’ AND post_parent = ‘0’

    and not

    WHERE post_type = ‘attachment’ AND post_parent > ‘0’

    Hi
    finally I made a plugin which work as expected. I copy and paste the code found in https://wordpress.stackexchange.com/questions/78266/how-can-i-attach-hotlinked-images-in-posts-pages-within-the-same-server into an existing plugin (“Assign missing category”) and made some edits.
    Please make a database backup first!!!
    This is what I did (copy the code into a file named attach-inserted-images.php, zip it, and install it as a plugin)

    <?php
    /*
    Plugin Name: Attach inserted images
    Plugin URI:
    Description: Attach inserted images
    Version: 0.1
    Author: Fede
    Author URI: 
    
    == Changelog ==
    
    = 0.1 =
    * first release (June 11, 2013)
    
    */
    
    add_action('admin_menu', 'attach_inserted_images_add_pages');
    
    function attach_inserted_images_add_pages() {
    	add_submenu_page('edit.php', 'Attach Inserted Images', 'Attach Inserted Images', 'update_core', __FILE__, 'attach_inserted_images_options');
    }
    
    // displays the options page content
    function attach_inserted_images_options() {
    	if ( current_user_can('update_core') ) {
    	// variables for the field and option names
    		$hidden_field_name = 'attach_inserted_images_submit_hidden';
    
    		// See if the user has posted us some information
    		// If they did, this hidden field will be set to 'Y'
    		if ( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
    			attach_inserted_images();
    			// Put an options updated message on the screen ?>
    			<div class="updated"><p><strong><?php _e('Images attached.', 'attach-inserted-images'); ?></strong></p></div>
    		<?php } // Now display the options editing screen ?>
    
        <div class="wrap">
        <?php if ( !isset($_POST[ $hidden_field_name ]) || $_POST[ $hidden_field_name ] != 'Y' ) { ?>
    	<form method="post" id="attach_inserted_images_form">
        <h2><?php _e( 'Attach Inserted Images', 'attach-inserted-images'); ?></h2>
    	<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
    
    	<p>Press the button below to attach inserted images to the posts where they were inserted.</p>
    
    	<p class="submit">
    	<input type="submit" name="submit" value="<?php _e('Attach Inserted Images ?', 'attach-inserted-images'); ?>" class="button-primary" />
    	</p>
    	</form>
        <?php } // if ?>
    
    	<p><?php echo get_num_queries(); ?> queries. <?php timer_stop(1); ?> seconds.</p>
        </div>
    
    <?php } // if user can
    } // end function attach_inserted_images_options() 
    
    function attach_inserted_images() {
    
    global $wpdb;
    $lost = $wpdb->get_col( "
        SELECT ID FROM $wpdb->posts
        WHERE post_type = 'attachment' AND post_parent = '0' AND ID BETWEEN 3000 AND 3500
    " );
    
    $urls = array ();
    foreach ( $lost as $id )
        $urls[ $id ] = get_the_guid( $id );
    
    global $wpdb;
    foreach ( $urls as $id => $url )
    {
        $posts = get_posts( array ( 's' => $url, 'numberposts' => 1 ) );
    
        if ( ! $posts )
            continue;
    
        $parent_id = $posts[0]->ID;
        $wpdb->query(
            $wpdb->prepare(
                "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID = %d",
                $parent_id,
                $id
            )
        );
    }	
    
    } 
    
    // i18n
    $plugin_dir = basename(dirname(__FILE__)). '/languages';
    load_plugin_textdomain( 'attach_inserted_images', WP_PLUGIN_DIR.'/'.$plugin_dir, $plugin_dir );
    ?>

    As you can see, the main edit is the line
    WHERE post_type = 'attachment' AND post_parent = '0' AND ID BETWEEN 3000 AND 3500
    because I can’t edit php time limit of my server (30 seconds), so I need to restrict the operation on 500 rows at a time (in this case I look for unattached files with ID between 3000 and 3500, and change manually inside wordpress the values after every iteration.)
    I checked how many IDs I have with phpMyAdmin, or better, exporting the tables to Excel ??

    The plugin shows up in Posts edit section in the dashboard.

    Please be aware that I’m not a programmer, so use it carefully!!! It worked in my media environment, which had a lot of unattached media, already present in media library thanks to “Add to server plugin” (so they were of post_type = attachment in the database), those media are inserted into posts but had the value of parent_post = 0 in the database so they were unattached.
    If you have some doubts, please write in this post before using it in real world.

    gnucco3, does your plugin still work?

    @oomskaap:

    I am having the exact same problem as the OP. I have tested his plugin, and after wrangling around a bit, I seem to have gotten it to work. The only thing, since I use an admin thumbnail plugin to display the thumbnails in the admin panel, i had to regenerate my thumbnails.

    But other than that, it seems to be working fine.

    PS: I host my own web server, so I changed the WHERE statement above to 1000000 just to make sure I got everything. Before I realized I could do that, I used 0-1000, 1001-2000, etc. Usage may vary

    @dietrichmd

    I paid a programmer to modify a hidden gem plugin found on github.
    It works wonders and I had my over 15000 unattached images on 2000 posts reattached to the respective post with a breeze. If you ever need it let me know.

    that would be uber handy. ??

    @dietrichmd Shot. I’ll share it here:

    sockshare.com/file/1F0A9FFFDA8C33B8

    I wrote a quick readme file as well, which is included.

    Thanks a bunch. So far it seems to be working pretty well.

    Out of curiosity, what is the difference in this and the regular media tools plugin?

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Auto attach images to the post where they are inserted’ is closed to new replies.