database - How do I loopiterate through posts to edit all img tags?

Heads up, I'm pretty new to Wordpress... But I need to loop through multiple posts and change all 'data-src�

Heads up, I'm pretty new to Wordpress... But I need to loop through multiple posts and change all 'data-src' attributes to 'src' so my images will display. What's the way/best way to go about doing this? Would appreciate any info on this topic. Thanks.

Heads up, I'm pretty new to Wordpress... But I need to loop through multiple posts and change all 'data-src' attributes to 'src' so my images will display. What's the way/best way to go about doing this? Would appreciate any info on this topic. Thanks.

Share Improve this question asked Mar 19, 2019 at 16:16 Evan Meredith-DaviesEvan Meredith-Davies 1531 gold badge1 silver badge4 bronze badges 3
  • so you have raw html in the post content that contains data-src? – mrben522 Commented Mar 19, 2019 at 16:18
  • That's right. I have over 500 different posts and most contain images. Although the only way I could bulk upload the data (CSV) was using an importer that converted my data to posts although the plugin would remove the src tag, therefor my way around it was to change 'src' to 'data-src' and change it back after the data was uploaded. <img data-alt="" data-src="/cms/photo/newsheadlines/Dec_3.jpg" style="width: 100%;" /> – Evan Meredith-Davies Commented Mar 19, 2019 at 16:22
  • 1 I'd add to any answer the usual warnings about database backups before making changes like that. I'd test on a backup copy to make sure that your syntax doesn't affect other things by mistake. If you don't want to change the actual content, some client-side JS against the content could be done at page display time. Although that wouldn't affect anyone that is not allowing client-side JS. – Rick Hellewell Commented Mar 19, 2019 at 18:53
Add a comment  | 

2 Answers 2

Reset to default 0

You can loop through all your posts and use regex to replace that string. The quick and dirty version would be to just put this in a page template, assign it to a page and then visit said page. If you want something a little cleaner then modify this code to work with something like WP Utility Script Runner.

$posts_to_clean = get_posts(array(
    'posts_per_page' => -1,
));
echo 'Posts to clean: ' . count($posts_to_clean) . '<br>';
foreach ($posts_to_clean as $dirty_post) {
    $dirty_post->post_content = preg_replace('`(data-src)`', 'src', $dirty_post->post_content);
    $updated = wp_update_post($dirty_post, true);
    if (!is_wp_error($updated)) {
        echo 'Updated post ' . $updated . '<br>';
    } else {
        echo 'Unable to update post ' . $dirty_post->ID . '<br>';
    }
}
echo 'Complete!'

You could also do this directly in your phpmyadmin by a quick search and replace:

Launch phpMyAdmin. Then, you will need to click on your WordPress database name on the left and then click on the SQL tab at the top. Enter this code (be sure to change the table prefix if yours is different)

update wp_posts set post_content =
replace(post_content,'data-src','src');

Then click "go".

And ANOTHER option! Although I don't necessarily like plugins to do this work I know that there is one called Better Search Replace that will do this from within the the dashboard with a simple GUI as well.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745674746a4639618.html

相关推荐

  • database - How do I loopiterate through posts to edit all img tags?

    Heads up, I'm pretty new to Wordpress... But I need to loop through multiple posts and change all 'data-src�

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信