使用 WordPress 选项显示设置选项 API
Displaying settings options up using WordPress options API
我在使用 WordPress 设置保存我的字段时遇到问题 API
我有 1 个选项页面,包含 1 个部分和 1 个字段。所以这很简单。一切正常 appearing/rendering,但我无法保存字段。
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;
我不确定发生了什么,但我觉得有人把必填字段搞混了。这些字段显示在 WordPress 中,当我点击更新时出现 "setting saved." 消息,但没有任何内容被保存。
注册设置时出现一点问题。
基于register_setting,第二个参数是$option_name所以你必须输入你的发送名称
register_setting(
'address_settings_section',
'address_line_1'
);
现在成功了。
您可以在 wordpress site 中找到有关创建选项页面的更多信息。
第一个参数是你的 option group name
&
第二个参数是 option name
,它在 add_settings_field() 中给出,在你的情况下应该是 address_line_1
而不是 address_settings_section
终于
register_setting(
'address_settings_section',
'address_line_1'
)
我在使用 WordPress 设置保存我的字段时遇到问题 API
我有 1 个选项页面,包含 1 个部分和 1 个字段。所以这很简单。一切正常 appearing/rendering,但我无法保存字段。
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;
我不确定发生了什么,但我觉得有人把必填字段搞混了。这些字段显示在 WordPress 中,当我点击更新时出现 "setting saved." 消息,但没有任何内容被保存。
注册设置时出现一点问题。
基于register_setting,第二个参数是$option_name所以你必须输入你的发送名称
register_setting(
'address_settings_section',
'address_line_1'
);
现在成功了。
您可以在 wordpress site 中找到有关创建选项页面的更多信息。
第一个参数是你的 option group name
&
第二个参数是 option name
,它在 add_settings_field() 中给出,在你的情况下应该是 address_line_1
而不是 address_settings_section
终于
register_setting(
'address_settings_section',
'address_line_1'
)