Ask your WordPress questions! Pay money and get answers fast! (more info)

Remove all instances on tags in post_content using mysql WordPress

I need to remove all instances of inline <img> tags in all posts/page.

I'm hoping to do this via phpMyAdmin but lack the mysql knowledge to do so.

Thanks for the help.

Answers (4)

2013-10-18

zebra webdesigns answers:

Hello Denis

I would like to guide you or do this for you.
You can contact me through skype id: bhuvan530531
my email [email protected]

and just for clarification. you need to remove the <img> </img> tag alone or need to remove the image URL too.
Make sure you have a database backup or else I will create a backup for you.

Have a great day

2013-10-18

Nirmal answers:

You should export your database to a .sql dump file, than make a copy of it to backup. Then you should write a small function to file_get_contents of that sql file, and run on it a preg_replace that will find the pattern you mentioned and replace it with null. Then, import the new database file again to MySQL.

<?php

// 1: get the SQL content from the file

$orig_file = file_get_contents('/path/to/filename.sql');


// 2: image only replacement

$pattern = '/<img (.+?)pix\/spotlight(.+?)\/>/i';

$replace_with = '';

$replace = preg_replace($pattern, $replace_with, $orig_file);



// 3: write the updated SQL content to a file

file_put_contents('/path/to/new_file.sql', $replace);

?>

2013-10-18

Navjot Singh answers:

Use the solution mentioned [[LINK href="http://wordpress.stackexchange.com/a/116353/5730"]]here[[/LINK]]. Just load WP dashboard once after using the code in theme's function.php and images should be gone. Have tested it. It works.


Navjot Singh comments:

To delete from pages as well, modify the query mentioned on the link as

function remove_images_form_past_posts() {
if ( get_transient('images_removed_from_past') ) return;
$args = array ( 'nopaging' => 1, 'post_type' => array('post','page') );
$posts = get_posts( $args );
if ( $posts ) {
foreach ( $posts as $post ) {
$newcontent = preg_replace('/<img[^>]+\>/i', '', $post->post_content);
$newpost = array( 'ID' => $post->ID, 'post_content' => $newcontent);
wp_update_post($newpost);
}
set_transient('images_removed_from_past', 1);
}
}

add_action('admin_init','remove_images_form_past_posts');

2013-10-18

Eric P. answers:

<strong>@Navjot Singh</strong>'s solution works, and is the cleanest/easiest to do.

I would put that in as a plugin, though you could temporarily add it to your theme's functions.php file.

To make it a plugin, just add this to the top:
<?php
/*
Plugin Name: Remove images from posts and pages.
*/


And put the "?>" close php tag (with no newline/carriage return) after Navjot's code.

Activate the plugin, deactivate it and delete it.