WordPress插件开发OOP风格

时间:2014-03-24 作者:Sadia Mehjabin

我正在构建我的第一个基于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\');

1 个回复
最合适的回答,由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().

结束

相关推荐

Query date in wordpress loop

我目前有一个名为“事件”的自定义帖子类型。我根据这里的教程创建了这个http://tatiyants.com/how-to-use-wordpress-custom-post-types-to-add-events-to-your-site/. 我想查询日期,只显示日期即将到来的帖子,而不是过去的帖子。$event_date >= time然而,在教程中,他使用一个短代码显示结果。我正在尝试将其转换为标准wp循环,以便在索引中显示它。php。他在其短代码函数中使用以下内容:add_shortcode