Skip to content

Instantly share code, notes, and snippets.

@faishal
Created August 12, 2013 10:36
Show Gist options
  • Save faishal/6209796 to your computer and use it in GitHub Desktop.
Save faishal/6209796 to your computer and use it in GitHub Desktop.
rtMedia fix broken media path . add function in plugin/theme and call yoursite.com/?fix_broken_media=true
function check_broken_media () {
global $wpdb;
$media_model = new RTMediaModel();
$ob_migration = new RTMediaMigration();
$sql = "select * from wp_postmeta m join wp_posts p on p.ID = m.post_id where meta_value like '%rtMedia%'";
$results = $wpdb->get_results ( $sql );
$upload_path = trim ( get_option ( 'upload_path' ) );
if ( empty ( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
$dir = WP_CONTENT_DIR . '/uploads';
} elseif ( 0 !== strpos ( $upload_path, ABSPATH ) ) {
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH
$dir = path_join ( ABSPATH, $upload_path );
} else {
$dir = $upload_path;
}
foreach ( $results as $row ) {
$row->meta_value = maybe_unserialize ( $row->meta_value );
if ( is_array ( $row->meta_value ) ) {
} else {
if ( ! file_exists ( trailingslashit ( $dir ) . $row->meta_value ) ) {
//echo $row->meta_value . "-- <br />";
$file = explode ( "/", $row->meta_value );
$cmd = "cd " . trailingslashit ( $dir ) . "rtMedia && find . -name '" . $file[ count ( $file ) - 1 ] . "' ";
$data = exec ( $cmd );
$file_path = str_replace ( "/./", "/", trailingslashit ( $dir ) . "rtMedia/" . $data );
if ( file_exists ( str_replace ( "/./", "/", trailingslashit ( $dir ) . "rtMedia/" . $data ) ) ) {
$new_path = str_replace ( trailingslashit ( $dir ), "", $file_path );
//echo $new_path;
//update meta
//update activity
$ob_migration->search_and_replace ( $row->meta_value, $new_path );
$sql1 = $wpdb->prepare ( "update $wpdb->postmeta set meta_value = replace(meta_value,%s,%s)", $row->meta_value, $new_path );
$sql2 = $wpdb->prepare ( "update $wpdb->posts set guid = replace(guid,%s,%s)", $row->meta_value, $new_path );
$wpdb->query ( $sql1 );
$wpdb->query ( $sql2 );
} else {
echo $row->meta_value . "-- <br />";
}
}
}
}
exit;
}
if ( isset ( $_REQUEST[ "fix_broken_media" ] ) ) {
check_broken_media ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment