jeremyhixon / rationaloptionpages Goto Github PK
View Code? Open in Web Editor NEWPHP Class for building Option Pages in WordPress
License: MIT License
PHP Class for building Option Pages in WordPress
License: MIT License
Hi, is there a way to add class to <tr>
element?
I added 'class' => 'my_custom_class'
under a single field array but it assign the class to the input element.
Thanks
Fosco
Hi,
I just had a nightmare dealing with localization, it turns out that even with :
php 'title' => __( 'Activate E-mail modification', 'text-domain' ),
the title is not translated.
Is that normal or i am doing something wrong ?
Thank you
https://github.com/jeremyHixon/RationalOptionPages/blob/master/RationalOptionPages.php#L401-L417
As it stands, when I pass a field like this:
'post-types' => array(
'title' => __( 'Post Types', 'plugin-name' ),
'type' => 'checkbox',
'choices' => $post_types_choices,
),
),
... the $post_type_choices don't get rendered. Instead, I just get a single checkbox that will resolve the field to "on" rather than giving each choice it's own toggle. I'd expect it to work like this instead.
Thanks for providing this library :)
There seams to be a type mismatch if you define numeric values for your choice values.
PHP transfers them into int but in the values from get_option
are always strings.
Example Configuration:
'sample-page' => array(
'page_title' => __('Sample Page', 'sample-domain'),
'sections' => array(
'sample-section' => array(
'title' => __('Section One', 'sample-domain'),
'fields' => array(
'select' => array(
'title' => __('Select', 'sample-domain'),
'type' => 'select',
'value' => 'option-two',
'choices' => array(
'0' => __('Nothing selected', 'sample-domain'),
'1' => __('Not working', 'sample-domain'),
'x' => __('Working', 'sample-domain'),
),
),
)
)
),
),
);
Try to select the "Not working" choice.
PHP Array:
var_dump(array(
'0' => __('Nothing selected', 'sample-domain'),
'1' => __('Not working', 'sample-domain'),
'x' => __('Working', 'sample-domain'),
));
array(3) {
--
| [0]=>
| string(16) "Nothing selected"
| [1]=>
| string(11) "Not working"
| ["x"]=>
| string(7) "Working"
| }
get_option
var_dump(get_option('sample-page', array() ));
array(1) {
--
| ["select"]=>
| string(1) "1"
| }
My use case is to save have a dropdown for all categories, I would like to save just the id of the selected category, but at the moment I have to add a string prefix to make it work
Hi, if I use 'type' => 'wp_editor'
it works correctly but when I save, it lose all text formats (bold, italic etc..) and the page reloads without keeping text format.
How can I solve it?
And then, when I use it, is it enough to echo it?
Thanks
Cols, Rows, and Enter Doesn't Work in Textarea, please fix it jeremy :(
wp editor does not work if it is called twice in a different section
'wp_editor_footer' => array(
'title' => __('WP Editor', 'sample-domain'),
'type' => 'wp_editor',
'value' => 'Pellentesque consectetur volutpat lectus, ac molestie lorem molestie nec. ',
),
Hello,
I'm having trouble to figure out how to use the callback. I already created a function, set 'custom' to true and set the 'callback' to the functions name. But, instead of running the function, the function's name is displayed as a notice message (below the page title).
This is the part of the code:
function awpha_bookmarks_test() {
echo 'It works!';
}
$awpha_bookmarks_settings_page = array(
'awpha_bookmarks_settings' => array(
'parent_slug' => 'edit.php?post_type=awpha_bookmarks',
'page_title' => __( 'Settings', 'awpha_bookmarks' ),
'menu_slug ' => 'awpha_bookmarks_settings',
'sections' => array(
//Setup
'section-2' => array(
'title' => __( 'Submit bookmark URL', 'awpha_bookmarks' ),
'fields' => array(
'frontend_url' => array(
'id' => 'frontend_url',
'title' => __( 'Frontend URL', 'awpha_bookmarks' ),
'custom' => true,
'callback' => 'awpha_bookmarks_test',
),
),
),
...extra code...
I love and prefer the layout of this code, I want to use the samples you have provided but I have no idea how to call it.
I have created a menu item, and I have created a page but I have no idea how to use your RationalOptionsPages.php to output my array that I have made to show my options in the admin.
I really like this! I'm getting started with WordPress plugin development, and this has been really helpful. My issue may be unique and not a typical use case, but here is what I'm trying to accomplish:
You can see that above I'm trying to create the initial page for my plugin. That page has all of my sections, fields, etc., set on it. I added a sub-page to the plugin, and it automatically made a sub-page above it that has the same name as the menu:
What I would like to do is rename the first sub-page to be called "Settings." I've seen a lot of plugins do it this way, and I wanted to mimic that. The slug between "Settings" and "Plugin Menu Name" should be the same.
Is this possible?."
How do I add the options page underneath the WordPress "Settings" instead of in the main menu?
When using wp_editor field, no data is saved and empty wp_editor is returned on page refresh.
Well this happens if we omit value attribute in the wp_editor array.
The solution I found was to include array value data as 'value' => '',
Can you update the code so that even if we don't include the value attribute, it get's automatically adds an empty value?
I add the 'parent_slug' option to the page array with the value 'tools' in the hope that it'll appear in the tools menu, however my page doesn't appear in Tools, but also doesn't appear anywhere else. Not sure what I'm doing wrong.
$pages = array(
'footer-text' => array(
'page_title' => __( 'Footer Text', 'footer-domain' ),
'parent_slug' => 'tools',
'icon_url' => 'dashicons-feedback',
'sections' => array(
'section-one' => array(
'title' => __( 'Edit Footer Content', 'footer-domain' ),
'fields' => array(
'footer-left' => array(
'title' => __( 'Left Content', 'footer-domain' ),
'type' => 'wp_editor',
'attributes' => Array('rows'=>10),
),
'footer-right' => array(
'title' => __( 'Right Content', 'footer-domain' ),
'type' => 'wp_editor',
'attributes' => Array('rows'=>10),
),
),
),
),
),
);
I tried to create a select field that allows the user to choose multiple entries.
The Input will not be saved and no option will be selected by default.
//...
'fields' => array(
'test_field' => array(
'id' => 'test_field',
'title' => 'Test Field',
'type' => 'select',
'choices' => array( 'a', 'b', 'c' ),
'attributes' => array(
'multiple' => true,
),
),
//...
Everything works fine without 'multiple' => true
.
Am I missing something here or is it a bug?
Hi,
I am trying to reproduce your demo code in the readme. I put it in the functions.php as it is without any modifications. It created the page with all fields. It can also save and put the values in the database. But it does not show a "settings saved notice". What could be the reason? Am I missing anything?
Thanks.
That should be the fix for it.
Line 491
printf(
'<textarea %s id="%s" name="%s" %s %s %s %s title="%s">%s</textarea>%s', <-----------
!empty( $field['class'] ) ? "class='{$field['class']}'" : '',
$field['id'],
"{$page_key}[{$field['id']}]",
!empty( $field['placeholder'] ) ? "placeholder='{$field['placeholder']}'" : '',
!empty( $field['rows'] ) ? "rows='{$field['rows']}'" : '',
!empty( $field['cols'] ) ? "cols='{$field['cols']}'" : '', // cols <-----------
!empty( $field['wrap'] ) ? "wrap='{$field['wrap']}'" : '', // wrap <-----------
$field['title_attr'],
$field['value'],
!empty( $field['text'] ) ? "<p class='help'>{$field['text']}</p>" : ''
);
break;```
I am trying to have users enter some HTML in two textarea blocks and when saving the HTML is being stripped out. How can I keep it in there?
Also...I can't seem to figure out how to set the cols and rows for the textarea. Would you happen to have an example?
Hey, I know I could programitacally add this myself. But I have a request to add an Media Liberary selection option as well, for future uses.
Hi,
is there a way to add a "select media" button to select a media from wordpress library and store the media url in the $options array?
Thanks
Hi there!
How to make certain field "required" so user can't submit before fill the required fields?
Hi!
Thank you for this useful tool.
Just wondering about an issue with non-latin characters using in page_title or field title.
Something connected to
iconv(): Detected an illegal character in input string in RationalOptionPages.php on line 621
How can i fix it or bypass if i want to use non-latin, cyrillic for example?
Thanks!
How to do it?
Hi @jeremyHixon
I am pretty new to wordpress plugins and this tool just amazes the way it can create menus.
I wanted to create a page that is right above 'Appearance Menu'. I was using the Page Generator tool and the result for 2 different setting are same. The settings are as following
Setting1
Menu Option: Position-> Separator (Below Comment)
Setting2
Menu Option: Position-> Appearance
My question is isn't the setting1 supposed to display the new menu above the appearance tab?
I compare the codes for both settings and the difference is shown the image below
https://i.imgur.com/U3N1vR4.png
Hi,
Thanks for providing this PHP Class. I find this easier to work with for creating fields on a settings page in a plugin.
If we take this sample code,
require_once('RationalOptionPages.php');
$pages = array(
'sample-page' => array(
'page_title' => __( 'Sample Page', 'sample-domain' ),
),
);
$option_page = new RationalOptionPages( $pages );
how can I delete the option named sample-page
from the database when the plugin is uninstalled/deleted?
function your_prefix_activate() {
register_uninstall_hook( __FILE__, 'your_prefix_uninstall' );
}
// And here goes the uninstallation function:
function your_prefix_uninstall() {
delete_post_meta_by_key( 'sample-page' );
}
is my current attempt but it doesn't seem to be working.
Any help is appreciated.
When using parent_slug => options-general.php to get the options page to show up under the standard Settings menu, the library will cause the "Settings Saved" message to be shown twice when saving the form.
If using parent_slug => tools.php it works as expected and only shows the "Settings saved" message once.
Notice: Undefined variable: tel
// Get all options for the page
$options = get_option( 'sample-page', array() );
// Each field id is a key in the options array
$date = $options['date'];
$tel = $options['telephone'];
echo $tel;
Is there a way to display the value of the range slider so the user knows what they have selected in the option panel
I have an options subpage setup under a custom post type page.
Changing 'capability'
to 'editor'
allows access to the page for Editors, but it doesn't allow Editors to save changes. Editors get the "Cheatin' uh?" message when attempting to save.
I've also tried using a custom capability in place of the role name. Same deal.
Is this a limitation of WP?
Might I be missing a setting in the array?
Here's what I've got:
$signersOptions = array(
'signers-page' => array(
'parent_slug' => 'edit.php?post_type=cpt_signers',
'menu_title' => 'Options',
'page_title' => 'Signers',
'capability' => 'editor',
'sections' => array(
'signers_section1' => array(
'title' => 'Options',
'fields' => array(
'schools' => array(
'title' => 'Schools to include',
'id' => 'included_schools',
'type' => 'textarea',
)
)
)
)
)
);
I've tried everything to get the value of the settings to display. I want to display the value on the front end via a page template, but nothing I'm doing works. Maybe I'm doing something wrong, can someone show me how they got theirs to work?
Is there a way to pass parameters to wp_editor in the $pages array, for instance, to remove media buttons? I tried a few different things, but haven't had much luck. I also tried changing line 94 to:
'media_buttons' => false,
but the button is still there.
First of all - amazing work! Thank you so much!
Just working through a situation where I have a multi-select and I am unable to set default selected options. I've used it in the same way as you would on a standard select box and the settings screen loads fine, but just doesn't select anything.
So, I've fixed the issue and it turns out the default wasn't being set for a standard selects either. Here's the fix I implemented and I hope it's something you can test and add to your codebase?
//RationalOptionPages.php Line 475
// we need to add an extra 'else' statement inside the foreach loop for when the select DOESN'T have any options set - i.e it's not been updated yet. Whole revised foreach loop code below. Also removed the unnecessary '$selected' variable on Line 476
foreach ( $field['choices'] as $value => $text ) {
if ( isset( $this->options[ $field['id'] ] ) ) {
if (!is_array($this->options[ $field['id'] ] ) ) {
$selected = $value === $this->options[ $field['id'] ] ? 'selected="selected"' : '';
} else
{
$selected = in_array( $value, $this->options[ $field['id'] ] ) ? 'selected="selected"' : '';
}
} else
{
if(is_array($field['value']))
{
$selected = (in_array($value, $field['value'])) ? 'selected="selected"' : '';
} else
{
$selected = $value === $field['value'] ? 'selected="selected"' : '';
}
}
printf('<option %s value="%s">%s</option>',
$selected, // selected
$value, // value
__($text,'text-domain') // text
);
}
// end
Hi,
Is it possible to load a settings field from an other plugin.
I like to "copy" the Google Analytics Tracking ID from the Perfmatters plugin into a own options page.
What would the sample code look like if I wanted to put my settings menu item as an item under the "Settings" main menu?
Thanks for a brilliant library!
Is there a particular reason why the select type doesn't support the text attribute?
I quick and dirty hack to make it work (but will probably fail any html validation):
@@ -472,6 +472,7 @@ class RationalOptionPages {
$field_tag_name, // name
__($field['title_attr'],'text-domain') // title
);
+ $myLab = "";
foreach ( $field['choices'] as $value => $text ) {
$selected = $value === $field['value'] ? 'selected' : '';
if ( isset( $this->options[ $field['id'] ] ) ) {
@@ -488,8 +489,10 @@ class RationalOptionPages {
$value, // value
__($text,'text-domain') // text
);
+ if (!empty( $field['text'] ) ) { $myLab='<p class="help">'.__($field['text'],'text-domain').'</p>'; }
}
echo '</select>';
+ if (!empty($myLab) ) {echo $myLab;}
break;
case 'textarea':
printf(
This can produces an output like this with the text attribute places underneath the select box:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.