Php 使用WordPress选项API显示设置选项
我在使用WordPress设置API保存字段时遇到问题 我有一个选项页,有一个部分和一个字段。所以这很简单。所有显示/渲染都很好,但我无法保存字段Php 使用WordPress选项API显示设置选项,php,database,wordpress,setting,Php,Database,Wordpress,Setting,我在使用WordPress设置API保存字段时遇到问题 我有一个选项页,有一个部分和一个字段。所以这很简单。所有显示/渲染都很好,但我无法保存字段 class LocalSEOAdmin { var $slug = 'local_seo_settings'; var $name = "Local SEO"; public function __construct(){ // When WP calls admin_menu, call $this-
class LocalSEOAdmin {
var $slug = 'local_seo_settings';
var $name = "Local SEO";
public function __construct(){
// When WP calls admin_menu, call $this->admin_settings
if ( is_admin() ) {
add_action('admin_menu', array( &$this, 'add_options_page' ) ); // on admin_menu call $this->init_admin_menu
add_action( 'admin_init', array( &$this, 'add_setting_fields' ) );
}
}
public function add_options_page(){
add_options_page(
$this->name, // Page title
$this->name, // Menu Title
'manage_options', // Role
$this->slug, // Menu slug
array( &$this, 'local_seo_admin_menu_callback' ) // Callback to render the option page
);
}
public function local_seo_admin_menu_callback(){
?>
<div class='wrap'>
<h1><?php echo $this->name ?> Options</h1>
<form method='post' action='options.php'>
<?php
settings_fields( 'address_settings_section' );
do_settings_sections( $this->slug );
submit_button();
?>
</form>
</div>
<?php
}
public function add_setting_fields(){
add_settings_section(
'address_settings_section', // ID used to identify this section and with which to register options
'Address Options', // Title to be displayed on the administration page
array( &$this, '_render_address_options'), // Callback used to render the description of the section
$this->slug // Page on which to add this section of options
);
add_settings_field(
'address_line_1', // ID used to identify the field throughout the theme
'Address Line 1', // The label to the left of the option interface element
array( &$this, '_render_address_line_1'), // The name of the function responsible for rendering the option interface
$this->slug, // The page on which this option will be displayed
'address_settings_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__( 'Activate this setting to display the header.', 'sandbox' ),
)
);
register_setting(
'address_settings_section',
'address_settings_section'
);
}
public function _render_address_options(){
echo '<p>Add you address</p>';
}
public function _render_address_line_1(){
$option = get_option( 'address_line_1' );
$html = '<input type="text" id="address_line_1" name="address_line_1" value="' . $option . '" />';
echo $html;
}
}
new LocalSEOAdmin;
类LocalSEOAdmin{
var$slug='本地搜索引擎优化设置';
var$name=“本地搜索引擎优化”;
公共函数构造(){
//当WP调用admin_菜单时,调用$this->admin_设置
if(is_admin()){
add_action('admin_menu',array(&$this,'add_options_page');//在admin_菜单上调用$this->init_admin_菜单
add_action('admin_init',array(&$this,'add_setting_fields'));
}
}
公共功能添加选项页面(){
添加选项页面(
$this->name,//页面标题
$this->name,//菜单标题
“管理_选项”,//角色
$this->slug,//菜单slug
数组(&$this,'local\u seo\u admin\u menu\u callback')//用于呈现选项页的回调
);
}
公共函数本地\搜索引擎\管理\菜单\回调(){
?>
选择权
当你注册你的设置时有一个小问题
第二个参数base-on是$option\u name,所以您必须输入发送名称
register_setting(
'address_settings_section',
'address_line_1'
);
现在它成功了
您可以在中找到有关创建选项页的详细信息。中
第一个参数是您的选项组名称
&
第二个参数是选项名
,在中给出,在您的情况下是应该是地址行1
而不是地址设置部分
最后
register_setting(
'address_settings_section',
'address_line_1'
)
你一定是在开玩笑。结果证明你是对的,天知道我怎么没发现。谢谢!