Option Page Helper is just a class aimed to help developer create option pages easily in code. The class wrappes all function calls to create a option page. It stores the variables that are used multiple times for cleaner code and it auto-generate callbacks to minimize the development time.
Still work in progress...
I built this for myself but see a greater use for it. If it doesn't suite your needs right out of the box, please let me know and I might add in more features. If you feel like helping out, feel free to do so on
github
I thought I was a bit complicated to add option pages so I wrote a helper that simplify thing a bit. The helper stores recurring variables like page slug so you don't have to pass it with all function calls. It created callback automatically based on the section or field name, so you don't have to care about that eigher. This is a simple example on how to create a options page
`
$options = array(
'page_title' => 'Settings Admin', // Page title
'menu_title' => 'My Settings', // Menu title
'capability' => 'manage_options', // Capability
'menu_slug' => 'my-settings', // Menu slug
'callback' => array($this, 'page'), // Callback object
'form' => array(
array(
'name' => 'First Section', // Section name
'fields' => array(
'first_option' => 'First option' // Field id and name
)
)
),
);
$this->optionPageHelper = new OptionPageHelper($options);
`
You're page is created, but there are some callbacks to take care of.
`
/**
- Options page callback.
- This is where you add everything you want to the page.
- The form with all your settings is generated automatically
- when renderForm() is called.
*/
public function page()
{
$output = '
My Option Page
' . $this->optionPageHelper->renderForm() . '
';
echo $output;
}
And then we have the field(s) callback...
/**
- Callback for an option field.
- The field callback will be generated automatically based on the id of your field.
- If your field id is first_option, the callback will be firstOptionFieldCallback.
- The option key for your setting will also be generated automatically.
- It will be prefixed with the menu_slug in your options array.
- first_option will be saved as my-settings_first_option.
*/
public function firstOptionFieldCallback()
{
// Get saved field values
$option = get_option('my-settings_first_option');
// Build element and print
echo '';
}
`
Don't forget to read the code comments here, they're useful :)
The Utils class contains some formatting helpers, so you need that file to. The OptionPage class is just an example on how to implement this, just like this readme.