Php 未保存Wordpress自定义选项
我在为wordpress添加自定义设置页面时遇到问题,更具体地说,在获取要保存的选项时遇到问题 由于不同的示例和不同的函数对同一事物的解释不同,因此相关文档似乎缺乏且不一致 wordpress codex上的最新示例使用sanitize回调函数保存选项。但是add_settings_field()文档说保存选项应该在幕后进行。似乎更愿意使用san回调来保存 我尝试了各种不同的方法,我的代码曾经设法保存其中一个字段,但不是全部 我当前的代码:Php 未保存Wordpress自定义选项,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我在为wordpress添加自定义设置页面时遇到问题,更具体地说,在获取要保存的选项时遇到问题 由于不同的示例和不同的函数对同一事物的解释不同,因此相关文档似乎缺乏且不一致 wordpress codex上的最新示例使用sanitize回调函数保存选项。但是add_settings_field()文档说保存选项应该在幕后进行。似乎更愿意使用san回调来保存 我尝试了各种不同的方法,我的代码曾经设法保存其中一个字段,但不是全部 我当前的代码: class wwtkSettings { pu
class wwtkSettings {
public function __construct() {
if ( is_admin() ){
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
}
public function add_plugin_page(){
add_options_page( 'WWTK Settings', 'WWTK settings', 'manage_options', 'wwtk-settings', array( $this, 'create_admin_page' ) );
}
public function create_admin_page() {
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>WWTK Settings</h2>
<form method="post" action="options.php">
<?php
settings_fields( 'wwtk-setting-group' );
do_settings_sections( 'wwtk-settings' );
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function page_init() {
register_setting( 'wwtk-setting-group', 'wwtk-setting-group', array( $this, 'wwtk_validate_options' ) );
add_settings_section(
'wwtk-settings-section',
'Categories and pages',
array( $this, 'print_section_info' ),
'wwtk-settings'
);
add_settings_field(
'frontpage',
'Frontpage category:',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'frontpage'
);
add_settings_field(
'category1',
'Category & page 1',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'category1'
);
add_settings_field(
'category2',
'Category & page 2',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'category2'
);
add_settings_field(
'category3',
'Category & page 3',
array( $this, 'create_category_field' ),
'wwtk-settings',
'wwtk-settings-section',
'category3'
);
}
//TODO: check if category and page exist. and sanitize!
public function wwtk_validate_options( $input ) {
return $input;
}
public function print_section_info(){
print 'Enter the names of categories that should be displayed on the page with the same name(both must exist):';
}
public function create_category_field($args){
?><input type="text" id="<?php echo $args ?>" name="wwtk-setting-group" value="<?php echo get_option( 'wwtk-setting-group['.$args.']' ); ?>" /><?php
}
}
class wwtk设置{
公共函数构造(){
if(is_admin()){
添加操作('admin_menu',array('add_plugin_page');
添加操作('admin_init',数组($this,'page_init'));
}
}
公共函数添加插件页面(){
添加选项页面('WWTK Settings','WWTK Settings','manage_options','WWTK Settings',数组('create_admin_page'));
}
公共函数创建\管理\页面(){
?>
WWTK设置
/*代码与您的示例类似*/
类MySettingsPage{
/**
*保存要在字段回调中使用的值
*/
私人美元期权;
/**
*启动
*/
公共函数构造()
{
添加操作('admin_menu',array('add_plugin_page');
添加操作('admin_init',数组($this,'page_init'));
}
/**
*添加选项页面
*/
公共函数添加插件页面()
{
//此页面将位于“设置”下
添加选项页面(
“设置管理员”,
“我的设置”,
“管理选项”,
“我的设置管理员”,
数组('创建管理页面')
);
}
/**
*选项页回调
*/
公共函数创建\管理\页面()
{
//设置类属性
$this->options=get_option('my_option_name');
?>
我的设置
这是你插件的全部代码吗?我正在为你设计一个解决方案,但我想确保我有所有必要的代码来帮助你;)这是一个自定义主题,除了添加导航菜单和添加对缩略图的支持,这是整个functions.php。非常感谢你的帮助:)重点是利用保存的选项f或者在每个页面上显示特定类型的帖子。我想尝试不同的方法,但在尝试保存多个自定义选项时遇到了困难。您如何以及何时调用该类?也在functions.php文件中?通常您会在类之外创建和设置页面,然后将所有选项都放在类。是。该类正在functions.php$wwtkSettings=new wwtkSettings()内实例化;代码的结构方式为pr。此示例:
/* code similar to your example */
class MySettingsPage{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My Settings',
'manage_options',
'my-setting-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_option_name' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_option_group' );
do_settings_sections( 'my-setting-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_option_group', // Option group
'my_option_name', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'My Custom Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-setting-admin' // Page
);
add_settings_field(
'id_number', // ID
'ID Number', // Title
array( $this, 'id_number_callback' ), // Callback
'my-setting-admin', // Page
'setting_section_id' // Section
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-setting-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
if( !is_numeric( $input['id_number'] ) )
$input['id_number'] = '';
if( !empty( $input['title'] ) )
$input['title'] = sanitize_text_field( $input['title'] );
return $input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function id_number_callback()
{
printf(
'<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
esc_attr( $this->options['id_number'])
);
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_option_name[title]" value="%s" />',
esc_attr( $this->options['title'])
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();