WordPress: 定时更新Twenty Fifteen主题的颜色配置

避免手动修改颜色的麻烦

最近在用Twenty Fifteen主题,用了一下颜色功能.发现挺好用.有个想法,就是白天颜色自动变成亮色,晚上颜色自动变成暗色.

找了一下资料,发现颜色配置存储在wp_options表里面,用列option_name值为’theme_mods_twentyfifteen’的字段存储.

一下就好办多了.

wordpress logo
image-3028

定时任务和Python脚本

如果使用了Autoptimize或者其它缓存插件,在更新颜色的Shell脚本里面一定要先更新颜色,然后清除一次缓存.

Python脚本

Python版本: 需要使用Python3.6+.

首先需要安装Python连接MySQL的插件:

pip install mysql-connector-python

有的可能需要使用:

pip3 install mysql-connector-python


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import mysql.connector
from mysql.connector import errorcode
import sys

'''

定时更新Wordpress的Twenty Fifteen主题的颜色配置.

安装: pip install mysql-connector-python


调用: python3 BckfCNUpdateTheme.py blueStyle
调用: python3 BckfCNUpdateTheme.py darkStyle
调用: python3 BckfCNUpdateTheme.py yellowStyle
调用: python3 BckfCNUpdateTheme.py pinkStyle
调用: python3 BckfCNUpdateTheme.py purpleStyle

'''


# 配色yellow 黄色
yellowStyle='a:7:{i:0;b:0;s:18:"custom_css_post_id";i:2941;s:16:"background_color";s:6:"f4ca16";s:12:"color_scheme";s:6:"yellow";s:17:"sidebar_textcolor";s:7:"#111111";s:23:"header_background_color";s:7:"#ffdf00";s:12:"header_image";s:13:"remove-header";}'

# 配色 粉色
pinkStyle='a:7:{i:0;b:0;s:18:"custom_css_post_id";i:2941;s:16:"background_color";s:6:"ffe5d1";s:12:"color_scheme";s:4:"pink";s:17:"sidebar_textcolor";s:7:"#ffffff";s:23:"header_background_color";s:7:"#e53b51";s:12:"header_image";s:13:"remove-header";}'

# 配色 purple 紫色
purpleStyle='a:7:{i:0;b:0;s:18:"custom_css_post_id";i:2941;s:16:"background_color";s:6:"674970";s:12:"color_scheme";s:6:"purple";s:17:"sidebar_textcolor";s:7:"#ffffff";s:23:"header_background_color";s:7:"#2e2256";s:12:"header_image";s:13:"remove-header";}'

# 配色blue 蓝色
blueStyle='a:7:{i:0;b:0;s:18:"custom_css_post_id";i:2941;s:16:"background_color";s:6:"e9f2f9";s:12:"color_scheme";s:4:"blue";s:17:"sidebar_textcolor";s:7:"#ffffff";s:23:"header_background_color";s:7:"#55c3dc";s:12:"header_image";s:13:"remove-header";}'

# 配色dark. 暗色
darkStyle='a:7:{i:0;b:0;s:18:"custom_css_post_id";i:2941;s:16:"background_color";s:6:"111111";s:12:"color_scheme";s:4:"dark";s:17:"sidebar_textcolor";s:7:"#bebebe";s:23:"header_background_color";s:7:"#202020";s:12:"header_image";s:13:"remove-header";}'

def updateStyle(style):
    try:
      cnx = mysql.connector.connect(user='root',
                                    database='wordpress',host='127.0.0.1',password='123')
      cursor = cnx.cursor()
      result=cursor.execute("""update wp_options set option_value='{0}' where option_name='theme_mods_twentyfifteen' """.format(style))
      print(result)
      cnx.commit()
    except mysql.connector.Error as err:
      if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
      elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
      else:
        print(err)
    else:
        cursor.close()
        cnx.close()
    print("处理完成!~")

if __name__ == '__main__':
    if len(sys.argv)>1:
        styleStr=sys.argv[1]
        if styleStr=='blueStyle':
            updateStyle(blueStyle)
        elif styleStr=='darkStyle':
            updateStyle(darkStyle)
        elif styleStr=='yellowStyle':
            updateStyle(yellowStyle)
        elif styleStr=='pinkStyle':
            updateStyle(pinkStyle)
        elif styleStr=='purpleStyle':
            updateStyle(purpleStyle)

 

Shell脚本(在Shell脚本中写下面这段代码,并保存文件):

python3 BckfCNUpdateTheme.py blueStyle

定时任务(第一条每天晚上20点执行,第二条每天早上8点执行.):

0 20 * * * sh /opt/style01.sh
0 8 * * * sh /opt/style02.sh

如果没有使用缓存插件,也可以将调用Python程序的语句直接写在定时任务里面.