因为服务器本身配置比较低,所以对于主题使用的几个库,都准备采用引用外部静态资源的方式,刚好学习了如何利用wordpress的自定义组件添加一些设置项,于是在自定义界面中,为几个静态库设置了一个名为静态资源的节点,并且依次设置了默认值:
<?php
function august_customize_register( $wp_customize ) {
$wp_customize -> add_section( 'august_static', array(
'title' => '静态资源' ,
'priority' => 160,
));
$wp_customize->add_setting('august_options[jquery]',array(
'default' => 'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js',
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[jquery]', array(
'label' => '主题jquery包',
'type' => 'textarea',
'section' => 'august_static',
));
$wp_customize->add_setting('august_options[headroom]',array(
'default' => 'https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/headroom/0.12.0/headroom.min.js',
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[headroom]', array(
'label' => '主题headroom包',
'type' => 'textarea',
'section' => 'august_static',
));
$wp_customize->add_setting('august_options[prism]',array(
'default' => 'https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.26.0/prism.min.js',
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[prism]', array(
'label' => '主题prism的js包',
'type' => 'textarea',
'section' => 'august_static',
));
$wp_customize->add_setting('august_options[prismcss]',array(
'default' => 'https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.26.0/themes/prism.min.css',
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[prismcss]', array(
'label' => '主题prism的css包',
'type' => 'textarea',
'section' => 'august_static',
));
}
add_action( 'customize_register', 'august_customize_register' );
?>
此时在自定义界面上已经有了“静态资源”,这个节点,也正确的展现了默认值。但是问题来了,无法在页面调用我的设置项“august_options”,查看了一下wordpress的options.php页,发现并没有生成这一个自定义项目。这默认值合着就是一个提示的作用,尝试对其中一个项目做了一下修改(就删除了一个字母),此时页面右上角“保存并发布”按钮亮起,选择保存。重新查看了一下options.php页,发现所修改的那项数据被正确生成了,但是其他的默认值仍旧没有提交;
这默认值还需要修改下才生效有点不符合用户的习惯,默认值较多的情况下,挨个让用户去修改也过于繁琐,于是考虑用add_option的方法,首次使用主题自定义功能时检测一下有没有生成“august_options”这一项目,如果没有则直接添加,并且为其加入一些默认的值。修改后的程序如下:
<?php
function august_customize_register( $wp_customize ) {
if(!get_option('august_options')){
add_option('august_options',array(
'jquery' => 'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js',
'headroom' => 'https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/headroom/0.12.0/headroom.min.js',
'prism' => 'https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.26.0/prism.min.js',
'prismcss' => 'https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/prism/1.26.0/themes/prism.min.css',
));
};
$wp_customize -> add_section( 'august_static', array(
'title' => '静态资源' ,
'priority' => 160,
));
$wp_customize->add_setting('august_options[jquery]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[jquery]', array(
'label' => '主题jquery包',
'type' => 'textarea',
'section' => 'august_static',
));
$wp_customize->add_setting('august_options[headroom]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[headroom]', array(
'label' => '主题headroom包',
'type' => 'textarea',
'section' => 'august_static',
));
$wp_customize->add_setting('august_options[prism]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[prism]', array(
'label' => '主题prism的js包',
'type' => 'textarea',
'section' => 'august_static',
));
$wp_customize->add_setting('august_options[prismcss]',array(
'transport' => 'postMessage',
'type' => 'option',
));
$wp_customize->add_control( 'august_options[prismcss]', array(
'label' => '主题prism的css包',
'type' => 'textarea',
'section' => 'august_static',
));
}
add_action( 'customize_register', 'august_customize_register' );
?>
这样当用户点击主题自定义页面后,系统会查询options.php页面中是否已有'august_options'字段,如果没有该字段,则自动生成该字段,并生成一些预设值。