Custom Wordpress Plugin Not Saving Settings - php

I'm working on my first custom WP plugin and I'm trying to register a setting using the Settings API. I've followed a number of guides, and I've successfully created an admin menu page with the section and field defined in the plugin, but the single setting (in the code its fbm_lockout_updates) will not save, and no table in the database has been created for it.
EDIT: It is in fact in the options table. I was mistaken about that much. However, my options page still cant seem to update it, so I suppose my callback function is bad?
After trying a dozen things, I'm not sure where I'm going wrong. Here is the relevant code edited for brevity:
/* Create Menu */
add_action( 'admin_menu', 'fbm_config_menu' );
function fbm_config_menu() {
$page_title = 'Sample Plugin';
$menu_title = 'Sample Plugin Config';
$capability = 'manage_options';
$menu_slug = 'fbm_config';
$function = 'fbm_config_page';
add_menu_page(
$page_title,
$menu_title,
$capability,
$menu_slug,
$function
);
}
/* Register Settings and Fields */
function fbm_register_settings() {
register_setting( 'fbm_config', 'fbm_lockout_updates');
add_settings_section(
'fbm_restriction_section',
'Development Restrictions',
'fbm_restriction_callback',
'fbm_config'
);
add_settings_field(
'fbm_lockout_updates_field',
'Lockout Updates',
'fbm_lockout_field_callback',
'fbm_config',
'fbm_restriction_section'
);
}
add_action( 'admin_init', 'fbm_register_settings' );
/* Settings Callbacks */
function fbm_restriction_callback() {
?>
<p><?php esc_html_e( 'Description of Setting Section', 'fbm_config' ); ?></p>
<?php
}
function fbm_lockout_field_callback() {
$setting = get_option('fbm_lockout_updates');
?>
<input type="checkbox" name="fbm_lockout_updates" value="0" <?php checked('1', $setting); ?> >
<?php
}
/* Load Admin Page */
function fbm_config_page(){
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error( 'fbm_con_messages', 'fbm_con_message', __( 'Settings Saved', 'fbm_config' ), 'updated' );
}
settings_errors( 'fbm_con_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'fbm_config' );
do_settings_sections( 'fbm_config' );
submit_button( 'Save Settings' );
?>
</form>
<div>
<?php }
?>

In this case register_setting will save your data in wp-options table, take a look into it.
Recommended if you want your setting to be shown in the wordpress api use both admin and api hook
add_action( 'rest_api_init', 'bm_register_settings' );
add_action( 'admin_init', 'fbm_register_settings' );

Related

How to add and remove a value in a dropdown list within settings of a wordpress plugin

I am creating my first plugin. It creates two custom columns for a woocomerce product. The first column takes values from a textfield and the second from a dropdown list. The first has SKUs the second has Suppliers.
I then created a settings page for my plugin. I would like to be able to add a new supplier and be able to remove it. Is it possible?
This is the part of the settings:
add_action( 'admin_menu', 'wpcfqe_add_admin_menu' );
add_action( 'admin_init', 'wpcfqe_settings_init' );
function wpcfqe_add_admin_menu( ) {
add_options_page( 'Custom Fields and Quick Edit', 'Custom Fields and Quick Edit', 'manage_options', 'custom_fields_and_quick_edit', 'wpcfqe_options_page' );
}
function wpcfqe_settings_init( ) {
register_setting( 'pluginPage', 'wpcfqe_settings' );
add_settings_section(
'wpcfqe_pluginPage_section',
__( 'Add new Supplier', 'wordpress' ),
'wpcfqe_settings_section_callback',
'pluginPage'
);
add_settings_field(
'wpcfqe_text_field_0',
__( 'Supplier', 'wordpress' ),
'wpcfqe_text_field_0_render',
'pluginPage',
'wpcfqe_pluginPage_section'
);
}
function wpcfqe_text_field_0_render( ) {
$options = get_option( 'wpcfqe_settings' );
?>
<input type='text' name='wpcfqe_settings[wpcfqe_text_field_0]' value='<?php echo $options['wpcfqe_text_field_0']; ?>'>
<?php
}
function wpcfqe_settings_section_callback( ) {
echo __( 'Settings for Supplier', 'wordpress' );
}
function wpcfqe_options_page( ) {
?>
<form action='options.php' method='post'>
<h2>Custom Fields and Quick Edit</h2>
<?php
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>
</form>
<?php
}
How to save new values to the supplier dropdown list
You can modify any wordpress data using $wpdb class. It allows you to create custom WP database queries with which you can modify data in db. It is very useful when it comes to creating a plugin and you should get familiar with it.
https://codex.wordpress.org/Class_Reference/wpdb#

