I’m creating a custom settings page for my plugin, and I would like to have a repeater field. Example: Phone Number, I would like the user to be able to click on a ‘+’ button and add all his phone numbers (creating a settings_field for each one).
Not sure what would be the best approach. Ajax? I couldn’t see anything in the documentation or settings API.
I guess I can add an HTML input field using JS from the frontend, but how should I relate that input with my settings and menu page, so it gets stored and shown in the future?
Here is how I’m adding my field:
add_settings_field(
'sdfe_field_organization_telephone',
'Telephone',
'sdfe_field_organization_textInput_cb',
'sdfe',
'sdfe_section_organization',
[
'label_for' => 'sdfe_field_organization_telephone',
'class' => 'sdfe_row'
]
);
And here is how I’m creating the HTML output:
function sdfe_field_organization_textInput_cb( $args ) {
$options = get_option( 'sdfe_options' );
?>
<input
class="regular-text"
type="text"
id="<?php echo esc_attr( $args['label_for'] ); ?>"
name="sdfe_options[<?php echo esc_attr( $args['label_for'] ); ?>]"
value="<?php echo isset( $options[ $args['label_for'] ] ) ? $options[ $args['label_for'] ] : '' ?>" >
<button type="button">Add Telephone</button>
<?php
}
Thank you so much in advance. This is my first plugin, and I would love to learn.
]]>When a user install my plugin, I’d like some of them already checked.
How can I set some default and initial values?
Is this actually supported? If so, I’d really appreciate a pointer to appropriate documentation. lots of searches of yielded nothing of use.
]]>function __construct(){
add_action('admin_init', array($this, 'plugin_admin_init'));
}
public function create_fields(){
register_setting('dleToWp_options', 'database_name');
register_setting('dleToWp_options', 'database_prefix');
register_setting('dleToWp_options', 'version');
//Stuck here
// add_settings_section('dleToWp_options', __('Settings', 'dleToWp'), NULL, 'dleToWp');
// add_settings_field('plugin_text_string', 'Plugin Text Input', 'plugin_setting_string', 'plugin', 'dleToWp');
}
public function settings_page(){
echo '<div class="wrap">
<div class="icon32" id="icon-tools"></div>
<h2>'.__('Database converter', 'dleToWp').'</h2>
<form action="options.php" method="post">';
settings_fields('dleToWp_options');
do_settings_sections('dleToWp');
echo '</form></div>';
}
I nee create 3 fields: Database name, database prefix and select box of versions.
]]>add_settings_field('yankit', 'Strip title', array($this, 'admin_form_checkbox'), __FILE__, 'main_section', 'yankit');
function admin_form_checkbox ( $f )
{ echo "<input type='checkbox' id='$f' name='plugin_image-formatr[$f]' $checked/>"; }
This code generates the following output:
<tr valign="top">
<th scope="row"><label for="y">Strip title</label></th>
<td><input type="checkbox" id="yankit" name="plugin_image-formatr[yankit]"></td>
</tr>
The problem is the <label for="y">
should be <label for="yankit">
so it’s trying to not have a bug but, it does. Any insight as to why the id reference is truncated?
It is interesting to note that the definition for add_settings_field() in wp-admin/includes/template.php, the last argument is declared as an array and when an array parameter is passed, the label is not printed at all; and, to reiterate, when a string parameter is passed, the label is printed with a truncated id reference.
]]>To use these in my theme, I call something like:
<?php $options = get_option('theme_options'); ?>
<?php echo $options['option_name']; ?>
but I would like to add an if statement to check and see if a particular field has been filled.
I tried the following, but it isn’t working.
<?php if ( get_option ( 'option_name' )) { //do something } ?>
I need to get to the field name somehow… I’ve been searching for a while and there are many tutorials on building a settings page – but not many on using the new options afterward. Any help would be appreciated.
]]>I’ve added a new field in my Settings->general page, following this example:https://codex.www.ads-software.com/Settings_API
The field is added (also in my DB), but any data I input is not stored.
I’m using both add_settings_field
and register_settings
:
add_settings_field('site_logo', 'Logo', 'site_logo_callback_function', 'general', 'default');
register_setting('general','site_logo');
So why is data not stored when I click the save button?
]]>Everything else works correct, i.e. if I manually change value of option in database then checkbox is changed too, but I can’t save after clicking on Permalink Settings page.
Here is code:
//function to add field on Permalink Settings page
//based on https://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
function add_ser_cyr_to_lat_slug_settings_field() {
// The fields are:
//the id the form field will use
//name to display on the page
//callback function
//the name of the page
//the section of the page to add the field to
add_settings_field('ser_cyr_to_lat_slug' , 'Преслов?ава?е подлошка' ,
'ser_cyr_to_lat_slug_field_callback' , 'permalink' , 'optional');
//register the setting to make sure it gets checked
register_setting('permalink','ser_cyr_to_lat_slug', 'ser_cyr_to_lat_slug_validate');
}
//function for printing field on Permalink Settings page
//based on https://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
function ser_cyr_to_lat_slug_field_callback() {
global $ser_cyr_to_lat_slug, $broj;
//print checkox ?>
<label for="ser_cyr_to_lat_slug"><input id="ser_cyr_to_lat_slug" name="ser_cyr_to_lat_slug" type="checkbox" value="1"
<?php checked('1', get_option('ser_cyr_to_lat_slug')); ?>
/> Преслови српска ?ирилична у енглеска латинична слова у пермалинковима</label>
<?php }
// Sanitize and validate input.
//based on https://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/
function ser_cyr_to_lat_slug_validate($input) {
// Our first value is either 0 or 1
$input = ( $input == 1 ? 1 : 0 );
return $input;
}
//add action so that functions could actually work
add_action('admin_init', 'add_ser_cyr_to_lat_slug_settings_field');
As you can see, this script is based on Ozh’s tip. When I first time tried this tip, I just copied his functions and that worked without problem. Later when I renamed functions and variables, this didn’t work.
Here is code that worked:
add_action('admin_init', 'add_my_settings_field');
function add_my_settings_field(){
// The fields are:
//the id the form field will use
//name to display on the page
//callback function
//the name of the page
//the section of the page to add the field to
add_settings_field('my_field_id' , 'My Field Caption' ,
'my_field_callback' , 'writing' , 'default');
//register the setting to make sure it gets checked
register_setting('writing','my_field_id', 'ozh_sampleoptions_validate');
}
$my_field_id = get_option('my_field_id');
$broj = 1;
//if (!$my_field_id) {
function my_field_callback(){
global $my_field_id, $broj;
//echo out the text field, drop down, or other type of field. ?>
<input id="my_field_id" name="my_field_id" type="checkbox" value="1"
<?php checked('1', get_option('my_field_id')); ?>
/>
<?php }
// Sanitize and validate input. Accepts an array, return a sanitized array.
function ozh_sampleoptions_validate($input) {
// Our first value is either 0 or 1
$input = ( $input == 1 ? 1 : 0 );
return $input;
}
I can’t see where I made mistake.
If you could see what is problem, please write it.
Thanks in advance.
I’m amazed with this new add_settings_field thing. That’s awesome!!
But I’m trying to figure out how to pass an argument to the callback function. Actually, I don’t know if it could be done
Eg.
$as = array('A', 'B', 'C', 'D');
$adb = null;
function my_new_settings(){
global $as;
foreach($as as $a):
$adb = sanitize_title($a);
register_setting('media',"{$adb}_1");
register_setting('media',"{$adb}_2");
add_settings_field("{$adb}" , "{$a} setting:" ,
'my_new_callback' , 'media' , 'default');
endforeach;
}
function my_new_callback(){
global $adb;
echo "<label for=\"{$adb}_1\">Setting 1</label>
<input name=\"{$adb}_1\" id=\"{$adb}_1\" value=\"".attribute_escape(get_option("{$adb}_1"))."\" class=\"small-text\" type=\"text\">
<label for=\"{$adb}_2\">Setting 2</label>
<input name=\"{$adb}_2\" id=\"{$adb}_2\" value=\"".attribute_escape(get_option("{$adb}_2"))."\" class=\"small-text\" type=\"text\">
";
}
add_action('admin_init', 'my_new_settings');
Could anyone help here?
Thanks in advance!
n!
PS: I think this is a WP-Advanced Forum question but I can’t post there, does anyone know why it is closed to moderators?
]]>