News

Article updates

Revert the media files urls to default format

Note! This is addressed for user who used early staging versions of WP Hide. At the moment, none of urls are being saved on database, everything is processed when being outputted.

Per plugin Documentation at Rewrite – Uploads when used the “Block uploads URL” option, Once the uploads url being changed, this will block any access to old URL’s format, which otherwise are still available. If this feature is turned on, all new images inserted within posts (or any custom post type) will use the new URL format.

This is quite powerful functionality as it will completely hide the original url structure of the files and actual physical location. However in case of plugin uninstall the urls will stay as is. But in such case, there are no help modules available to identify the correct mapping with the default structure for the files to be server correctly. An easy revert would be to use a small script will will take care of urls and revert back to default state.

Presuming the media urls where changed to ‘app/files’ while the default is ‘wp-content/uploads’, the following code can be used:



    require_once ('wp-config.php');
    
    $search_for     =   "app/files";
    $replacement    =   "wp-content/uploads";

    global $wpdb;
    $table_rows = $wpdb->get_results("SELECT * FROM " . $wpdb->posts    . " WHERE post_content LIKE '%". esc_sql ($search_for ) . "%' OR post_content LIKE '%".  esc_sql ( trim( json_encode( $search_for ), '"' ) ) ."%'");

    echo 'Processing Posts';
    foreach($table_rows as $data)
        {
            $meta_value             =   $data->post_content;
            $default_meta_value     =   $meta_value;
            
            if (is_serialized($meta_value)) $meta_value = unserialize($meta_value);
            $meta_value = element_block_replace_process($meta_value, $search_for, $replacement);
            $meta_value = maybe_serialize($meta_value);

            // update the value
            if ( $default_meta_value    !=  $meta_value )
                {
                    echo "
FOUND " . $data->ID; $query = "UPDATE " . $wpdb->posts . " SET post_content = '" . esc_sql($meta_value) . "' WHERE ID = '" . $data->ID . "'"; $results = $wpdb->get_results($query); } } echo '
Done'; echo '
Processing Postmeta'; //'%s\\\\/%' $search_for_ = trim( json_encode( $search_for ), '"' ); $search_for_ = str_replace( '\\', '\\\\\\\\', $search_for_ ); $table_rows = $wpdb->get_results("SELECT * FROM " . $wpdb->postmeta . " WHERE meta_value LIKE '%". esc_sql ($search_for ) . "%' OR meta_value LIKE '%". $search_for_ ."%'"); foreach($table_rows as $data) { $meta_value = $data->meta_value; $default_meta_value = $meta_value; $is_serialized = FALSE; if (is_serialized($meta_value)) $meta_value = unserialize($meta_value); if ( $meta_value !== $default_meta_value) $is_serialized = TRUE; $meta_value = element_block_replace_process($meta_value, $search_for, $replacement); $meta_value = element_block_replace_process($meta_value, trim( json_encode( $search_for ), '"' ), trim( json_encode( $replacement ), '"' )); $meta_value = maybe_serialize($meta_value); // update the value if ( $default_meta_value != $meta_value ) { echo "
FOUND " . $data->meta_id; $query = "UPDATE " . $wpdb->postmeta . " SET meta_value = '" . esc_sql($meta_value) . "' WHERE meta_id = '" . $data->meta_id . "'"; $results = $wpdb->get_results($query); } } function element_block_replace_process($data, $replace, $replace_with) { switch (gettype($data)) { case 'array': foreach($data as $key => $value) { $data[$key] = element_block_replace_process($value, $replace, $replace_with); } break; case 'object': foreach($data as $key => $value) { $data->$key = element_block_replace_process($value, $replace, $replace_with); } break; case 'string': $data = str_replace($replace, $replace_with, $data); break; } return $data; } echo '
All Done';

The code should be placed within a file in the root install of WordPress, then accessed through your-domain.com/file.php
Before running this a database back-up is highly recommended to be done just in case something go wrong.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Scroll to top