0

I am working with my options table in my database using the Settings API in WP. My code shows data from my form input to be stored in "Kaipo_plugin_options". I want to retrieve that specific options row BUT also retrieve the admins username,email and user id with it. I want that information to be stored in a string, all in one variabe. I have tried many ways but none of them works. Please pay attention to my code from my if submit button is set part. i.e if(isset($_POST['submit'])) ...onwards thanks

class Kaipo_options {

    public $options;

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

    public function add_menu_page(){
        add_options_page('Kaipo', 'Kaipo','administrator', __FILE__, array('Kaipo_options', 'display_options_page'));
    }


    public function display_options_page(){
      ?>
        <div class="wrap">
          <?php get_screen_icon(); ?>
            <h2>Kaipo Options</h2>
            <form method="post" action="options.php" enctype="multipart/form-data">
                <?php settings_fields('Kaipo_plugin_options'); ?>
                <?php do_settings_sections(__FILE__); ?>
                <p class="submit">
                  <input name="submit" type="submit" class="button-primary" value="Save Changes"/>
                </p>
            </form>
        </div>
    <?php
    }

    public function register_settings_and_fields(){
        register_setting('Kaipo_plugin_options', 'Kaipo_plugin_options', array($this, 'Kaipo_validate_settings')); //3rd param optional callback
        add_settings_section('Kaipo_main_section', 'Main Settings', array($this,'Kaipo_main_section_cb'), __FILE__);//id, title, cb, page
        add_settings_field('Kaipo_banner_heading','Banner Heading: ', array($this,'Kaipo_banner_heading_setting'),__FILE__, 'Kaipo_main_section');
        add_settings_field('Kaipo_logo','Your Logo: ', array($this,'Kaipo_logo_settings'),__FILE__, 'Kaipo_main_section');
    }

    public function Kaipo_main_section_cb(){
        //optional parameter
    }
    //is the file an image? need validation jono!
    public function Kaipo_validate_settings($plugin_options){
        if(!empty($_FILES['Kaipo_logo_upload']['tmp_name'])){
            $override = array('test_form' => false);
            $file = wp_handle_upload($_FILES['Kaipo_logo_upload'],$override);
            $plugin_options['Kaipo_logo'] = $file['url'];
        }else{
           $plugin_options['Kaipo_logo'] = $this->options['Kaipo_logo'];
        }
        return $plugin_options;
    }

    /* ALL BELOW HERE IS THE INPUTS */

    //Banner Heading
    public function Kaipo_banner_heading_setting(){
        if ( isset( $this->options['Kaipo_banner_heading'] ) )
            $value = esc_attr( $this->options['Kaipo_banner_heading'] );
        else
            $value = '';

        echo "<input name='Kaipo_plugin_options[Kaipo_banner_heading]' type='text' value='$value'/>";
    }

    public function Kaipo_logo_settings(){
        echo '<input type="file" name="Kaipo_logo_upload" /><br/><br/>';
        if(isset($this->options['Kaipo_logo'])){
           echo "<img src='{$this->options['Kaipo_logo']}' alt=''/>";
        }

    }
}

add_action('admin_menu', function(){
    Kaipo_options::add_menu_page();
});


add_action('admin_init', function(){
   new Kaipo_options();
});


add_action('admin_init', function(){
    if(isset($_POST['submit'])) {

        global $current_user;

        $ajaxstring = array();

        get_currentuserinfo();
        $ajaxstring[0] = $current_user->user_login;
        $ajaxstring[1] = $current_user->user_email;
        $ajaxstring[2] = $current_user->user_firstname;
        $ajaxstring[3] = $current_user->user_lastname;
        $ajaxstring[4] = $current_user->display_name;
        $ajaxstring[5] = $current_user->ID;
        $ajaxstring[6] = get_option('Kaipo_plugin_options');

        foreach($ajaxstring as $x) {
            echo "Key=" . $x ;
            echo "<br>";
        }
    }


});
`
  • You won't see your debugging echo as your options form will post to options.php, which will save and then redirect/exit. When you say "stored in a string", do you mean in the database/options table, or in a global variable at runtime? – TheDeadMedic Apr 14 '15 at 13:35
  • I would like options row get_option["Kaipo_plugin_options"] to be stored in a global variable everytime the user clicks the submit button. Also is it possible to simply js alert the submit button to the screen that variable string just for my learning purposes? – 75Kane Apr 14 '15 at 13:51
  • If I run $string = get_option('Kaipo_plugin_options'); and then echo out the output I get an error headers already sent. I just want to retrieve that information from my database row options as a string. Not sure how that can be done? – 75Kane Apr 14 '15 at 14:13
  • Sorry, I'm not sure what that's got to do with your original question - where does storing/reading user data come into it? If you just need your options as a global, use $GLOBALS['Kaipo_plugin_options'] = get_option( 'Kaipo_plugin_options' );. Now in any function, use global $Kaipo_plugin_options; and the variable will hold your options. – TheDeadMedic Apr 14 '15 at 14:28
  • Thanks but my issue was that I wanted to store the admins information as well in get_option('Kaipo_plugin_options'); but my code did not run. Also if I simply echo my variable I get an empty array. not sure why – 75Kane Apr 14 '15 at 14:52

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.