首页>建站相关>用javascript实现wordpress主题后台设置页面

用javascript实现wordpress主题后台设置页面

这是目前为止自己写过的最长的代码,利用js生成表单,然后读取后台的数据,赋值给表单。用户修改表单的值后,整理数据提交给后台。因为不太熟悉wordpress的语法,但是又想实现一个后台设置页面,所以尝试了一下这个比较冷门的方式。

完整代码

2022年10月21日

add_action('admin_menu', 'theme_options');   
function theme_options(){   
    add_theme_page( 'August主题设置', '选项', 'administrator', 'theme_options_page','display_function');   
} 
function display_function(){  ?> 
    <?php 
        wp_nonce_field('update-options');
        $options=get_option('August_options');
        if($options){
            echo '<script>var options='.$options.';</script>';
        }else{
            echo '<script>var options={};</script>';
        }
    ?>   
<script>
var ThemeForm={
    form:'',
    formid:'august_options_form',
    formvalue:'august_options',
    submitname:'august_options_save',
    title:'August主题设置页',
    titleclass:'august_options_title',
    containerclass:'august_options_container',
    labelclass:'august_options_label',
    inputclass:'august_options_input',
    descriptionclass:'august_options_description',
    formlist:[],
       setUp:function(title,formid,formvalue){
        if(title){ThemeForm.title=title;}
        if(formid){ThemeForm.formid=formid;}
        if(formvalue){ThemeForm.formvalue=formvalue;}
        ThemeForm.form=document.createElement('form');
        ThemeForm.form.id=ThemeForm.formid;
                ThemeForm.form.method='post';  
        ThemeForm.form.action='options.php';
        if(document.getElementById('wpbody-content')){
            parent=document.getElementById('wpbody-content');
            parent.appendChild(ThemeForm.form);
        }
        title=document.createElement('h3');
        title.classList=ThemeForm.titleclass;
        title.textContent=ThemeForm.title;
        ThemeForm.form.appendChild(title);
    },
    createFooter:function(){
        var wpnonce=document.getElementsByName('_wpnonce')[0];
        var wpreferer=document.getElementsByName('_wp_http_referer')[0];
        ThemeForm.form.appendChild(wpnonce);
        ThemeForm.form.appendChild(wpreferer);
        var input_data=document.createElement('input');
        input_data.type='hidden';
        input_data.id=ThemeForm.formvalue;
        input_data.name=ThemeForm.formvalue;
        input_data.value='update';
        var input_action=document.createElement('input');
        input_action.type='hidden';
        input_action.id='page_value';
        input_action.name='action';
        input_action.value='update';
        var input_options=document.createElement('input');
        input_options.type='hidden';
        input_options.id='page_options';
        input_options.name='page_options';
        input_options.value=ThemeForm.formvalue;
        container=ThemeForm.makeContainer();
        input_submit=document.createElement('input');
        input_submit.type='submit';
        input_submit.name=ThemeForm.submitname;
        input_submit.value='Save';
        input_submit.onclick=ThemeForm.makeJson;
        container.appendChild(input_submit);
        ThemeForm.form.appendChild(input_data);
        ThemeForm.form.appendChild(input_action);
        ThemeForm.form.appendChild(input_options);
        ThemeForm.form.appendChild(container);
    },
    setClass:function(title,container,label,input,description){
        ThemeForm.titleclass=title;
        ThemeForm.containerclass=container;
        ThemeForm.labelclass=label;
        ThemeForm.inputclass=input;
        ThemeForm.descriptionclass=description;
    },
    makeContainer:function(){
        var container=document.createElement('p');
        container.classList=ThemeForm.containerclass;
        return container;
    },
    makeLabel:function(text,parent){
        var label=document.createElement('label');
        label.textContent=text+':';
        label.classList=ThemeForm.labelclass;
        parent.appendChild(label);
    },
    makeDescription:function(text,parent){
        var desc=document.createElement('p');
        desc.classList=ThemeForm.descriptionclass;
        desc.textContent=text;
        parent.appendChild(desc);
    },
        //单行文本
    makeInput:function(name,value,parent){
        var input=document.createElement('input');
        var obj={};
        input.type='input';
        input.name=name;
        if(options&&options[name]){
            input.value=options[name];
        }else if(value){
            input.value=value;
        }else{
                    input.value='';  
                }
        var obj={};
        obj.element=input;
        obj.getValue=function(){
            options[obj.element.name]=obj.element.value;
        }
        ThemeForm.formlist.push(obj);
        parent.appendChild(input);
    },
        //多行文本
    makeTextarea:function(name,value,parent){
        var input=document.createElement('textarea');
        input.type='textarea';
        input.name=name;
        if(options&&options[name]){
            input.value=options[name];
        }else if(value){
            input.value=value;
        }else{
                    input.value='';  
                }
                var obj={};
        obj.element=input;
        obj.getValue=function(){
            options[obj.element.name]=obj.element.value;
        }
        ThemeForm.formlist.push(obj);        
        parent.appendChild(input);
    },
        //单选框
    makeRadio:function(name,value,option,parent){
        radios=[];
        var choice;
        if(options&&options[name]){
            choice=options[name];
        }else{
            choice=value;
        }
        for(i in option){
            var input=document.createElement('input');
        input.type='radio';
        input.value=i;
        input.name=name;
        if(choice==input.value){
            input.checked=true;
        }
            radios.push(input);
        var label=document.createElement('label');
            label.textContent=option[i];
            parent.appendChild(input);
        parent.appendChild(label);
        }
        var obj={};
            obj.element=radios;
            obj.getValue=function(){
            for(i in obj.element){
                if(obj.element[i].checked===true){
                    options[obj.element[i].name]=obj.element[i].value;
                }
            }
        }
    ThemeForm.formlist.push(obj);
    },
    //多选框
    makeCheckbox:function(name,value,option,parent){
        checks=[];
        var choice;
        if(options&&options[name]){
            choice=options[name];
        }else{
            choice=value;
        }
        for(i in option){
            var input=document.createElement('input');
            input.type='checkbox';
            input.value=i;
            input.name=name;
            if(choice.includes(input.value)){
                input.checked=true;
            }
            checks.push(input);
            var label=document.createElement('label');
            label.textContent=option[i];
            parent.appendChild(input);
            parent.appendChild(label);
        }
        var obj={};
        obj.element=checks;
        obj.getValue=function(){
                options[name]=[];
        for(i in obj.element){
            if(obj.element[i].checked===true){
            options[name].push(obj.element[i].value);
                }
            }
        }
        ThemeForm.formlist.push(obj);
    },
    createNew:function(label,description,name,type,value,option){
        container=ThemeForm.makeContainer();
        ThemeForm.makeLabel(label,container);
        switch(type)
        {
        case 'input':
            ThemeForm.makeInput(name,value,container);
        break;
        case 'textarea':
            ThemeForm.makeTextarea(name,value,container);
        break;
        case 'radio':
            ThemeForm.makeRadio(name,value,option,container);
        break;
        case 'checkbox':
            ThemeForm.makeCheckbox(name,value,option,container);
        break;
        default:
        break;
        }
        ThemeForm.makeDescription(description,container);
        ThemeForm.form.appendChild(container);
    },
    makeJson:function(){
        for(i in ThemeForm.formlist){
            ThemeForm.formlist[i].getValue();
        };
        options=JSON.stringify(options);
        document.getElementById('august_options').value=options;
    }
}
//前置项目
ThemeForm.setUp();
//元素示例,可自定义,
//create参数:
//标签名,设置项说明,设置项id名,设置项的元素类型,默认值,默认选项
ThemeForm.createNew('测试1','这就是一个输入框测试','site-logo','input');
ThemeForm.createNew('测试2','这就是一个多行文本测试','test2','textarea');
ThemeForm.createNew('测试3','这就是一个单选测试','check1','radio','',{'red':'红色','green':'绿色的 ','blue':'蓝色','yellow':'黄色'});
ThemeForm.createNew('测试4','这就是一个多选测试','check2','checkbox','',{'red':'红色','green':'绿色的 ','blue':'蓝色','yellow':'黄色'});
//尾部项目
ThemeForm.createFooter();
</script>
<?php }?>

