Skip to content

Instantly share code, notes, and snippets.

@pyxze
Created October 16, 2013 01:17
Show Gist options
  • Save pyxze/7001177 to your computer and use it in GitHub Desktop.
Save pyxze/7001177 to your computer and use it in GitHub Desktop.
WordPress utility script
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
print "1. Update WordPress password."
print "2. Update WordPress site and home URLs."
print "3. Disable plugins."
print "4. Reset theme."
choice = raw_input("What would you like to do? ")
import re
f = open("wp-config.php")
t = f.readlines()
for i in t:
pdb = re.compile(r".+DB_NAME', '(.+)'\);")
if re.match(pdb, i):
dbname = re.match(pdb, i).group(1)
puser = re.compile(r".+DB_USER', '(.+)'\);")
if re.match(puser, i):
dbuser = re.match(puser, i).group(1)
ppass = re.compile(r".+DB_PASSWORD', '(.+)'\);")
if re.match(ppass, i):
dbpass = re.match(ppass, i).group(1)
ppre = re.compile(r"\$table_prefix[ ]{1,2}= '(.+)';")
if re.match(ppre, i):
dbpre = re.match(ppre, i).group(1)
import MySQLdb as mdb
if choice == "1":
import hashlib
import random
import string
con = mdb.connect('localhost', dbuser, dbpass, dbname)
cur = con.cursor()
cur.execute("SELECT * from %susers" % dbpre)
rows = int(cur.rowcount)
users = []
for i in range(rows):
row = cur.fetchone()
print row[0], row[1], row[4]
cur.close()
con.close()
ans = raw_input("Which row do you want to update? ")
m = hashlib.md5()
newpass = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for x in range(12))
print "The new password is:", newpass
m.update(newpass)
newpass = m.hexdigest()
con = mdb.connect('localhost', dbuser, dbpass, dbname)
cur = con.cursor()
cur.execute("UPDATE %susers SET user_pass='%s' WHERE ID='%s'" % (dbpre, newpass, ans))
cur.close()
con.commit()
con.close()
elif choice == "2":
wpurl = raw_input("What should the URL be? ")
con = mdb.connect('localhost', dbuser, dbpass, dbname)
cur = con.cursor()
cur.execute("UPDATE wp_options SET option_value='%s' WHERE option_name='siteurl'" % wpurl)
cur.execute("UPDATE wp_options SET option_value='%s' WHERE option_name='home'" % wpurl)
cur.close()
con.commit()
con.close()
elif choice == "3":
con = mdb.connect('localhost', dbuser, dbpass, dbname)
cur = con.cursor()
cur.execute('update wp_options set option_value="a:0:{}" where option_name="active_plugins"');
cur.close()
con.commit()
con.close()
elif choice == "4":
con = mdb.connect('localhost', dbuser, dbpass, dbname)
cur = con.cursor()
cur.execute('UPDATE wp_options SET option_value = "default" WHERE option_name = "template"');
cur.execute('UPDATE wp_options SET option_value = "default" WHERE option_name = "stylesheet"');
cur.execute('UPDATE wp_options SET option_value = "default" WHERE option_name = "current_theme"');
cur.close()
con.commit()
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment