Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter gnucco3

    (@gnucco3)

    Thanks a lot. It’s working now.

    Bye, Fede.

    Thread Starter gnucco3

    (@gnucco3)

    Hi David
    please have a look to the post
    http://wordpress.org/support/topic/auto-attach-images-to-the-post-where-they-are-inserted
    where I show how I solved my problem. Feel free to use that piece of code into MLA, if you want. Now I only have in my media library, under “unattached” category, only orphan images, which are not present (or has errors) in my posts.
    Thanks for your time…

    Hi
    finally I made a plugin which work as expected. I copy and paste the code found in http://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.

    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 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…

    http://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

    Thread Starter gnucco3

    (@gnucco3)

    Hi David
    I’ve just sent you a message with my mail. Thank you very much for your attention. If I understand correctly, you will be able to sort media by “inserted in”?
    I’m sorry I can’t code…but if I could, I would add a “simple” link, in “Attached to” column, just after “(unattached)”, to directly attach the media to the post, like “Attach to its post” where “its” is the number you already have in “Inserted in” column. Of course this would mean 5000 click, but for me it would be a lot better than writing by hand the id of the post in edit mode.
    Anyway, thanks again.

    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…)

Viewing 7 replies - 1 through 7 (of 7 total)