0

I am building my first oop based plugin.This main problem is that it is not showing settings field(font error input).So far my approach..

$plugin_settings = new WordPress_Plugin_Template_Settings( __FILE__ );

The class is

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>';
    }
}

I think I have error in add_settings_field('error_font_color', 'Error Font Color', array( $this,'ma_error_font_color'), __FILE__, 'main_section');

1 Answer 1

2

Change ma_admin_options_init() to:

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');
}

The fourth parameter of add_settings_section() and add_settings_field() is $page and that needs to match the page ID set in add_options_page().

3
  • Thank you very much.Could you just please do another favour,it is not saving input text value but the option (ma_admin_styler) is created in database. Commented Mar 24, 2014 at 18:37
  • You don't appear to have a validation callback even though you specified one when you registered your setting. Commented Mar 24, 2014 at 18:53
  • Don't forget to mark this as the answer if this solved your question. Commented Mar 24, 2014 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.