我的设置未保存在wordpress主题选项页中

如何解决我的设置未保存在wordpress主题选项页中

| 我正在尝试基于WordPress的Settings API创建主题选项页面。当我在浏览器中检查options.php页面时(例如http://mysite.com/wordpress/wp-admin/options.php),我看到coolorange_options的条目,但它为空。我在文本字段中添加了网址,宽度和高度后,发生了这种情况。 到目前为止,我已经编写了足够的代码来获取信息并将其存储到数据库中,但没有为任何目的检索它。我在代码顶部的php注释中引用了一个教程。考虑到我不太了解php或编程,这里一定有问题,我只是不知道什么。整个代码如下:
<?php 
// This options page mainly follows the WordPress Settings API Tutorial at
// http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/
add_action(\'admin_menu\',\'theme_admin_add_page\');
add_action(\'admin_init\',\'theme_admin_init\');

function theme_admin_init() {
register_setting(\'coolorange_theme_options\',\'coolorange_options\',\'coolorange_options_validate\');
// what each parameter represents: add_settings_field($id,$title,$callback,$page,$section,$args);
add_settings_section(\'coolorange_logo_main\',\'Logo Section Settings\',\'logo_section_text\',\'coolorange\');
add_settings_field(\'upload_image_button\',\'<strong>Upload logo to the Media Folder</strong>\',\'file_upload_button\',\'coolorange\',\'coolorange_logo_main\'); // Upload Logo button
add_settings_field(\'logo_textfields\',\'<strong>Logo location</strong>\',\'file_location\',\'coolorange_logo_main\'); // logo url,width and height text fields
add_settings_field(\'restore_selectbox\',\'<strong>Restore original heading</strong>\',\'restore_dropdown\',\'coolorange_logo_main\');
}

function logo_section_text() {
echo \'<p>In this section,you can replace the standard blog title heading with a custom logo. The logo cannot be wider than <strong>960 pixels</strong>.</p>\';
}

function file_upload_button() {
$options = get_option(\'coolorange_theme_options\');
echo \'<input id=\"upload_image_button\" class=\"button-secondary\" type=\"button\" name=\"coolorange_theme_options[upload_image_button]\" value=\"Upload Logo\" />\';
}

function file_location() { //opens file_location function
$options = get_option(\'coolorange_theme_options\'); ?>
<h3>How to upload a logo to replace the heading</h3>
    <div style=\"background-color: #FFFFFF; border: 1px solid #BBBBBB; padding: 30px;\">
    <ul style=\"list-style: disc;\">
        ...instructions to upload logo
    </ul>
</div>
<p><strong>File URL:</strong> <input id=\"image_url\" type=\"text\" value=\"<?php $options[\'image_url\']; ?>\" size=\"60\" name=\"coolorange_theme_options[image_url]\" /><br />
<strong>Logo width:</strong> <input id=\"image_width\" type=\"text\" value=\"<?php $options[\'image_width\']; ?>\" size=\"6\" name=\"coolorange_theme_options[image_width]\" /> Enter logo width in pixels (for example <strong>800px</strong>)<br />
<strong>Logo height:</strong> <input id=\"image_height\" type=\"text\" value=\"<?php $options[\'image_height\']; ?>\" size=\"6\" name=\"image_height\" /> Enter logo height in pixels (for example <strong>90px</strong>)</p>
<?php  
} //closes file_location function

function restore_dropdown() { //opens restore_dropdown function
$options = get_option(\'coolorange_theme_options\'); 
// http://codex.wordpress.org/Function_Reference/selected ?>
<select name=\"co_restore_selectbox[select_options]\" id=\"co_restore_selectbox\">
    <option value=\"default\" <?php if ( $co_restore_selectbox[\'select_options\'] == \'default\' )
        echo \'selected=\"selected\"\'; ?>>select an option</option>
    <option value=\"restore\" <?php if ( $co_restore_selectbox[\'select_options\'] == \'restore\' )
        echo \'selected=\"selected\"\'; ?>>Restore CSS</option>
</select>
<?php 
} // closes restore_dropdown funcion

function css_switcher() {
$options = get_option(\'coolorange_theme_options\');
//$backgroundurl = \"url(\'\" . $options[\'image_url\']; . \"\') top center no-repeat\";
?>
<style type=\"text/css\">
<!--
#logo {
background: <?php echo $backgroundurl; ?>;
width: <?php echo $image_width; ?>;
height: <?php echo $image_height; ?>;
padding: 1em 2em 0 2em;
margin: 0 auto;
}

#blog-title a {
display: block;
width: <?php echo $image_width; ?>;
height: <?php echo $image_height; ?>;
text-indent: -2000px;
}

#blog-description {
text-indent: -2000px;
}
-->
</style>
<?php } // closes css_switcher function

add_action(\'wp_head\',\'css_switcher\');

function theme_admin_add_page() {
add_theme_page(\'Cool Orange Theme Options\',\'Cool Orange Theme Options\',\'manage_options\',\'theme_options_page\');
}

//Scripts to load WP\'s Media Library panel
//http://www.webmaster-source.com/2010/01/08/using-the-wordpress-uploader-in-your-plugin-or-theme/
function my_admin_scripts() {
...scripts to load media library - unrelated
}

function my_admin_styles() {
...styles for media library loader
}

if (isset($_GET[\'page\']) && $_GET[\'page\'] == \'coolorange\') {
...actions to add for media library loader script
}

function theme_options_page() {
?>
<div class=\"wrap\">
<div id=\"icon-themes\" class=\"icon32\"><br /></div><h2>Cool Orange Theme Options</h2>
<form action=\"options.php\" method=\"post\" name=\"options_form\"><?php settings_fields(\'coolorange_theme_options\'); ?>
<?php do_settings_sections(\'coolorange\'); ?>

<input name=\"Submit\" class=\"button-primary\" type=\"submit\" value=\"<?php esc_attr_e(\'Save Changes\'); ?>\" />
</form>
</div>
<?php 
}
?>
    

解决方法

        我今天早些时候找到了这个问题的答案。根据教程,我需要确保将每个选项(输入字段,选择框选项等)的名称指定为name = \“ coolorange_options [some_text] \”。我在ID后面命名了some_text部分,例如name = \“ coolorange_options [image_url]。 在本教程中,我在http://ottopress.com/2009/wordpress-settings-api-tutorial/上遵循了较新的版本,每个名称都被php识别为数组。直到我将名称co_restore_selectbox [select_options]更改为coolorange_options [select_options]时,名称才出现在数组中。当我再次在浏览器中检查options.php时,coolorange_options下的条目显示单词SERIALIZED DATA,我认为这意味着该数组已写入options数据库。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-