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)?
Related
Below is my plugin. Everything works except the update part. It will show the little table with the existing values and when I click on the update button in that little table, it takes me to "Nothing Here" page, instead of calling the page again.
The url doesn't update to the page url, it still has the parameter on the end of the url too.
<?php
/*
Plugin Name: deano CRUD Operations
Plugin URI:
Description: A simple plugin that allows you to perform CRUD operations on custom database table
Version: 1.0.0
Author: Dean-O
Author URI:
License: GPL2
*/
function deanocrud() {
global $wpdb;
//$table_name = $wpdb->prefix . 'books';
$table_name = 'books';
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
if (isset($_POST['newsubmit'])) {
error_log('newsubmit here');
$wp_id = $_POST['wp_id'];
$title = $_POST['title'];
$author = $_POST['author'];
$wpdb->query("INSERT INTO $table_name(wp_id,title,author) VALUES('$wp_id','$title','$author')");
//echo "<script>location.replace('admin.php?page=crud.php');</script>";
//wp_redirect( "http://localhost/tadpolewp/deano-plugin-successful/" );
//wp_redirect( "http://localhost/tadpolewp/deano-crud/" );
?>
<script type="text/javascript">
document.location.href="http://localhost/tadpolewp/deano-crud/";
</script>
<?php
}
if (isset($_POST['uptsubmit'])) {
error_log('uptsubmit here');
$id = $_POST['uptid'];
$title = $_POST['title'];
$author = $_POST['author'];
error_log('upt_id = '.$id);
error_log('title = '.$title);
error_log('author = '.$author);
$wpdb->query("UPDATE $table_name SET title='$title',author='$author' WHERE id='$id'");
//echo "<script>location.replace('admin.php?page=crud.php');</script>";
//echo "<script>location.replace('http://localhost/tadpolewp/deano-crud/');</script>";
?>
<script type="text/javascript">
document.location.href="http://localhost/tadpolewp/deano-crud/";
</script>
<?php
}
if (isset($_GET['del'])) {
$del_id = $_GET['del'];
$wpdb->query("DELETE FROM $table_name WHERE user_id='$del_id'");
//echo "<script>location.replace('admin.php?page=crud.php');</script>";
echo "<script>location.replace('http://localhost/tadpolewp/deano-crud/');</script>";
}
?>
<div class="wrap">
<h2>CRUD Operations</h2>
<table class="wp-list-table widefat striped">
<thead>
<tr>
<th width="25%">USER_ID1</th>
<th width="25%">__</th>
<th width="25%">Title</th>
<th width="25%">Author</th>
<th width="25%">Actions</th>
</tr>
</thead>
<tbody>
<form action="" method="post">
<tr>
<td><input type="text" value="AUTO_GENERATED" disabled></td>
<td><input type="hidden" name="wp_id" value="<?php echo esc_attr( $current_user_id ); ?>" ></td>
<td><input type="text" id="title" name="title"></td>
<td><input type="text" id="author" name="author"></td>
<td><button id="newsubmit" name="newsubmit" type="submit">INSERT</button></td>
</tr>
</form>
<?php
$result = $wpdb->get_results("SELECT * FROM $table_name where wp_id='$current_user_id'");
foreach ($result as $print) {
echo "
<tr>
<td width='25%'>$print->wp_id</td>
<td width='25%'></td>
<td width='25%'>$print->title</td>
<td width='25%'>$print->author</td>
<td width='25%'><a href='index.php?page=deano-crud.php&upt=$print->id'><button type='button'>UPDATE</button> <a href='index.php?page=deano-crud.php&del=$print->id'><button type='button'>DELETE</button></a></td>
</tr>
";
}
?>
</tbody>
</table>
<br>
<br>
<?php
if (isset($_GET['upt'])) {
$upt_id = $_GET['upt'];
error_log('upt_id = '.$upt_id);
$result = $wpdb->get_results("SELECT * FROM $table_name WHERE id='$upt_id'");
foreach($result as $print) {
$title = $print->title;
$author = $print->author;
error_log('title = '.$title);
error_log('author = '.$author);
error_log('id = '.$print->id);
}
echo "
<table class='wp-list-table widefat striped'>
<thead>
<tr>
<th width='25%'>ID</th>
<th width='25%'>Title</th>
<th width='25%'>Author</th>
<th width='25%'>Actions</th>
</tr>
</thead>
<tbody>
<form action='' method='post'>
<tr>
<td width='25%'>$print->id <input type='hidden' id='uptid' name='uptid' value='$print->id'></td>
<td width='25%'><input type='text' id='title' name='title' value='$print->title'></td>
<td width='25%'><input type='text' id='author' name='author' value='$print->author'></td>
<td width='25%'><button id='uptsubmit' name='uptsubmit' type='submit'>UPDATE</button> <a href='index.php?page=deano-crud'><button type='button'>CANCEL</button></a></td>
</tr>
</form>
</tbody>
</table>";
}
?>
</div>
<?php
}
add_shortcode('deanocrud_sc','deanocrud');
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');
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/
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');
?>
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.