Created
July 26, 2012 11:57
-
-
Save ttscoff/3181658 to your computer and use it in GitHub Desktop.
wp_mangler: runs a replace on WordPress post content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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