Find and Replace text in the entire table using a MySQL query

Website has been hacked and they have injected javascript into every single post, page and product (woocommerce) - Editing every page manually would take for ever, we have over 3000 posts.

Is there a simple find and replace we could use to remove this javascript?

Topic hacked Wordpress

Category Web


Do you have SSH access? WP-CLI via search-replace.

$ wp search-replace '<script>bad javascript code</script>' '' --precise

By default, this only searches for tables registered in $wpdb. To overwrite this behaviour, you can use either the --all-tables or --all-tables-with-prefix flag. (Or pass table names to the command manually.)

Alternative: Easiest would be to download a dump, search/replace in that file and finally import again.

  1. Download complete db backup
  2. search & replace with your favourite text editor, replacing <script>bad javascript code</script> through an empty string
  3. Delete tables
  4. Upload your edited backup

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.