php - Wordpress - Save values to array upon form submit in admin option page -
i have done successful first time plugin, unfortunately cannot submit updated values array i've created upon register_activation_hook.
i need insights in how wordpress can handle dirty work - or $_post better way it?
below you'll see admin.php handles admin related stuff.
<?php // meaning of abbreviations: // clsc = custom login shortcode // runs when plugin activated register_activation_hook( plugin_main_file, 'clsc_install'); // create new database fields function clsc_install() { $clsc_options = array( 'login_link' => '/log-in/', 'login_string' => __('log in', 'clsc'), 'login_class' => '', // default empty inherit theme styles 'logout_link' => wp_logout_url( home_url()), 'logout_string' => __('log out', 'clsc'), 'logout_class' => '', // default empty inherit theme styles 'account_link' => '/my-account/', 'account_string' => __('my account', 'clsc'), 'account_class' => '' // default empty inherit theme styles ); add_option('clsc_options_array', $clsc_options, '', 'yes'); } // create admin option page function add_clsc_option_page() { add_options_page( 'custom login', // text displayed in title tag 'custom login', // text used menu 'administrator', // capability required display menu 'custom-login-shortcodes', // unique slug name refer menu 'clsc_html_page'); // function output page content } /* call html code */ add_action('admin_menu', 'add_clsc_option_page'); // enqueue admin styles , scripts function clsc_enqueue_scripts() { global $wpdb; $screen = get_current_screen(); if ( $screen->id != 'settings_page_custom-login-shortcodes' ) { return; // exit if incorrect screen id } wp_enqueue_style( 'brokenfruit-shortcodes-styles', plugins_url( 'admin/css/admin_styles.css', dirname(__file__) ) ); wp_enqueue_style( 'bootstrap', plugins_url('admin/css/bootstrap.css', dirname(__file__) ) ); wp_enqueue_script('admin_js_bootstrap_hack', plugins_url('admin/scripts/bootstrap-hack.js', dirname(__file__) ) ); } add_action('admin_enqueue_scripts', 'clsc_enqueue_scripts' ); // build admin interface function clsc_html_page(){ $options = get_option('clsc_options_array'); ?> <form method="post" action="options.php"> <?php wp_nonce_field('update-options'); ?> <div class="bootstrap-wrapper"> <div class="row"> <div class="col-md-12"> <h1><?php _e('custom login shortcode','clsc'); ?></h1> <p><?php _e('to use shortcode:','clsc'); ?><br/><span class="shortcode-preview">[custom_login]</span></p> </div> </div> <div class="row" id="login-content"> <div class="col-md-4"> <h5><?php _e('log in link:','clsc'); ?></h5> <input placeholder="<?php _e('example: /log-in/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['login_link']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log in string:','clsc'); ?></h5> <input placeholder="<?php _e('example: log in', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['login_string']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log in class:','clsc'); ?></h5> <input placeholder="<?php _e('example: login_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['login_class']; ?>" /> </div> </div> <div class="row top-buffer" id="logout-content"> <div class="col-md-4"> <h5><?php _e('log out link:','clsc'); ?></h5> <input placeholder="<?php _e('example: /log-out/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['logout_link']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log out string:','clsc'); ?></h5> <input placeholder="<?php _e('example: log out', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['logout_string']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log out class:','clsc'); ?></h5> <input placeholder="<?php _e('example: logout_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['logout_class']; ?>" /> </div> </div> <div class="row top-buffer" id="account-content"> <div class="col-md-4"> <h5><?php _e('account link:','clsc'); ?></h5> <input placeholder="<?php _e('example: /my-account/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['account_link']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('account string:','clsc'); ?></h5> <input placeholder="<?php _e('example: account', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['account_string']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('account class:','clsc'); ?></h5> <input placeholder="<?php _e('example: account_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['account_class']; ?>" /> </div> </div> <div class="row top-buffer"> <div class="col-md-12"> <input type="hidden" name="action" value="update" /> <input type="hidden" name="page_options" value="clsc_options_array" /> <input class="btn btn-primary top-buffer" type="submit" value="<?php _e('save changes') ?>" /> </div> </div> </div> </form> <?php } ?>
feedback alternative approaches more welcome!
i thank in advance , have great day! :)
i have update function clsc_html_page()
, added action 'admin_init'
save register setting fields.
/** plugin name: clsc plugin uri: http://google.com/ description: description version: 1.0 author: authourname author uri: http://authoururri.com/ license: gplv2 or later text domain: text domain */ // runs when plugin activated register_activation_hook( plugin_main_file, 'clsc_install'); // create new database fields function clsc_install() { $clsc_options = array( 'login_link' => '/log-in/', 'login_string' => __('log in', 'clsc'), 'login_class' => '', // default empty inherit theme styles 'logout_link' => wp_logout_url( home_url()), 'logout_string' => __('log out', 'clsc'), 'logout_class' => '', // default empty inherit theme styles 'account_link' => '/my-account/', 'account_string' => __('my account', 'clsc'), 'account_class' => '' // default empty inherit theme styles ); add_option('clsc_options_array', $clsc_options, '', 'yes'); } add_action('admin_init','admin_init_register_setting'); function admin_init_register_setting() { // register plugins settings /*register_setting('wp_plugin_template-group', 'account_class'); register_setting('wp_plugin_template-group', 'account_string'); register_setting('wp_plugin_template-group', 'account_link'); register_setting('wp_plugin_template-group', 'logout_class'); register_setting('wp_plugin_template-group', 'logout_string'); register_setting('wp_plugin_template-group', 'logout_link'); register_setting('wp_plugin_template-group', 'login_class'); register_setting('wp_plugin_template-group', 'login_string'); register_setting('wp_plugin_template-group', 'login_link'); */ register_setting('wp_plugin_template-group', 'clsc_options_array'); } // create admin option page function add_clsc_option_page() { add_options_page( 'custom login', // text displayed in title tag 'custom login', // text used menu 'administrator', // capability required display menu 'custom-login-shortcodes', // unique slug name refer menu 'clsc_html_page'); // function output page content } /* call html code */ add_action('admin_menu', 'add_clsc_option_page'); // enqueue admin styles , scripts function clsc_enqueue_scripts() { global $wpdb; $screen = get_current_screen(); if ( $screen->id != 'settings_page_custom-login-shortcodes' ) { return; // exit if incorrect screen id } wp_enqueue_style( 'brokenfruit-shortcodes-styles', plugins_url( 'admin/css/admin_styles.css', dirname(__file__) ) ); wp_enqueue_style( 'bootstrap', plugins_url('admin/css/bootstrap.css', dirname(__file__) ) ); wp_enqueue_script('admin_js_bootstrap_hack', plugins_url('admin/scripts/bootstrap-hack.js', dirname(__file__) ) ); } add_action('admin_enqueue_scripts', 'clsc_enqueue_scripts' ); function clsc_html_page() { if(!current_user_can('manage_options')) { wp_die(__('you not have sufficient permissions access page.')); } ?> <div class="wrap"> <form method="post" action="options.php"> <?php $options = get_option('clsc_options_array'); @settings_fields('wp_plugin_template-group'); ?> <?php @do_settings_fields('wp_plugin_template-group'); ?> <div class="bootstrap-wrapper"> <div class="row"> <div class="col-md-12"> <h1><?php _e('custom login shortcode','clsc'); ?></h1> <p><?php _e('to use shortcode:','clsc'); ?><br/><span class="shortcode-preview">[custom_login]</span></p> </div> </div> <div class="row" id="login-content"> <div class="col-md-4"> <h5><?php _e('log in link:','clsc'); ?></h5> <input name="clsc_options_array[login_link]" placeholder="<?php _e('example: /log-in/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['login_link']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log in string:','clsc'); ?></h5> <input name="clsc_options_array[login_string]" placeholder="<?php _e('example: log in', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['login_string']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log in class:','clsc'); ?></h5> <input name="clsc_options_array[login_class]" placeholder="<?php _e('example: login_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['login_class']; ?>" /> </div> </div> <div class="row top-buffer" id="logout-content"> <div class="col-md-4"> <h5><?php _e('log out link:','clsc'); ?></h5> <input name="clsc_options_array[logout_link]" placeholder="<?php _e('example: /log-out/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['logout_link']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log out string:','clsc'); ?></h5> <input name="clsc_options_array[logout_string]" placeholder="<?php _e('example: log out', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['logout_string']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('log out class:','clsc'); ?></h5> <input name="clsc_options_array[logout_class]" placeholder="<?php _e('example: logout_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['logout_class']; ?>" /> </div> </div> <div class="row top-buffer" id="account-content"> <div class="col-md-4"> <h5><?php _e('account link:','clsc'); ?></h5> <input name="clsc_options_array[account_link]" placeholder="<?php _e('example: /my-account/', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['account_link']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('account string:','clsc'); ?></h5> <input name="clsc_options_array[account_string]" placeholder="<?php _e('example: account', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['account_string']; ?>" /> </div> <div class="col-md-4"> <h5><?php _e('account class:','clsc'); ?></h5> <input name="clsc_options_array[account_class]" placeholder="<?php _e('example: account_style', 'clsc') ?>" class="form-control" type="text" value="<?php echo $options['account_class']; ?>" /> </div> </div> </div> <?php @submit_button(); ?> </form> </div> <?php }
Comments
Post a Comment