Displaying settings options up using WordPress options API

I'm having issues getting my field to save using WordPresses setting API
I have 1 option page, with 1 section and 1 field. So it's pretty straight forward. Everything appearing/rendering fine, but I can't save the fields.
class LocalSEOAdmin {
var $slug = 'local_seo_settings';
var $name = "Local SEO";
public function __construct(){
// When WP calls admin_menu, call $this->admin_settings
if ( is_admin() ) {
add_action('admin_menu', array( &$this, 'add_options_page' ) ); // on admin_menu call $this->init_admin_menu
add_action( 'admin_init', array( &$this, 'add_setting_fields' ) );
}
}
public function add_options_page(){
add_options_page(
$this->name, // Page title
$this->name, // Menu Title
'manage_options', // Role
$this->slug, // Menu slug
array( &$this, 'local_seo_admin_menu_callback' ) // Callback to render the option page
);
}
public function local_seo_admin_menu_callback(){
?>
<div class='wrap'>
<h1><?php echo $this->name ?> Options</h1>
<form method='post' action='options.php'>
<?php
settings_fields( 'address_settings_section' );
do_settings_sections( $this->slug );
submit_button();
?>
</form>
</div>
<?php
}
public function add_setting_fields(){
add_settings_section(
'address_settings_section', // ID used to identify this section and with which to register options
'Address Options', // Title to be displayed on the administration page
array( &$this, '_render_address_options'), // Callback used to render the description of the section
$this->slug // Page on which to add this section of options
);
add_settings_field(
'address_line_1', // ID used to identify the field throughout the theme
'Address Line 1', // The label to the left of the option interface element
array( &$this, '_render_address_line_1'), // The name of the function responsible for rendering the option interface
$this->slug, // The page on which this option will be displayed
'address_settings_section', // The name of the section to which this field belongs
array( // The array of arguments to pass to the callback. In this case, just a description.
__( 'Activate this setting to display the header.', 'sandbox' ),
)
);
register_setting(
'address_settings_section',
'address_settings_section'
);
}
public function _render_address_options(){
echo '<p>Add you address</p>';
}
public function _render_address_line_1(){
$option = get_option( 'address_line_1' );
$html = '<input type="text" id="address_line_1" name="address_line_1" value="' . $option . '" />';
echo $html;
}
}
new LocalSEOAdmin;
I'm not sure what's going on, but I feel I've mixed up the required fields someone. The fields are showing in WordPress and when I hit update the "setting saved." messages comes up, but nothing gets saved.
there is just a little problem when you register your setting.
base on register_setting, the second parameter, is $option_name so you must put send name of you inputs
register_setting(
'address_settings_section',
'address_line_1'
);
and now it's worked.
you can find more about creating options pages in wordpress site.
In register_setting()
First paramerter is your option group name &
Second parameter is option name which is given in add_settings_field() , in your case is should be address_line_1 not address_settings_section
Finally
register_setting(
'address_settings_section',
'address_line_1'
)

wordpress theme options not saving

