@CLE_Ren got any templates/examples to follow?
— Ben Weiser (@benweiser) March 19, 2016
After Ben tweeted his disdain for the WordPress Settings API, a common pain in the ass for WordPress developers, I shared how I typically handle custom settings pages. This post shows a basic template of my process, which involves creating a custom form, then processing the input myself. In my opinion, this method is much simpler than the Settings API, and, also, more flexible.
Take a look, and I’ll explain below.
First off, this code sets up a menu page in the WordPress admin. It is a top-level page, which means it gets its own top-level menu link, as opposed to a submenu page, which falls under a top-level menu item.
myplugin_render_settings_page() function renders the markup for the settings page. In an actual plugin, I’d pull out this markup into a separate template file, but it’s fine here for the sake of convenience.
The markup I used was obtained thanks to the WP Admin Style plugin. If you’re a WordPress developer, you should be using this plugin!
myplugin_process_settings() function runs some security and privilege checks, then processes the form’s input. This sample settings page submits input that will be used to add only one option to the database. If you’re using multiple settings, it is recommended that you save them all in an array, then save the array, rather than saving each setting to a separate row.
If you have any questions about the code, feel free to post them below.