Simple yet powerful utility that I use mostly for my client's projects. This project was built over time so I could re-use them over multiple of my projects.
This project was actually built to have clean admin interface with ability to add tabbed settings page.
$admin = new \Sovit\Utilities\Admin_Setting();
$admin->set_page_id("my-setting-page")
->set_capability('manage_options')
->set_page_title(esc_html__('My Setting'))
->set_menu_title(esc_html__('My Setting'))
->set_setting_key("my_setting_name")
->set_icon('dashicons-admin-links');
set_page_id($page_id)
- Unique Page ID (if page id ismy-setting-page
then setting page url would be/wp-admin/admin.php?page=my-setting-page
)set_capability($capability)
- Set capability (eg:manage_options
)set_menu_title($menu_title)
- Admin menu nameset_page_title($page_title)
- Admin setting page titleset_icon($icon)
- (dashicon, icon url) Menu iconset_setting_key($setting_key)
- Setting name keyset_menu_position($menu_position)
- Menu Positionset_menu_parent($menu_parent)
- Set menu parent (eg:options-general.php
if you want setting page as child ofSettings
page)
add_filter("sovit/settings/{$page_id}/tabs",function($tabs){
$tabs["my_tab"]=[
"label"=>__("My Tab","textdomain")
];
return $tabs;
});
Tabs also accept render_callback
if you require custom tab content
add_filter("sovit/settings/{$page_id}/tabs",function($tabs){
$tabs["my_tab"]=[
"label"=>__("My Tab","textdomain"),
"render_callback"=>"my_tab_callback"
];
return $tabs;
});
function my_tab_callback(){
echo "My tab Content";
}
add_filter("sovit/settings/{$page_id}/tabs",function($sections){
$sections["general"]=[
"label"=>__("General Settings","textdomain")
];
return $sections;
});
Sections also accept render_callback
if you require custom section content
add_filter("sovit/settings/{$page_id}/sections",function($sections){
$sections["general_setting"]=[
"label"=>__("General Settings","textdomain"),
"render_callback"=>"my_section_callback"
];
return $sections;
});
function my_section_callback(){
echo "My Section Content";
}
add_filter("sovit/settings/{$page_id}/fields",function($fields){
$fields["fruit"]=[
"label"=>__("Fruit","textdomain"),
'section' => "general_setting",
'tab' => "general",
'type' => 'select',
"options" => [
"apple" => __("Apple", "textdomain"),
],
'desc' => esc_html__('Select a fruit', "textdomain"),
// There are other options based on field types
// see lib/Controls.php
];
return $fields;
});
Fields also accept render_callback
if you require custom field content
add_filter("sovit/settings/{$page_id}/fields",function($sections){
$fields["fruit"]=[
"label"=>__("Fruit","textdomain"),
"render_callback"=>"my_custom_field",
// .. other field data
];
return $fields;
});
function my_custom_field($field=array()){
echo "<pre>".print_r($field,true)."</pre>"; // print field data
}
add_filter("sovit/settings/{$page_id}/fields",function($sections){
$fields["age"]=[
"label"=>__("Age","textdomain"),
"render_callback"=>"my_custom_field",
"validate_callback"=>"my_field_validation",
"sanitize_callback"=>"my_field_sanitization",
// other field options
];
return $fields;
});
function validate_callback($value){
return is_int($value) && $value>5; // return true if age is integer and greater than 5
}
function sanitize_callback($value){
return sanitize_text_field($value);
}
This project is work in progress but is usable as is. There are more features, but not yet documented.
If you want to contribute, create pull request.