Skip to content

Instantly share code, notes, and snippets.

@ianthekirkland
Forked from ttscoff/wp_mangler.rb
Created August 12, 2012 01:05
Show Gist options
  • Save ianthekirkland/3328544 to your computer and use it in GitHub Desktop.
Save ianthekirkland/3328544 to your computer and use it in GitHub Desktop.
wp_mangler: runs a replace on WordPress post content
#!/usr/bin/env ruby
# Requires 'sequel' and 'mysql' rubygems
require 'rubygems'
require 'sequel'
require 'cgi'
def e_sql(str)
str.to_s.gsub(/(?=[\\])/, "\\")
end
def process_post(input)
# do your thing
input
end
def process_db(db_name = '', user='', pass='', host = 'localhost', db_prefix = '')
db = Sequel.mysql(db_name, :user => user, :password => pass, :host => host)
db["select post_content, ID from wp_#{db_prefix}posts where post_status = 'publish' and post_type = 'post'"].each do |post|
content = post[:post_content]
content = e_sql(process_post(content)) # run process_post on post_content, escape result
update_ds = db["UPDATE wp_#{db_prefix}posts SET post_content = ? WHERE ID = ?",content,post[:ID]]
update_ds.update # replace the existing content with the new content
end
end
# process_db (dbname, username, password, host, dbprefix)
process_db('database','user','pass','localhost','tableprefix_')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment