1

Hi I am writing this plugin as an exercise and everything is working except when I try to validate image upload through register_setting callback function it doesn't save data to options.php at all. Here is the code. I will highlight the problematic area.

This is the line where I am having problem.

register_setting('JW_options_page',  'JW_options_page',array($this,  'jw_validate_setting'));

If I remove array($this, 'jw_validate_setting')); everything works except that I can't upload image.

<?php
/*
    Plugin Name: Theme Admin Panel
    Plugin URI: http://www.orangecreative.net
    Description: Add extra functionality to restricted users.
    Author: S. Abdullah
    Version: 1.0
    Author URI: http://www.orangecreative.net
 */

class JW_options{
    public $options;
    public function __construct(){
        $this->options = get_option('JW_options_page');
        $this->register_settings_and_fields();
    }

    public function admin_menu_page(){
        add_options_page('Theme Options', 'Theme Options', 'administrator', __FILE__, array('JW_options', 'display_options_page'));
    }

    public function display_options_page(){
        ?>
        <div class="wrap">
            <h2>My Theme options</h2>
            <?php var_dump(get_option('JW_options_page')); ?>
            <form action="options.php" method="post" enctype="multipart/form-data">
                <?php settings_fields('JW_options_page'); ?>
                <?php do_settings_sections(__FILE__); ?>
                <p class="submit">
                    <input type="submit" value="Save Changes" class="button-primary"/>
                </p>

            </form>
        </div>
    <?php
    }

    public  function register_settings_and_fields(){
        register_setting('JW_options_page', 'JW_options_page',array($this, 'jw_validate_setting'));
        add_settings_section('jw_main_section', 'Main Settings', array($this, 'jw_main_section_cb'), __FILE__);
        add_settings_field('jw_banner_heading', 'Banner Heading :', array($this, 'jw_banner_heading_settings'), __FILE__, 'jw_main_section');
        add_settings_field('jw_logo', 'Your Logo :', array($this, 'jw_logo_setting'), __FILE__, 'jw_main_section');
        add_settings_field('jw_color_scheme', 'Your Desired Color :', array($this, 'jw_color_scheme_setting'), __FILE__, 'jw_main_section');

    }



    public  function jw_validate_setting($plugin_options){
            echo('ccsjdncjncdjcndjcndjcncjdncjdcndj');
//        if(!empty($_FILES['jw_logo_upload']['temp_name'])){
//            $override = array('test_form' => false);
//            $file = wp_handle_upload($_FILES['jw_logo_upload'], $override);
//            print_r($file);
//
//        }
    }
    public function jw_main_section_cb(){
        //optional
    }

    public function jw_banner_heading_settings(){
        echo "<input name='JW_options_page[jw_banner_heading]' type='text' value='{$this->options['jw_banner_heading']}'/>" ;

    }
    public function jw_logo_setting(){
        echo "<input type='file' name='JW_options_page[jw_logo_upload]'/>" ;
    }
    public function jw_color_scheme_setting(){
        $items = array('Red','Blue','Black');
        echo '<select name="JW_options_page[jw_options_page]">';
        foreach($items as $item){
            echo "<option value='$item'>$item</option>";
        }
        echo '</select>';
    }

}
if(is_admin()){
add_action('admin_menu',function(){
    JW_options::admin_menu_page();

});
add_action('admin_init', function(){
    new JW_options();
});
}
  • Just a tip, please read the tag descriptions before using them. The wordpress.com tag is reserved for questions regarding sites hosted on wordpress.com, wwhich is off topic btw. Thanks – Pieter Goosen Jul 3 '14 at 10:54
  • 1
    @PieterGoosen Thanks for pointing out I will keep in mind next time. – Shah Jul 3 '14 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.