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 Documentation at Rewrite – Uploads when using the “Block uploads URL” option, it will block any access to old/default 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 a powerful functionality as it will completely hide the original URL structure of the files and actual physical location. However, in the case of plugin uninstall, in rare occasions mostly caused by a conflict with another code, the URLs will stay as-is. In such a case, there are no help modules available to identify the correct mapping with the default structure for the files to be served correctly. An easy fix will be to revert all URLs to default, using the following script.

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



   include_once('wp-load.php');
    global $wpdb;
    
    
    $replacements       =   array ( 
                                    '/app/files/'                        =>  '/wp - content/uploads/', 
                                    '/cnt/themes/oceanwpmedia/'  =>  '/wp - content/uploads/'  
                                    );
    
    /**
    * Format is
    * Table name =>  { column to update => table unique id }
    * 
    * @var mixed
    */
    $replacements_map   =   array (
                                    $wpdb->prefix . 'posts'   =>  array ( 
                                                                'post_content'    =>  'ID'
                                                                ),
                                                                
                                    $wpdb->prefix . 'postmeta'   =>  array ( 
                                                                'meta_value'    =>  'meta_id'
                                                                ),
                                                                
                                    $wpdb->prefix . 'options'   =>  array ( 
                                                                'option_value'    =>  'option_id'
                                                                ),
                                    
                                    $wpdb->prefix . 'termmeta'   =>  array ( 
                                                                'meta_value'    =>  'meta_id'
                                                                ),
                                    
                                    $wpdb->prefix . 'revslider_slides'   =>  array ( 
                                                                                'params'    =>  'id',
                                                                                'layers'    =>  'id'
                                                                                ),
                                    $wpdb->prefix . 'revslider_sliders'   =>  array ( 
                                                                                'params'    =>  'id'
                                                                                ),
                                    
                                    $wpdb->prefix . 'revslider_static_slides'   =>  array ( 
                                                                                'params'    =>  'id',
                                                                                'layers'    =>  'id'
                                                                                ),

                                    

                                    );
    
    
    foreach ( $replacements as $replaced_word => $replacement_word )
        {
        
            $replaced__json   =   json_encode_process( $replaced_word );
            
            //process every option element
            foreach ( $replacements_map     as  $table  =>  $fields )
                {
                    $table_rows = $wpdb->get_results("SELECT * FROM " . $table);
                    
                    foreach ( $fields as    $field  =>  $field_unique_id )
                        {
                            foreach ($table_rows as $data)
                                {
                                    $meta_value = $data->$field;
                                    
                                    //check if the field contain any of the search values
                                    if ( ! strposa( $meta_value , $replaced_word )    &&  ! strposa( $meta_value , $replaced__json ) )
                                        continue;
                                    
                                    if(is_serialized( $meta_value ))
                                        $meta_value =   unserialize($meta_value);
                                    
                                    $meta_value_updated = element_block_replace_process($meta_value, $replaced_word, $replacement_word);
                                    
                                    $meta_value_updated =   maybe_serialize( $meta_value_updated );
                                    
                                    if ( $meta_value_updated == $meta_value )
                                        continue;
                                    
                                    echo "\n
        Found: Table ".  $table .", Field ". $field_unique_id ." " . $data->$field_unique_id . "
"; //update the value $query = "UPDATE " . $table . " SET ". $field ." = '". esc_sql ( $meta_value_updated ) ."' WHERE ". $field_unique_id ." = '". $data->$field_unique_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 ); //try also jsonencoded format $replaced_ = json_encode_process ( $replace ); $replace_ = json_encode_process ( $replace_with ); $data = str_replace ( $replaced_, $replace_, $data ); break; } return $data; } function json_encode_process( $element ) { $element = trim( json_encode( $element ) , '"'); return $element; } function strposa( $haystack, $needle, $offset=0 ) { if ( !is_array ( $needle ) ) $needle = array($needle); foreach ( $needle as $query ) { if( strpos ( $haystack, $query, $offset ) !== false ) return true; } return false; } echo "\nAll 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