2022年10月24日

增加了一个选择框,description部分改用innerHTML以支持html标签的插入;
类名太多,暂时去掉了设置类的函数,均采用默认;

add_action('admin_menu', 'render_form');   
function render_form(){   
    add_theme_page( 'August主题设置', '选项', 'administrator', 'theme_options_page','theme_options');   
} 
function theme_options(){
    wp_enqueue_style( 'theme_options', get_template_directory_uri() . '/theme_options.css',false,'1.34','all'); 
    wp_nonce_field('update-options');
    $options=get_option('August_options');
    if($options){
        echo '<script>var options='.$options.';</script>';
    }else{
        echo '<script>var options={};</script>';
    }
?>   
<script>
var ThemeForm={
    form:'',
    formid:'august_options_form',
    formvalue:'august_options',
    submitname:'august_options_save',
    title:'August主题设置',
    titleclass:'august_options_title',
    containerclass:'august_options_container',
    labelclass:'august_options_label',
    inputclass:'august_options_input',
    textclass:'august_options_textarea',
    selectclass:'august_options_select',
    checkclass:'august_options_check',
    itemclass:'august_options_item',
    descriptionclass:'august_options_description',
    formlist:[],
       setUp:function(){
        ThemeForm.form=document.createElement('form');
        ThemeForm.form.id=ThemeForm.formid;
        ThemeForm.form.method='post';  
        ThemeForm.form.action='options.php';
        if(document.getElementById('wpbody-content')){
            parent=document.getElementById('wpbody-content');
            parent.appendChild(ThemeForm.form);
        }
        logo=document.createElement('div');
        logo.id='icon-themes';
        logo.classList='icon32';
        ThemeForm.form.appendChild(logo);
        title=document.createElement('h2');
        title.classList=ThemeForm.titleclass;
        title.textContent=ThemeForm.title;
        ThemeForm.form.appendChild(title);
    },
    createFooter:function(){
        var wpnonce=document.getElementsByName('_wpnonce')[0];
        var wpreferer=document.getElementsByName('_wp_http_referer')[0];
        ThemeForm.form.appendChild(wpnonce);
        ThemeForm.form.appendChild(wpreferer);
        var input_data=document.createElement('input');
        input_data.type='hidden';
        input_data.id=ThemeForm.formvalue;
        input_data.name=ThemeForm.formvalue;
        input_data.value='update';
        var input_action=document.createElement('input');
        input_action.type='hidden';
        input_action.id='page_value';
        input_action.name='action';
        input_action.value='update';
        var input_options=document.createElement('input');
        input_options.type='hidden';
        input_options.id='page_options';
        input_options.name='page_options';
        input_options.value=ThemeForm.formvalue;
        container=ThemeForm.makeContainer();
        input_submit=document.createElement('input');
        input_submit.type='submit';
        input_submit.name=ThemeForm.submitname;
        input_submit.classList='button-primary';
        input_submit.value='Save';
        input_submit.onclick=ThemeForm.makeJson;
        container.appendChild(input_submit);
        ThemeForm.form.appendChild(input_data);
        ThemeForm.form.appendChild(input_action);
        ThemeForm.form.appendChild(input_options);
        ThemeForm.form.appendChild(container);
    },
    setClass:function(title,container,label,input,description){
        ThemeForm.titleclass=title;
        ThemeForm.containerclass=container;
        ThemeForm.labelclass=label;
        ThemeForm.inputclass=input;
        ThemeForm.descriptionclass=description;
    },
    makeContainer:function(){
        var container=document.createElement('div');
        container.classList=ThemeForm.containerclass;
        return container;
    },
    makeItem:function(parent){
        var item=document.createElement('p');
        item.classList=ThemeForm.itemclass;
        parent.appendChild(item);
        return item;
    },
    makeLabel:function(text,parent){
        var label=document.createElement('label');
        label.textContent=text+':';
        label.classList=ThemeForm.labelclass;
        parent.appendChild(label);
    },
    makeDescription:function(text,parent){
        var desc=document.createElement('p');
        desc.classList=ThemeForm.descriptionclass;
        desc.innerHTML=text;
        parent.appendChild(desc);
    },
    //单行文本
    makeInput:function(name,value,parent){
        var input=document.createElement('input');
        var obj={};
        input.type='input';
        input.name=name;
        input.classList=ThemeForm.inputclass;
        if(options&&options[name]){
            input.value=options[name];
        }else if(value){
            input.value=value;
        }else{
                    input.value='';  
                }
        var obj={};
        obj.element=input;
        obj.getValue=function(){
            options[obj.element.name]=obj.element.value;
        }
        ThemeForm.formlist.push(obj);
        parent.appendChild(input);
    },
    //多行文本
    makeTextarea:function(name,value,parent){
        var input=document.createElement('textarea');
        input.type='textarea';
        input.name=name;
        input.classList=ThemeForm.textclass;
        if(options&&options[name]){
            input.value=options[name];
        }else if(value){
            input.value=value;
        }else{
                    input.value='';  
                }
                var obj={};
        obj.element=input;
        obj.getValue=function(){
            options[obj.element.name]=obj.element.value;
        }
        ThemeForm.formlist.push(obj);        
        parent.appendChild(input);
    },
    //单选框
    makeRadio:function(name,value,option,parent){
        radios=[];
        var choice;
        if(options&&options[name]){
            choice=options[name];
        }else{
            choice=value;
        }
        for(i in option){
            var input=document.createElement('input');
            input.type='radio';
            input.value=i;
            input.name=name;
            if(choice==input.value){
                input.checked=true;
            }
            radios.push(input);
        var label=document.createElement('label');
            label.textContent=option[i];
            label.classList=ThemeForm.checkclass; 
            parent.appendChild(input);
        parent.appendChild(label);
        }
        var obj={};
            obj.element=radios;
            obj.getValue=function(){
            for(i in obj.element){
                if(obj.element[i].checked===true){
                    options[obj.element[i].name]=obj.element[i].value;
                }
            }
        }
    ThemeForm.formlist.push(obj);
    },
    //多选框
    makeCheckbox:function(name,value,option,parent){
        checks=[];
        var choice;
        if(options&&options[name]){
            choice=options[name];
        }else{
            choice=value;
        }
        for(i in option){
            var input=document.createElement('input');
            input.type='checkbox';
            input.value=i;
            input.name=name;
            if(choice.includes(input.value)){
                input.checked=true;
            }
            checks.push(input);
            var label=document.createElement('label');
            label.textContent=option[i];
            label.classList=ThemeForm.checkclass; 
            parent.appendChild(input);
            parent.appendChild(label);
        }
        var obj={};
        obj.element=checks;
        obj.getValue=function(){
                options[name]=[];
        for(i in obj.element){
            if(obj.element[i].checked===true){
            options[name].push(obj.element[i].value);
                }
            }
        }
        ThemeForm.formlist.push(obj);
    },
    //选择框
    makeSelect:function(name,value,option,parent){
        var selectvalue;
        var selects=[];
        var selectcontainer=document.createElement('select');
        selectcontainer.name=name;
        selectcontainer.classList=ThemeForm.selectclass;
        if(options&&options[name]){
            selectvalue=options[name];
        }else{
            selectvalue=value;
        }
        for(i in option){
            var selectoption=document.createElement('option');
            selectoption.value=i;
            selectoption.textContent=option[i]
            if(selectvalue==selectoption.value){
                selectoption.selected=true;
            }
            selects.push(selectoption);
            selectcontainer.appendChild(selectoption);
        }
        parent.appendChild(selectcontainer);
        var obj={};
        obj.element=selects;
        obj.getValue=function(){
        for(i in obj.element){
            if(obj.element[i].selected===true){
                options[selectcontainer.name]=obj.element[i].value;
            }
        }
        }
    ThemeForm.formlist.push(obj);
    },
    createNew:function(label,description,name,type,value,option){
        container=ThemeForm.makeContainer();
        item=ThemeForm.makeItem(container);
        ThemeForm.makeLabel(label,item);
        switch(type)
        {
        case 'input':
            ThemeForm.makeInput(name,value,item);
        break;
        case 'textarea':
            ThemeForm.makeTextarea(name,value,item);
        break;
        case 'radio':
            ThemeForm.makeRadio(name,value,option,item);
        break;
        case 'checkbox':
            ThemeForm.makeCheckbox(name,value,option,item);
        break;
        case 'select':
            ThemeForm.makeSelect(name,value,option,item);
        break;
        default:
        break;
        }
        ThemeForm.makeDescription(description,container);
        ThemeForm.form.appendChild(container);
    },
    makeJson:function(){
        for(i in ThemeForm.formlist){
            ThemeForm.formlist[i].getValue();
        };
        options=JSON.stringify(options);
        document.getElementById('august_options').value=options;
    }
}
ThemeForm.setUp();
ThemeForm.createNew('测试1','11111<br>2222<br>','site-logo','input');
ThemeForm.createNew('测试2','这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试这就是一个多行文本测试','test2','textarea');
ThemeForm.createNew('测试3','这就是一个单选测试','check1','select','',{'red':'红色','green':'绿色的 ','blue':'蓝色','yellow':'黄色'});
ThemeForm.createNew('测试4','这就是一个多选测试','check2','checkbox','',{'red':'红色','green':'绿色的 ','blue':'蓝色','yellow':'黄色'});
ThemeForm.createFooter();
</script>
<?php }?>

