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.