我正在构建我的第一个基于oop的插件。这个主要问题是它没有显示设置字段(字体错误输入)。到目前为止,我的方法。。
$plugin_settings = new WordPress_Plugin_Template_Settings( __FILE__ );
课程是
class WordPress_Plugin_Template_Settings {
private $dir;
private $file;
private $assets_dir;
private $assets_url;
public function __construct( $file ) {
$this->file = $file;
$this->dir = dirname( $this->file );
$this->assets_dir = trailingslashit( $this->dir ) . \'assets\';
$this->assets_url = esc_url( trailingslashit( plugins_url( \'/assets/\', $this->file ) ) );
register_activation_hook( __FILE__,array( $this, \'ma_defaults_options\' ) );
add_action( \'admin_init\', array( $this, \'ma_admin_options_init\' ) );
add_action( \'admin_menu\', array( $this, \'add_menu_item\' ) );
}
function ma_defaults_options() {
$default_options = get_option( \'ma_admin_styler\' );
if( ( !is_array( $default_options ) ) ) {
$default = array(
"error_font_color" => "#ffff",
);
update_option( \'ma_admin_styler\', $default );
}
}
public function add_menu_item() {
add_options_page( \'Plugin Settings\', \'Plugin Settings\', \'edit_theme_options\', \'ma_admin_styler\', array( $this, \'settings_page\' ) );
}
public function ma_admin_options_init() {
register_setting( \'ma_admin_styler\', \'ma_admin_styler\', array( $this,\'ma_admin_styler_validate\' ) );
add_settings_section( \'main_section\', \'\', array( $this,\'section_text_default\' ), __FILE__ );
add_settings_field( \'error_font_color\', \'Error Font Color\', array( $this,\'ma_error_font_color\' ), __FILE__, \'main_section\' );
}
function section_text_default() {
}
function ma_error_font_color() {
$options = get_option( \'ma_admin_styler\' );
echo "<input name=\'ma_admin_styler[error_font_color]\' class=\'wp-color-picker-field\' data-default-color=\'#ffffff\' id=\'color\'value=\'{$options[\'error_font_color\']}\' />";
}
function settings_page() {
echo \'<div class="wrap">
<div class="icon32" id="plugin_settings-icon"><br/></div>
<h2>Plugin Settings</h2>
<form method="post" action="options.php" enctype="multipart/form-data">\';
settings_fields( \'ma_admin_styler\' );
do_settings_sections( \'ma_admin_styler\' );
echo \'<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="\' . esc_attr( __( \'Save Settings\', \'plugin_textdomain\' ) ) . \'" />
</p>
</form>
</div>\';
}
}
我想我在
add_settings_field(\'error_font_color\', \'Error Font Color\', array( $this,\'ma_error_font_color\'), __FILE__, \'main_section\');
最合适的回答,由SO网友:helgatheviking 整理而成
改变ma_admin_options_init()
收件人:
public function ma_admin_options_init(){
register_setting(\'ma_admin_styler\', \'ma_admin_styler\', array( $this,\'ma_admin_styler_validate\'));
add_settings_section(\'main_section\', \'\', array( $this,\'section_text_default\'), \'ma_admin_styler\');
add_settings_field(\'error_font_color\', \'Error Font Color\', array( $this,\'ma_error_font_color\'), \'ma_admin_styler\', \'main_section\');
}
的第四个参数
add_settings_section()
和
add_settings_field()
是
$page
这需要与中设置的页面ID匹配
add_options_page()
.