Creating WP Plug in - wordpress

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.

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');

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/

Manage custom posttype function using an additional menu in dashboard

I have created a child theme for twenty-seventeen in wordpress and created a slider function to display slider images and also a custom post type for the function.
Now I created an extra menu under appearances in dashboard as Slider settings and I need to manage the slider using that settings.
In that settings I need to have the following
o Enable slider option (check box)
o Enable slider only for logged in users (check box)
o Set a global title for slider block (text field)
I Added in back end but no condition added in front end to display slides based on this.
How could I do this?
You can use this code and change page for rendering options:
<?php
add_action('admin_menu', 'create_menu');
function create_menu() {
add_options_page(__( 'Plugin Settings', 'textdomain' ),__( 'Plugin Settings', 'textdomain' ), 'administrator', __FILE__, 'settings_page', __FILE__);
add_action( 'admin_init', 'mysettings' );
}
function mysettings() {
register_setting( 'settings-group', 'enable_slider' );
register_setting( 'settings-group', 'enable_slider_loggedin' );
register_setting( 'settings-group', 'slider_title' );
}
function settings_page() {
?>
<div class="wrap">
<h2><?php _e('PluginSettings','textdomain'); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( 'settings-group' ); ?>
<?php do_settings_sections( 'settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e(' Enable slider','textdomain'); ?></th>
<td>
<input name="enable_slider" type="checkbox" value="1" <?php checked( '1', get_option( 'enable_slider' ) ); ?> />
<p class="description"></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Enable slider only for logged in users','textdomain'); ?></th>
<td>
<input name="enable_slider_loggedin" type="checkbox" value="1" <?php checked( '1', get_option( 'enable_slider_loggedin' ) ); ?> />
<p class="description"></p>
</td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Title For Slider','textdomain'); ?></th>
<td><input type="text" name="slider_title" value="<?php echo get_option('slider_title'); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php } ?>
Using like this:
if ( get_option ('enable_slider') == 1 ) {
// enable
} else {
//disable
}

How to show WordPress admin menus in a custom dashboard widget?

I want to show WordPress administration menus in custom dashboard widgets. How to do it?
Or just paste this tested solution in theme functions.php and modify. Then wherever you need you may call your admin setting by get_option()
corrected with input from b__ and tested again
function register_mysettings() {
register_setting( 'michal-option-group', 'new_option_name' );
register_setting( 'michal-option-group', 'some_other_option' );
}
add_action( 'admin_init', 'register_mysettings' );
function add_michal_dashboard_widget(){
wp_add_dashboard_widget(
'michal_dashboard_widget', // slug.
'Michal Dashboard Widget', // title
'michal_dashboard_widget_function' // widget code
);
}
function michal_dashboard_widget_function(){
if (isset($_POST['new_option_name'])) update_option( 'new_option_name', sanitize_text_field( $_POST['new_option_name']));
if (isset($_POST['some_other_option'])) update_option( 'some_other_option', sanitize_text_field( $_POST['some_other_option']));
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<?php settings_fields( 'michal-option-group' ); ?>
<?php do_settings_sections( 'michal-option-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>
</table>
<?php submit_button(); ?>
</form>
<?php
}
add_action( 'wp_dashboard_setup', 'add_michal_dashboard_widget' );
First of all: you should create a dashboard widget. you can read more about how to do it here:
Dashboard Widgets API
Now for showing the menu you should take a look at this post:
Get all available admin pages in Wordpress
Good luck!

Wordpress settings page not saving settings to database

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.

Resources