生成表单

ThemeForm.createNew函数用于生成表单,它支持六个参数,文本框的默认值与默认候选项可以不写,所以新建一个文本框可以写成这样:

ThemeForm.createNew('站点logo','提交一个站点logo地址','site-logo-img','input');

单选与多选框最好写一下默认值,注意候选项采用了json格式:

ThemeForm.createNew('主题颜色','选择任意颜色作为主题主体颜色','theme-color','radio','',{'red':'红色','green':'绿色的 ','blue':'蓝色','yellow':'黄色'});

表单效果

代码会在主题外观的子菜单里生成一个新的"选项"页面,可以在该页面对各项参数进行编辑,数据整理会被整理为json对象;
js_create_theme_option_page_p1

提交保存后,options页面会生成一个august_options的新项目,如果想修改该参数名,请修改代码中formvalue的值。

js_create_theme_option_page_p2

使用以及数据调用

将完整代码置于主题functions.php文件的末尾,保存即可,如果觉得代码太长,也可以在主题根目录单独存一个文件例如myfunctions.php,然后打开functions.php文件在最底部添加下面的代码载入我们新建的这个文件:

    include_once('myfunctions.php'); 

因为后台数据是json格式的,在php中调用前需要先用json_decode()命令解码,示例如下:

<?php $august_options=json_decode(get_option('August_options'),true);?>
<?php echo $august_options["site-logo"];?>

