Wordpress settings page not saving settings to database - wordpress

I am developing my first Wordpress plugin. I've been following some guides on creating a settings page.
I have the following page which correctly displays the value of the fields in the database. When I go to the page, edit the fields and press "Save changes" the changes aren't saved to the database. If I change the values directly in the database, then the values does show up correctly in the input fields, but I still cannot update the values from my page.
Can you see any obvious errors that I've made or things that I'm missing?
<?php
add_action('admin_menu', 'SetupPage');
function SetupPage()
{
add_action('admin_init', 'RegisterSettings');
// Setup administration menu item
if (function_exists('add_options_page'))
{
add_menu_page(__("TestPage"), __("TestPage"), "manage_options", __FILE__, 'PageContent', plugins_url('/images/icon.png', __FILE__));
}
}
function RegisterSettings()
{
// Add options to database if they don't already exist
add_option("test_option1", "", "", "yes");
add_option("test_option2", "", "", "yes");
add_option("test_option3", "", "", "yes");
// Register settings that this form is allowed to update
register_setting('test_settings', 'test_option1');
register_setting('test_settings', 'test_option2');
register_setting('test_settings', 'test_option3');
}
?>
<?php
function PageContent()
{
if (!current_user_can('manage_options'))
wp_die(__("You don't have access to this page"));
?>
<div class="wrap">
<h2><?_e("Test settings")?></h2>
<form method="post">
<?php settings_fields('test_settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">test_option1</th>
<td><input type="text" name="test_option1" value="<?php echo get_option('test_option1'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">test_option2</th>
<td><input type="text" name="test_option2" value="<?php echo get_option('test_option2'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">test_option3</th>
<td><input type="text" name="test_option3" value="<?php echo get_option('test_option3'); ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save changes') ?>" />
</p>
</form>
</div>
<?php
}
?>

Looks to me like you need to add the action="options.php" in the form tag. Otherwise it seems right. No doubt you've looked at this codex page, since your code is very similar, but that's about the only difference I see.

It is also worth noting that if you have this problem, check to see if your trying to echo the settings fields.
So for me I had this:
echo '
<div class="wrap">
<h1>Theme Settings <img src="' . get_stylesheet_directory_uri('stylesheet_directory') . '/images/site-icon.png" width="32" height="32" /></h1>
<form method="post" action="options.php">
' . settings_fields( 'custom-settings-group' ) . '
' . do_settings_sections( 'custom-settings-group' ) . '
<table class="form-table">
<tr valign="top">
<th scope="row">Brisbane Hours</th>
<td><textarea rows="4" cols="40" name="brisbane_hours">' . esc_attr( get_option('brisbane_hours') ) . '</textarea></td>
</tr>
<tr valign="top">
<th scope="row">Adelaide Hours</th>
<td><input type="text" value="' . esc_attr( get_option('adelaide_hours') ) . '"/></td>
</tr>
</table>
' . submit_button() . '
</form>
</div>
';
Which means that these two fields were getting echoed:
settings_fields( 'custom-settings-group' )
do_settings_sections( 'custom-settings-group' )
Changing to this fixed it for me.
<?php settings_fields( 'snowys-custom-settings-group' ); ?>
<?php do_settings_sections( 'snowys-custom-settings-group' ); ?>

I checked the code and tested from my side and i did some changes see the complete working code
<?php
/**
* Plugin Name: Testing Plugin
*/
add_action('admin_menu', 'SetupPage');
add_action('admin_init', 'RegisterSettings');
function SetupPage() {
add_menu_page(__("TestPage"), __("TestPage"), "manage_options", __FILE__, 'PageContent', plugins_url('/images/icon.png', __FILE__));
}
function RegisterSettings() {
// Add options to database if they don't already exist
add_option("test_option1", "", "", "yes");
add_option("test_option2", "", "", "yes");
add_option("test_option3", "", "", "yes");
// Register settings that this form is allowed to update
register_setting('test_settings', 'test_option1');
register_setting('test_settings', 'test_option2');
register_setting('test_settings', 'test_option3');
}
?>
<?php
function PageContent() {
if (!current_user_can('manage_options'))
wp_die(__("You don't have access to this page"));
?>
<div class="wrap">
<h2><? _e("Test settings") ?></h2>
<form method="post" action="options.php">
<?php settings_fields('test_settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">test_option1</th>
<td><input type="text" name="test_option1" value="<?php echo get_option('test_option1'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">test_option2</th>
<td><input type="text" name="test_option2" value="<?php echo get_option('test_option2'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">test_option3</th>
<td><input type="text" name="test_option3" value="<?php echo get_option('test_option3'); ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save changes') ?>" />
</p>
</form>
</div>
<?php
}
?>
Hope it may help for someone :)

Which browser are you using? It may sound weird, but using Chrome, I've found a couple of plugins that don't save settings properly.
Not a great technical answer, but if the plugin is just for your own use and you can get its admin functions working in Firefox and IE it may be easier to settle for 'good enough'.

Do you have a multisite set? If so, the setting will be created in wp_options table instread of local wp_*_options single site table. You have to use
add_blog_option( get_current_blog_id(), "option_name", "" );
This ussaly occurs when you have had forced multisite on existing blog and now try to manage options.

Related

Wordpress add user number field in contact information part

I am wondering how to achieve the following.
I try to create a number field input in the wordpress user profile under contact information.
So far if i use show_user_profile & edit_user_profile hooks, the field will appear on the bottom of the profile page.
Is it possible to create a number field under the contact information part (standard in wordpress?)
I tried to add different priorities to the actions, but that didnt solve it.
So far i have the following:
function addurenuser(){
$userid = get_current_user_id();
if ( current_user_can('update_plugins',$userid)){ ?>
<table class="form-table">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr( get_the_author_meta( 'uren', $user->ID ) ); ?>">
</td>
</tr>
</table> <?php
}
}
add_action( 'show_user_profile', 'addurenuser' );
add_action( 'edit_user_profile', 'addurenuser' );
Use this code
function addurenuser()
{
echo '<script>
jQuery(document).ready(function($){
$(".your-custom-field").insertAfter($(".user-email-wrap").closest("table"));
});
</script>';
$userid = get_current_user_id();
if (current_user_can('update_plugins', $userid)) { ?>
<table class="form-table your-custom-field">
<tr>
<th>
<label>Uren</label>
</th>
<td>
<input type="number" name="uren" id="uren" value="<?php echo esc_attr(get_the_author_meta('uren', $userid->ID)); ?>">
</td>
</tr>
</table> <?php
}
}
add_action('show_user_profile', 'addurenuser');
add_action('edit_user_profile', 'addurenuser');

Custom User Settings In A Plugin Template

I have no problem submitting the initial default user data, and no problems recalling data. However, i can not figure out why this form is throwing an error (needed to submit user data).
Here is the code I'm using to create the default data...
// DEFAULT USER OPTIONS
add_action( 'admin_init', 'sci_pages_register_setting' );
function sci_pages_register_setting()
{
$user = wp_get_current_user();
$default_options=array(
'sci_avatar'=>'http://.../blank_avatar.png',
'sci_facebook'=>'',
'sci_twitter'=>''
);
add_option($user->user_nicename . '_plugin_options',$default_options);
register_setting( $user->user_nicename . '_plugin_options', $user->user_nicename . '_plugin_options', 'sci_pages_sanitize' );
}
function sci_pages_sanitize( $in )
{
return $in;
}
And this is the form code... And I'm really not sure what it is that I need to do for this to...
A. Stop throwing up an error
B. Allow users to submit this information from the front-end page.
<?PHP
function sci_options_page()
{
?>
<div class="wrap">
<h1>User Options</h1>
<?PHP
$user = wp_get_current_user();
$options = get_option($user->user_nicename . '_plugin_options');
settings_fields($user->user_nicename . '_plugin_options');
?>
<form method="post" action="#">
<table class="form-table">
<tr valign="top">
<th><h2>General</h2><th>
</tr>
<tr valign="top">
<th scope="row"><label for="sci_avatar">Avatar:</label></th>
<td><input type="text" id="sci_logo" size="50" name="sci_options[sci_avatar]" value="<?php echo $options['sci_avatar']; ?>" /></td>
</tr>
<tr valign="top">
<th><h2>Social Links</h2><th>
</tr>
<tr valign="top">
<th scope="row"><label for="sci_facebook">Facebook:</label></th>
<td><input type="text" id="sci_vimeo" size="50" name="sci_options[sci_facebook]" value="<?php echo $options['sci_facebook']; ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="sci_twitter">Twitter:</label></th>
<td><input type="text" id="sci_skype" size="50" name="sci_options[sci_twitter]" value="<?php echo $options['sci_twitter']; ?>" /></td>
</tr>
</table>
<?php
submit_button();
?>
</form>
</div>
<?php
}
sci_options_page();
?>
This is what I see in the front-end when accessing the page...
What am I doing wrong (other than feeling like a complete noob right now)?

How do I create a form with a textbox when writing a Wordpress Plugin/

I can't seem to understand how to create a form with the Wordpress Settings API that only includes a textbox where the user can insert some code and then click a button that says "Save Changes" For reference I am going for something like this:
Insert Headers an Footers Plugin Screenshot
My current code: https://paste.fedoraproject.org/paste/l8JjhNYvp6NPFG1saEsc4Q
This is one example of creating an options page: https://codex.wordpress.org/Creating_Options_Pages
<?php
// create custom plugin settings menu
add_action('admin_menu', 'my_cool_plugin_create_menu');
function my_cool_plugin_create_menu() {
//create new top-level menu
add_menu_page('My Cool Plugin Settings', 'Cool Settings', 'administrator', __FILE__, 'my_cool_plugin_settings_page' , plugins_url('/images/icon.png', __FILE__) );
//call register settings function
add_action( 'admin_init', 'register_my_cool_plugin_settings' );
}
function register_my_cool_plugin_settings() {
//register our settings
register_setting( 'my-cool-plugin-settings-group', 'new_option_name' );
register_setting( 'my-cool-plugin-settings-group', 'some_other_option' );
register_setting( 'my-cool-plugin-settings-group', 'option_etc' );
}
function my_cool_plugin_settings_page() {
?>
<div class="wrap">
<h1>Your Plugin Name</h1>
<form method="post" action="options.php">
<?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">New Option Name</th>
<td><input type="text" name="new_option_name" value="<?php echo esc_attr( get_option('new_option_name') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Some Other Option</th>
<td><input type="text" name="some_other_option" value="<?php echo esc_attr( get_option('some_other_option') ); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Options, Etc.</th>
<td><input type="text" name="option_etc" value="<?php echo esc_attr( get_option('option_etc') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
You can also use another method an use functions add_settings_section and add_settings_field which both need a callback function: http://qnimate.com/wordpress-settings-api-a-comprehensive-developers-guide/

WordPress logs out after using /wp-admin/admin.php

i'm currently updating a system with a new function. I'm adding the show_user_profile and edit_user_profile action to add custom data into the user-view:
<script type="text/javascript">
function bindRemoveButton() {
jQuery(function($) {
$('table.available-users button.remove').on('click', function() {
$(this).parent().parent().remove();
});
});
}
bindRemoveButton();
</script>
<table class="form-table available-users">
<tbody>
<tr>
<th>
<label for="accounts"><?php print __('Assigned accounts', ZVA_I18N); ?></label>
<br />
<br />
<a href="<?php bloginfo('template_directory'); ?>/templates/admin/user_add_box.php#TB_iframe=true&width=600&height=550" title="<?php print __('Add User', ZVA_I18N); ?>" class="thickbox button button-primary"><?php print __('Add', ZVA_I18N); ?></button>
</th>
<td>
<table class="wp-list-table widefat fixed users">
<thead>
<tr>
<th scope="col" class="manage-colum" style="padding-left: 20px;"><span><?php print __('Username', ZVA_I18N); ?></span></th>
<th scope="col" class="manage-colum"><span><?php print __('Date From', ZVA_I18N); ?></span></th>
<th scope="col" class="manage-colum"><span><?php print __('Date To', ZVA_I18N); ?></span></th>
<th scope="col" class="manage-colum"><span><?php print __('Action', ZVA_I18N); ?></span></th>
</tr>
</thead>
<tbody>
<?php
foreach($user_available AS $aindex => $available_user) {
$user = new WP_User($available_user['user_id']);
?>
<tr>
<td><?php print $user->user_login; ?> (<?php print $user->display_name; ?>)</td>
<td>
<input type="text" name="jumper_day_from[<?php print $user->ID; ?>]" size="2" value="<?php print date('d', $available_user['from']); ?>" placeholder="DD" />.
<input type="text" name="jumper_month_from[<?php print $user->ID; ?>]" size="2" value="<?php print date('m', $available_user['from']); ?>" placeholder="MM" />.
<input type="text" name="jumper_year_from[<?php print $user->ID; ?>]" size="4" value="<?php print date('Y', $available_user['from']); ?>" placeholder="YYYY" />
</td>
<td>
<input type="text" name="jumper_day_to[<?php print $user->ID; ?>]" size="2" value="<?php print date('d', $available_user['to']); ?>" placeholder="DD" />.
<input type="text" name="jumper_month_to[<?php print $user->ID; ?>]" size="2" value="<?php print date('m', $available_user['to']); ?>" placeholder="MM" />.
<input type="text" name="jumper_year_to[<?php print $user->ID; ?>]" size="4" value="<?php print date('Y', $available_user['to']); ?>" placeholder="YYYY" />
</td>
<td>
<button type="button" class="remove button button-primary"><?php print __('Remove', ZVA_I18N); ?></button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
Okay, no problems. See the Line 19 on my code, here i add a Link/Button to open a new Thickbox with additional content. When i click on the link, a thickbox pops up and displays the content from the File /wp-content/themes/MyTheme/templates/admin/user_add_box.php.
To use WordPress's own functions (like $wpdb or other), im including the admin.php on the user_add_box.php - YES, currently that's only these line on the file:
<?php
define('WP_ADMIN', true);
/* Nativate to /wp-admin/ directory */
require_once(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/wp-admin/admin.php');
?>
When i'm open the tickbox, the login will be shown - curiously. After that i'm complete logged out from WordPress.
I work very very long with WordPress and never had such problems. Especially since I also developing for years themes and plugins for WordPress.
Can you tell me, what i'm doing wrong? Why the system logs me out?
Thanks for your help.
EDIT
After see the core file, i see i must define constants like define('WP_ADMIN', true);, but thats dont solve the problem.
In the auth_redirect method (wp-includes/pluggable.php) will be checked if the file comes from wp-admin directory:
if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') )
If not, the system will be redirect automatical to the wp-login.php.
Solve it to rewrite the $_SERVER['REQUEST_URI'] variable:
<?php
$_SERVER['REQUEST_URI'] = 'wp-admin';
require_once(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/wp-admin/admin.php');
?>
Otherwise, add only wp-load.php and wp-admin/includes/admin.php:
<?php
$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_REFERER'];
require_once(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/wp-load.php');
require_once(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . '/wp-admin/includes/admin.php');
?>

Creating WP Plug in

I'm up to study about creating WP Plug in and so I read this page - http://codex.wordpress.org/Creating_Options_Pages
I try the example given in that page which is the below code :
<?php
// create custom plugin settings menu
add_action('admin_menu', 'baw_create_menu');
function baw_create_menu() {
//create new top-level menu
add_menu_page('BAW Plugin Settings', 'BAW Settings', 'administrator', __FILE__, 'baw_settings_page',plugins_url('/images/icon.png', __FILE__));
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
function register_mysettings() {
//register our settings
register_setting( 'baw-settings-group', 'new_option_name' );
register_setting( 'baw-settings-group', 'some_other_option' );
register_setting( 'baw-settings-group', 'option_etc' );
}
function baw_settings_page() {
?>
<div class="wrap">
<h2>Your Plugin Name</h2>
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">New Option Name</th>
<td><input type="text" name="new_option_name" value="<?php echo get_option('new_option_name'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Some Other Option</th>
<td><input type="text" name="some_other_option" value="<?php echo get_option('some_other_option'); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row">Options, Etc.</th>
<td><input type="text" name="option_etc" value="<?php echo get_option('option_etc'); ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php } ?>
Everything is just fine but when I test the form in the WP admin, when I insert the data in the form and click update button, there is no 'update message' appear.
So my question is how to make 'update message' or 'error message' appear when people insert data in the form and click the submit button.
Thanks a lot for your help!
I'm not sure about this, but I'd suggest taking the add_action call for registering your settings out of the baw_create_menu function so that it's set up before admin menu. I think admin_init fires before admin_menu, so your register_mysettings function isn't being called. But I'm not sure about that.
Also, I'd suggest reading the following resources on the WordPress Settings API:
http://codex.wordpress.org/Settings_API
http://www.presscoders.com/wordpress-settings-api-explained/
http://ottodestruct.com/blog/2009/wordpress-settings-api-tutorial/
http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
If you use the Settings API correctly, the message will appear automatically. Of course, the other option is adding the message conditionally. I.e., check if the form was submitted, and if so, echo the message at the beginning of the form, just after the page's title.

Resources