News

Article updates

Revert the media files urls to default format

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');

global $wpdb;
$table_rows = $wpdb->get_results("SELECT * FROM " . $wpdb->posts);

foreach($table_rows as $data)
{
	$meta_value = $data->post_content;
	if (is_serialized($meta_value)) $meta_value = unserialize($meta_value);
	$meta_value = element_block_replace_process($meta_value, 'computerkeen_up', '/wp-content/uploads');
	$meta_value = maybe_serialize($meta_value);

	// update the value

	$query = "UPDATE " . $wpdb->posts . " SET post_content    =   '" . $meta_value . "' WHERE ID = '" . $data->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.

Scroll to top