标签: javascript

移动端可扫我直达哦~

推荐阅读

javascript 2023-10-13

用jquery取代a链接的title说明文字

站点的标签页原来是类似下图左侧的,在标签后用括号的形式展示了文章数量,边栏本来就小,加了数字后感觉内容变长,也有点影响标签文字的识别,就想着改成下图右侧的形式。记录一下原来生成含数字标签的代码,方便以后修改时的查询:<?php ...

建站相关 javascript

javascript 2023-10-11

js正则表达式的匹配与替换操作

利用exec()可以提取到某一段字符串中的指定的值,比如有下面这样一行字符串:a='-aaaa-bbbb-cccc-';想要分别提取其中的连续的字符,即“aaaa”、“bbbb”、“cccc”,书写正则表达式如下,并利用exec()函...

建站相关 javascript

javascript 2023-10-09

双栏多栏主题图片的lazyload问题

想为全站添加lazyload效果,这样在图片加载成功后会得到一个渐变显示的效果,看起来酷酷的。使用jquery的lazyload老牌插件,先写的js效果,后添加的模块,左边栏的图片加载的好好的,等到右边栏输出缩略图时,就出现了问题。l...

建站相关 javascript

javascript 2023-09-27

php如何传递数据给前端的javascript

尝试写主题的时候遇到了需要利用后台的设置数据去改变前端javascript设置的需求,比如后台设置一个幻灯的轮播速度为“5000”毫秒,需要将这个数据传递给js插件“slidejs”。最初的做法是利用php直接输出一段标签,类似下面这...

