To remove the sizes
and srcset
attributes (along with their values) from <img>
tags in your blog posts using WP Migrate DB Pro’s Find and Replace feature with regex, you can use the following approach:
Regex Solution
Use this regex pattern in the Find field to match and remove both sizes
and srcset
attributes, including their values:
\s*(sizes|srcset)="[^"]*"
- Explanation:
\s*
: Matches zero or more whitespace characters (to account for formatting variations).(sizes|srcset)
: Matches either thesizes
orsrcset
attribute.="[^"]*"
: Matches the attribute value enclosed in quotes (e.g.,"..."
), capturing everything between the quotes.
- Replace field: Leave it empty (
""
) to remove the matched attributes entirely.
Steps in WP Migrate DB Pro
- Go to the Find and Replace feature in WP Migrate DB Pro.
- In the Find field, enter the regex pattern:
\s*(sizes|srcset)="[^"]*"
- Check the Regex option to enable regular expression mode.
- In the Replace field, leave it blank (
""
). - Run a preview to ensure it matches the
sizes
andsrcset
attributes correctly. - Execute the replacement across your database.
Example
Before:
<img src="https://example.com/image.jpg" sizes="(max-width: 1024px) 100vw, 1024px" srcset="https://example.com/image-1024x683.jpg 1024w, https://example.com/image-300x200.jpg 300w" alt="" width="1024" height="683" />
After:
<img src="https://example.com/image.jpg" alt="" width="1024" height="683" />
Why Your Attempts Didn’t Work
Your patterns (<img[^>]+srcset="([^">]+)"
and <img[^>]+sizes="([^">]+"
) had a few issues:
- The capture group
([^">]+)
excludes>
and"
, but the values insidesizes
andsrcset
can contain complex strings (e.g., commas, spaces, or parentheses), and it doesn’t account for all cases. - The patterns were too restrictive and didn’t handle both attributes in a single pass.
- The
[^>]+
might have been too greedy, potentially missing some matches due to attribute ordering or whitespace.
Alternative: PHP Script
If the regex in WP Migrate DB Pro doesn’t work as expected, you can run a PHP script to process the content directly in your WordPress database. Here’s an example:
<?php
// Connect to WordPress
require_once('wp-load.php');
global $wpdb;
// Query all posts
$posts = $wpdb->get_results("SELECT ID, post_content FROM {$wpdb->posts} WHERE post_type="post"");
foreach ($posts as $post) {
// Remove sizes and srcset attributes
$new_content = preg_replace('/\s*(sizes|srcset)="[^"]*"/i', '', $post->post_content);
// Update the post content if changed
if ($new_content !== $post->post_content) {
$wpdb->update(
$wpdb->posts,
['post_content' => $new_content],
['ID' => $post->ID]
);
echo "Updated post ID: {$post->ID}\n";
}
}
?>
- Steps:
- Backup your database before running the script.
- Place the script in your WordPress root directory.
- Run it via the command line (
php script.php
) or a temporary admin page. - Verify the changes in your posts.
Notes
- Test First: Always test regex or scripts on a staging site or a backup to avoid accidental data loss.
- Case Sensitivity: The regex is case-insensitive with the
i
flag in PHP (/i
). WP Migrate DB Pro may not need this, depending on its regex engine. - Other Attributes: If you need to remove additional attributes (e.g.,
width
,height
), add them to the(sizes|srcset|other)
group in the regex.