I am writing a WordPress plugin that makes use of the Settings API to register a few options. When I try to save my options, they do not save. The fields are blank when the page reloads and there is no message that the options saved. I've been racking my brain for hours and cannot find any answers. I would be very grateful for any assistance. Here is the (slightly modified) start to my code for the options page:
<?php
/**************************************** Register Settings Menu & Page ****************************************/
add_action( 'admin_menu', 'rv_admin_menu' );
function rv_admin_menu() {
// Parent Slug, Page Title, Menu Title, Capability, Menu Slug, Callback Function
add_submenu_page('slug', 'Misc. Options', 'Misc. Options', 'manage_options', 'rv-options-page', 'rv_options_create_page');
}
/**************************************** Register Settings ****************************************/
add_action( 'admin_init', 'rv_admin_init' );
function rv_admin_init() {
/** Add the Settings Group **/
register_setting( 'rv-options-group', 'rv-settings' ); // Option Group & Options Name
/* Add Custom Footer Section & Fields */
// ID, Title, Callback, Menu Page
add_settings_section( 'footer-section', 'Custom Footer', 'custom_footer_section_callback', 'rv-options-page' );
// ID, Title, Callback, Menu Page, Associated Section
add_settings_field( 'footer-content', 'Footer Content', 'custom_footer_callback', 'rv-options-page', 'footer-section' );
/* Add Author Box Section & Fields */
add_settings_section( 'author-box', 'Author Box', 'author_box_section_callback', 'rv-options-page' );
add_settings_field( 'author-box-enable', 'Globally Enable Author Box', 'author_box_enable_callback', 'rv-options-page', 'author-box' );
add_settings_field( 'author-box-title', 'Author Box Title', 'author_box_title_callback', 'rv-options-page', 'author-box' );
}
/**************************************** Footer ****************************************/
/** Custom Footer Section Intro **/
function custom_footer_section_callback() {
echo "Add your footer's custom HTML.";
}
/* Custom Footer Text Area */
function custom_footer_callback() {
$settings = (array) get_option( 'rv-settings' );
$footer = esc_attr( $settings['footer'] );
echo "<textarea rows='12' cols='100' name='rv-settings[footer]' value='$footer'></textarea>";
}
/**************************************** Author Box ****************************************/
/** Author Box Section Intro **/
function author_box_section_callback() {
echo "Customize the Author Box";
}
/* Globally Enable Author Box on Posts */
function author_box_enable_callback() {
$settings = (array) get_option( 'rv-settings' );
$enable_author_box = esc_attr( $settings['enable_author_box'] );
echo "<input type='checkbox' name='rv-settings[enable_author_box]' value='$enable_author_box' />";
}
/* Custom Author Box Title */
function author_box_title_callback() {
$settings = (array) get_option( 'rv-settings' );
$author_box_title = esc_attr( $settings['author_box_title'] );
echo "<input size='100' type='text' name='rv-settings[author_box_title]' value='$author_box_title' />";
}
/**************************************** Create Settings Page ****************************************/
function rv_options_create_page() { ?>
<div class="wrap">
<h2>RV Options</h2>
<form action="options.php" method="POST">
<?php settings_fields( 'rv-options-group' ); // Options Group ?>
<?php do_settings_sections( 'rv-options-page' ); // Menu Page ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}

Error: options page not found in Wordpress Plugin

When developing a plugin, using the Settings API do create an options page many developers get this message after saving the options and posting to "options.php"
Error: options page not found in Wordpress Plugin
Even adding a function to register this settings like:
function pg_register_settings()
{
add_settings_section(
'setting_section_oauth', // ID
'Instagram API Client Info', // Title
array( $this, 'print_section_oauth_info' ), // Callback
$this->plugin_slug.'-setting-admin' // Page
);
add_settings_field(
'key', // ID
'Application Key', // Title
array( $this, 'field_key_callback' ), // Callback
$this->plugin_slug.'-setting-admin', // Page
'setting_section_oauth' // Section
);
register_setting( 'bitloom-instagram-options', 'key' );
}
And using the code below on the form
<form method="post" action="options.php">
<?php
settings_fields( 'bitloom-instagram-options' );
do_settings_sections( 'bitloom-instagram-setting-admin' );
submit_button();
?>
</form>
Whats is missing?
You need to register settings with the admin_init hook, otherwise it won´t work.
add_action( 'admin_init', 'pg_register_settings' );

Targeting a specific `<div>` via Wordpress Plugin

Insert HTML Input to a themes template file?
My question is this:
Currently with my code, I can have whatever I've set in my custom settings page appear on
the end of every post using, of course, a WordPress filter.
The only thing is, I don't want what I input to go anywhere within a post. What I am trying to
achieve, is to have my input injected into the current themes home.php template file, within a <div></div> tag that's contained within that template file. The <div> in question has an ID attached, which is <div id="category-inner">, so is there any way use it's ID to target it in my plugin?
I've successfully managed to do it, by editing the actual home.php template file and inserting a bit of php directly there to show the user input, but that totally goes against what I'm trying to do, which is to not have to edit the source code of the template file and only have to use my plugin to insert the users inputted text in that specific location (the <div> mentioned above).
My plugin is only ever going to be adding user input in one place on the site, and it will never change. And the place it will always go in, is where I mentioned above.
Below is my plugin code:
<?php
/*/
Plugin Name: Custom Text Adder
Plugin URI: N/A
Description: Demonstrates how rubbish I am at pretty much everything I want to do
Version: 101
Author: JD
Author URI: N/A
/*/
// insert custom plugin settings menu
add_action('admin_menu', 'custom_create_menu');
add_filter('the_content', 'customhead_the_content');
function customhead_the_content($content) {
$output = $content;
$output .= '<div id="category-inner">';
$output .= get_option('post_text');
$output .= '</div>';
return $output;
}
// Add Font-Size to WYSIWYG Editor
function wp_editor_fontsize_filter( $options ) {
array_shift( $options );
array_unshift( $options, 'fontsizeselect');
array_unshift( $options, 'formatselect');
return $options;
}
add_filter('mce_buttons_2', 'wp_editor_fontsize_filter');
// Create Custom Menu
function custom_create_menu() {
//create new top-level menu
add_menu_page('Custom Plugin Settings', 'Custom Settings', 'administrator', __FILE__, 'custom_settings_page',plugins_url('/img/icon.png', __FILE__));
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
// Register our settings
function register_mysettings() {
register_setting( 'custom-settings-group', 'new_option_name' );
register_setting( 'custom-settings-group', 'some_other_option' );
register_setting( 'custom-settings-group', 'option_etc' );
register_setting( 'custom-settings-group', 'font_size' );
register_setting( 'custom-settings-group', 'post_text' );
}
function custom_settings_page() {
?>
<!-- Custom Settings Page Container -->
<div class="wrap">
<h2>Custom Text</h2>
<form method="post" action="options.php">
<?php settings_fields( 'custom-settings-group' ); ?>
<table class="form-table">
<?php /* Bring the editor onto the page */
wp_editor( '', 'post_text', $settings = array() );
// 4.
// Custom buttons for the editor.
// This is a list separated with a comma after each feature
// eg. link, unlink, bold, ...
$settings = array(
'textarea_name' => 'post_text',
'media_buttons' => true,
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
'bullist,blockquote,|,justifyleft,justifycenter' .
',justifyright,justifyfull,|,link,unlink,|' .
',spellchecker,wp_fullscreen,wp_adv'
)
);
submit_button( 'Save everything', 'primary', 'submit' ); ?>
</table>
</form>
</div>
<?php }; ?>
Here is the screenshot which will be able to explain this in the most concise way possible:
http://i.imgur.com/hiEjEsA.jpg
Again, any and all help is hugely appreciated and will hopefully stop my brain from hurting!
You all rock,
Casey. :)
I'm not sure if you want this to be a pure PHP solution, but since you have the ID of the div you could target it with jQuery and insert the text into the div on page load (or form submit) with something like this:
$(document).ready(function() {
var userText = $('#input-id').val();
$('#category-inner').html(userText);
})

Categories