建站相关 javascript

javascript 2023-05-28

利用js获取当前页面的域名与网络协议等信息

浏览器会在用户有历史搜索记录之后自动弹出一些关键词供用户选择。但弹出的窗口的位置往往距离输入框过近,弹窗样式也不是特别好看。所以准备参考360,记录用户的搜索关键词,以标签的形式放在弹出窗口中,当输入框获取到焦点时,弹出自定义的窗口,...

建站相关 javascript

javascript 2023-05-09

利用localStorage保存用户的浏览记录

尝试在页面上加载了一言,没事刷新页面就会看到一条新的记录。偶然刷到这样一句话:“大佬永远都觉得自己是萌新”,秉承这种态度的人,一方面可能是出自谦虚。另一方面,技术迭代日新月异,知识浩如烟海,能在某一方面保持拔尖的人,确实为数不多。说回...

建站相关 javascript

javascript 2023-04-27

利用console.time来测试一下js程序的执行效率

想测试一下自己的程序跑一圈需要多少时间,可以尝试启动一个计时器来跟踪它的占用时长。每一个计时器必须拥有唯一的名字。当以此计时器名字为参数调用 console.timeEnd() 时,浏览器将以毫秒为单位,输出对应计时器所经过的时间。启...

建站相关 javascript

javascript 2023-04-10

利用lazysizes.js实现图片懒加载

站点的专题图片稍有点大,恩,其实主要是服务器的带宽过小。导致访问的时候图片加载过慢,页眉部分会空出一块,直到加载结束。于是就想参照joe主题的样式,做一个图片懒加载的效果。joe主题使用了lazysizes实现图片的懒加载。于是查询了...

建站相关 javascript

javascript 2022-11-16

利用js代码获取淘宝详情页大图

利用代码成功获取到了京东的宝贝详情页的大图,想顺便尝试一下淘宝的大图的获取。与京东不同,京东修改了图片的路径地址以实现不同尺寸的展现,淘宝则是以修改图片后缀名的方式,如果想要展现宝贝的大图,直接去掉图片的后缀名就可以了。获取图片的js...

建站相关 javascript