|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Custom Settings example by using WP Settings API Wrapper. |
| 4 | + * |
| 5 | + * @package wp-settings-api-wrapper |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * An example of adding a custom settings page. |
| 10 | + * |
| 11 | + * @return void |
| 12 | + */ |
| 13 | +function wp_register_custom_settings() { |
| 14 | + |
| 15 | + $custom_settings = new WP_Custom_Settings( |
| 16 | + // Arguments to add menu page. |
| 17 | + [ |
| 18 | + 'page_title' => __( 'Custom Settings', 'wp-custom-settings' ), |
| 19 | + 'menu_title' => __( 'Custom Settings', 'wp-custom-settings' ), |
| 20 | + 'capability' => 'manage_options', |
| 21 | + 'menu_slug' => 'wp-custom-settings-page', |
| 22 | + 'icon_url' => '', |
| 23 | + 'position' => null, |
| 24 | + ], |
| 25 | + // Arguments to register setting. |
| 26 | + [ |
| 27 | + 'option_group' => 'wp_custom_settings_group', |
| 28 | + 'option_name' => 'wp_custom_settings_options', |
| 29 | + 'args' => array( |
| 30 | + 'type' => 'array', |
| 31 | + 'description' => 'Description of Custom Settings.', |
| 32 | + 'show_in_rest' => true, |
| 33 | + 'default' => array(), |
| 34 | + 'sanitize_callback' => null, |
| 35 | + ), |
| 36 | + ], |
| 37 | + // Arguments to add sections and fields. |
| 38 | + [ |
| 39 | + new WP_Custom_Settings_Section( |
| 40 | + 'wp_custom_settings_section', |
| 41 | + __( 'Section Title.', 'wp-custom-settings' ), |
| 42 | + __( 'Section Description.', 'wp-custom-settings' ), |
| 43 | + [ |
| 44 | + new WP_Custom_Settings_Field( |
| 45 | + 'text', |
| 46 | + 'wp_custom_settings_field', |
| 47 | + __( 'Field Title', 'wp-settings-api-wrapper' ), |
| 48 | + [ |
| 49 | + 'description' => 'Description of Custom Settings.', |
| 50 | + 'label_for' => 'wp_custom_settings_field', |
| 51 | + 'class' => 'regular-text', |
| 52 | + ] |
| 53 | + ), |
| 54 | + ] |
| 55 | + ), |
| 56 | + new WP_Custom_Settings_Section( |
| 57 | + 'wp_custom_settings_section_1', |
| 58 | + __( 'Section Title 1.', 'wp-custom-settings' ), |
| 59 | + __( 'Section Description 1.', 'wp-custom-settings' ), |
| 60 | + [ |
| 61 | + new WP_Custom_Settings_Field( |
| 62 | + 'textarea', |
| 63 | + 'wp_custom_settings_field_1', |
| 64 | + __( 'Field Title 1', 'wp-custom-settings' ), |
| 65 | + [ |
| 66 | + 'description' => 'Description of Custom Settings Field 1.', |
| 67 | + 'label_for' => 'wp_custom_settings_field_2', |
| 68 | + 'class' => 'large-text', |
| 69 | + ] |
| 70 | + ), |
| 71 | + ] |
| 72 | + ), |
| 73 | + ] |
| 74 | + ); |
| 75 | + |
| 76 | +} |
| 77 | +add_action( 'init', 'wp_register_custom_settings' ); |
0 commit comments