我必须在wordpress主题中使用数组创建一个选项页面。提交所有填写信息后,我正在使用
var_dump($ this-> options)显示空白数组(){}
我想在输入框中提交选项页面表单提交信息时,我会get_option
在某个变量中输入值,这样我就必须在我的主题中显示信息。
<?php
class MySettingsPage
{
private $options;
public function __construct()
{
add_action( 'admin_menu', array( $this, 'bguru_register_options_page' ) );
add_action( 'admin_init', array( $this, 'bguru_register_settings' ) );
}
public function bguru_register_options_page()
{
// This page will be under "Settings"
add_theme_page('Business Guru Options',
'Theme Customizer',
'edit_theme_options',
'bguru-options',
array( $this, 'bguru_options_page')
);
}
public function bguru_options_page()
{
// Set class property
$bguru_logo = is_array( get_option( 'bguru_logo' ) ) ? get_option( 'bguru_logo' ) : array();
$bguru_vimeo = is_array( get_option( 'bguru_vimeo' ) ) ? get_option( 'bguru_vimeo' ) : array();
$this->options = array_merge( $bguru_logo, $bguru_vimeo);
var_dump($this->options);
?>
<div class="wrap">
<?php screen_icon(); ?>
<h1>Business Guru Options</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'defaultbg' );
do_settings_sections( 'defaultbg' );
submit_button();
?>
</form>
</div>
<?php
}
public function bguru_register_settings()
{
register_setting('defaultbg','bguru_logo', array( $this, 'sanitize' ) );
register_setting('defaultbg', 'bguru_vimeo', array( $this, 'sanitize' ));
add_settings_section(
'setting_section_id', // ID
'General',
array( $this, 'print_section_info' ), // Callback
'defaultbg' // Page
);
add_settings_field(
'bguru_logo', // ID
'Logo', // Title
array($this,'logo_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
add_settings_field(
'bguru_vimeo', // ID
'Vimeo', // Vimeo
array( $this, 'socialv_callback' ), // Callback
'defaultbg', // Page
'setting_section_id' // Section
);
}
public function sanitize( $input )
{
$new_input = array();
if( isset( $input['bguru_logo'] ) )
$new_input['bguru_logo'] = sanitize_text_field( $input['bguru_logo'] );
if( isset( $input['bguru_vimeo'] ) )
$new_input['bguru_vimeo'] = sanitize_text_field( $input['bguru_vimeo'] );
return $new_input;
}
public function print_section_info()
{
print 'Enter your settings below:';
}
public function logo_callback()
{
printf('<input type="text" id="bguru_logo" size="50" name="bguru_logo" value="%s" />',
isset( $this->options['bguru_logo'] ) ? esc_attr( $this->options['bguru_logo']) : ''
);
}
public function socialv_callback()
{
printf('<input type="text" id="bguru_vimeo" size="50" name="bguru_vimeo" value="%s" />',
isset( $this->options['bguru_vimeo'] ) ? esc_attr( $this->options['bguru_vimeo']) : ''
);
}
}
if( is_admin() )
$my_settings_page = new MySettingsPage();