Settings API in Wordpress - Mini Project: Build & Apply
myplugin_add_admin_menu that uses add_options_page to add a settings page titled "My Plugin Settings" under the Settings menu. Hook this function to admin_menu.Start by writing the plugin header comment. Then create a function named myplugin_add_admin_menu that calls add_options_page with the page title "My Plugin Settings" and menu title "My Plugin". Hook this function to the admin_menu action.
myplugin_settings_init. Inside it, register a setting named myplugin_options using register_setting. Then add a settings section with ID myplugin_section and title "Settings" using add_settings_section. Finally, add a settings field with ID myplugin_welcome_message, label "Welcome Message", and a callback function myplugin_welcome_message_render using add_settings_field. Hook myplugin_settings_init to admin_init.Inside myplugin_settings_init, call register_setting with group 'myplugin' and option name 'myplugin_options'. Then add a section with ID 'myplugin_section' and title "Settings". Finally, add a field with ID 'myplugin_welcome_message', label "Welcome Message", and callback myplugin_welcome_message_render. Hook this function to admin_init.
myplugin_welcome_message_render. Inside it, get the saved options using get_option('myplugin_options'). Then output an HTML input of type text with name myplugin_options[welcome_message] and value set to the saved welcome message or empty string if none.Define myplugin_welcome_message_render to get the saved options with get_option('myplugin_options'). Then output an input box with name myplugin_options[welcome_message] and value set safely using esc_attr.
myplugin_options_page. Inside it, output a form with method post and action set to options.php. Call settings_fields('myplugin'), do_settings_sections('myplugin'), and add a submit button with text "Save Settings". This form will display the settings fields and save the options.Define myplugin_options_page to output a form with action="options.php" and method="post". Inside the form, call settings_fields('myplugin'), do_settings_sections('myplugin'), and add a submit button with submit_button('Save Settings').