forked from herewithme/wordpress-cli-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relink-medias.php
70 lines (61 loc) · 2.53 KB
/
relink-medias.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// Increase PHP limits
@ini_set( 'memory_limit', '512M' );
@ini_set( 'max_execution_time', -1 );
// Place this file into master folder of WordPress
require( dirname(__FILE__) . '/wp-load.php' );
require_once(ABSPATH . 'wp-admin/includes/admin.php');
function hardFlush() {
// Like said in PHP description above, some version of IE (7.0 for example)
// will not 'update' the page if less then 256 bytes are received
// Send 250 characters extra
echo ' ';
echo ' ';
echo ' ';
echo ' ';
echo ' ';
flush();
ob_flush();
}
echo "\n".__('Begin the modification.');
hardFlush();
$attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'post_status' => null,
'post_parent' => null, // any parent
'output' => 'object',
) );
$total = count($attachments);
echo "\n".sprintf(__('Whe have %d attachments.'), $total);
hardFlush();
$i = 0;
foreach( $attachments as $attachment ) {
$i++;
// Update attachment link (double quote)
$result = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_content = REPLACE(post_content, %s, %s)", 'href="'.wp_get_attachment_url($attachment->ID), 'href="'.get_attachment_link($attachment->ID)) );
if ( is_int($result) && $result > 0 ) {
echo "\n".sprintf(__('%d/%d : Link modification OK for the media %d : %s.'), $i, $total, $attachment->ID, $attachment->post_title);
hardFlush();
} else {
// Echec
echo "\n".sprintf(__('%d/%d : An error occured with the media %d : %s. (no changes in DB)'), $i, $total, $attachment->ID, $attachment->post_title);
hardFlush();
}
/*
This part is optionnaly
// Update attachment link (simple quote)
$result = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_content = REPLACE(post_content, %s, %s)", "href='".wp_get_attachment_url($attachment->ID), "href='".get_attachment_link($attachment->ID)) );
if ( is_int($result) && $result > 0 ) {
echo "\n".sprintf(__('%d/%d : Link modification OK for the media %d : %s.'), $i, $total, $attachment->ID, $attachment->post_title);
hardFlush();
} else {
// Echec
echo "\n".sprintf(__('%d/%d : An error occured with the media %d : %s. (no changes in DB)'), $i, $total, $attachment->ID, $attachment->post_title);
hardFlush();
}
*/
}
echo "\n".__('Process finished.');
?>