|
| 1 | +```php |
| 2 | +<?php // functions.php file. |
| 3 | + |
| 4 | +use TheWebSolver\Core\Setting\Component\Container; |
| 5 | + |
| 6 | +/** |
| 7 | + * A global container for setting page. |
| 8 | + * |
| 9 | + * var $container \TheWebSolver\Core\Setting\Component\Container |
| 10 | + */ |
| 11 | +$container = new Container( 'testFields', HZFEX_SETTING_MENU ); |
| 12 | + |
| 13 | +/** |
| 14 | + * Initializes admin page. |
| 15 | + */ |
| 16 | +function add_admin_page() { |
| 17 | + global $container; |
| 18 | + |
| 19 | + $container->set_page( |
| 20 | + array( |
| 21 | + 'page_title' => __( 'Plugin Options', 'tws-setting' ), |
| 22 | + 'menu_title' => __( 'Plugin Options', 'tws-setting' ), |
| 23 | + 'position' => 99, |
| 24 | + ), |
| 25 | + ) |
| 26 | + ->set_capability( 'manage_options' ) |
| 27 | + ->set_menu(); |
| 28 | +} |
| 29 | +add_action( 'after_setup_theme', 'add_admin_page' ); |
| 30 | + |
| 31 | +/** |
| 32 | + * Creates sections and fields inside setting container. |
| 33 | + * |
| 34 | + * Creates 3 sections and fields inside respective sections as shown in screenshot in README file. |
| 35 | + */ |
| 36 | +function add_section_fields() { |
| 37 | + global $container; |
| 38 | + |
| 39 | + $container->add_section( // Create General fields section. |
| 40 | + 'simpleField', |
| 41 | + array( |
| 42 | + 'title' => __( 'General Setting Section', 'tws-core' ), |
| 43 | + 'tab_title' => __( 'General', 'tws-core' ), |
| 44 | + 'desc' => 'This section demonstrates general setting fields.', |
| 45 | + ) |
| 46 | + ) |
| 47 | + ->add_field( |
| 48 | + 'text_field', |
| 49 | + 'simpleField', |
| 50 | + array( |
| 51 | + 'label' => __( 'Text Field', 'tws-core' ), |
| 52 | + 'desc' => __( 'Description for a simple text field', 'tws-core' ), |
| 53 | + 'type' => 'text', |
| 54 | + 'placeholder' => __( 'Placeholder text', 'tws-core' ), |
| 55 | + 'default' => 'Text default', |
| 56 | + 'class' => 'widefat', // WP default for 100% width. |
| 57 | + 'sanitize_callback' => 'sanitize_text_field', // IMPORTANT !!! |
| 58 | + ) |
| 59 | + ) |
| 60 | + ->add_field( |
| 61 | + 'number_field', |
| 62 | + 'simpleField', |
| 63 | + array( |
| 64 | + 'label' => __( 'Number Input Field', 'tws-core' ), |
| 65 | + 'desc' => __( 'Number field with [min | max | step] options.', 'tws-core' ), |
| 66 | + 'type' => 'number', |
| 67 | + 'placeholder' => __( '0.5', 'tws-core' ), |
| 68 | + 'default' => '0.7', |
| 69 | + 'class' => 'widefat', // WP default for 100% width. |
| 70 | + 'sanitize_callback' => 'floatval', // IMPORTANT !!! |
| 71 | + 'min' => 0, |
| 72 | + 'max' => 1, |
| 73 | + 'step' => 0.1, |
| 74 | + ) |
| 75 | + ) |
| 76 | + ->add_field( |
| 77 | + 'checkbox_field', |
| 78 | + 'simpleField', |
| 79 | + array( |
| 80 | + 'label' => __( 'Checkbox field', 'tws-core' ), |
| 81 | + 'desc' => __( 'Check/Uncheck this field. Value is saved as "on" or "off"', 'tws-core' ), |
| 82 | + 'type' => 'checkbox', |
| 83 | + 'default' => 'off', |
| 84 | + 'class' => '', |
| 85 | + 'sanitize_callback' => 'sanitize_key', // IMPORTANT !!! |
| 86 | + ) |
| 87 | + ) |
| 88 | + ->add_field( |
| 89 | + 'radio_field', |
| 90 | + 'simpleField', |
| 91 | + array( |
| 92 | + 'label' => __( 'Radio button', 'tws-core' ), |
| 93 | + 'desc' => __( 'Select one of the radio button', 'tws-core' ), |
| 94 | + 'type' => 'radio', |
| 95 | + 'default' => 'radio_three', |
| 96 | + 'class' => '', |
| 97 | + 'options' => array( |
| 98 | + 'radio_one' => __( 'Radio One', 'tws-core' ), |
| 99 | + 'radio_two' => __( 'Radio Two', 'tws-core' ), |
| 100 | + 'radio_three' => __( 'Radio Three', 'tws-core' ), |
| 101 | + ), |
| 102 | + ) |
| 103 | + ) |
| 104 | + ->add_field( |
| 105 | + 'select_field', |
| 106 | + 'simpleField', |
| 107 | + array( |
| 108 | + 'label' => __( 'Select dropdown field', 'tws-core' ), |
| 109 | + 'desc' => __( 'Select any option from dropdown', 'tws-core' ), |
| 110 | + 'type' => 'select', |
| 111 | + 'default' => 'select_two', |
| 112 | + 'class' => 'widefat', |
| 113 | + 'options' => array( |
| 114 | + 'select_one' => __( 'Select One', 'tws-core' ), |
| 115 | + 'select_two' => __( 'Select Two', 'tws-core' ), |
| 116 | + 'select_three' => __( 'Select Three', 'tws-core' ), |
| 117 | + ), |
| 118 | + ) |
| 119 | + ) |
| 120 | + ->add_field( |
| 121 | + 'textarea_field_id', |
| 122 | + 'simpleField', |
| 123 | + array( |
| 124 | + 'label' => __( 'Text-area Field', 'tws-core' ), |
| 125 | + 'desc' => __( 'Description for a simple text-area field with placeholder', 'tws-core' ), |
| 126 | + 'type' => 'textarea', |
| 127 | + 'class' => '', |
| 128 | + 'placeholder' => 'Placeholder text for text-area field....', |
| 129 | + 'default' => '', |
| 130 | + 'sanitize_callback' => 'sanitize_textarea_field', |
| 131 | + 'rows' => '8', |
| 132 | + ) |
| 133 | + ) |
| 134 | + ->add_section( // Create Advanced fields section. |
| 135 | + 'advancedFields', |
| 136 | + array( |
| 137 | + 'title' => __( 'Advanced Setting Section', 'tws-core' ), |
| 138 | + 'tab_title' => __( 'Advanced', 'tws-core' ), |
| 139 | + 'desc' => sprintf( '<p>%1$s</p>', __( 'This section demonstrates advanced setting fields', 'tws-core' ) ), |
| 140 | + ) |
| 141 | + ) |
| 142 | + ->add_field( |
| 143 | + 'multi_checkbox', |
| 144 | + 'advancedFields', |
| 145 | + array( |
| 146 | + 'label' => __( 'Multi-checkbox Field', 'tws-core' ), |
| 147 | + 'desc' => __( 'A multi-checkbox field where multiple options can be selected.', 'tws-core' ), |
| 148 | + 'type' => 'multi_checkbox', |
| 149 | + 'class' => '', |
| 150 | + 'default' => array( 'checkbox_two' ), // default options value in an array |
| 151 | + 'options' => array( |
| 152 | + 'checkbox_one' => __( 'Checkbox One', 'tws-core' ), |
| 153 | + 'checkbox_two' => __( 'Checkbox Two', 'tws-core' ), |
| 154 | + 'checkbox_three' => __( 'Checkbox Three', 'tws-core' ), |
| 155 | + ), |
| 156 | + ) |
| 157 | + ) |
| 158 | + ->add_field( |
| 159 | + 'multi_select', |
| 160 | + 'advancedFields', |
| 161 | + array( |
| 162 | + 'label' => __( 'Multi-select Field', 'tws-core' ), |
| 163 | + 'desc' => __( 'A multi-select field where multiple options can be selected. Ctrl + click to select multiple.', 'tws-core' ), |
| 164 | + 'type' => 'multi_select', |
| 165 | + 'class' => 'widefat', |
| 166 | + 'default' => array( 'select_one', 'select_three' ), // default option value in array |
| 167 | + 'options' => array( |
| 168 | + 'select_one' => __( 'Select One', 'tws-core' ), |
| 169 | + 'select_two' => __( 'Select Two', 'tws-core' ), |
| 170 | + 'select_three' => __( 'Select Three', 'tws-core' ), |
| 171 | + ), |
| 172 | + ) |
| 173 | + ) |
| 174 | + ->add_field( |
| 175 | + 'wysiwyg', |
| 176 | + 'advancedFields', |
| 177 | + array( |
| 178 | + 'label' => __( 'Wysiwyg field', 'tws-core' ), |
| 179 | + 'desc' => __( 'Wysiwyg field for advanced field editing.', 'tws-core' ), |
| 180 | + 'type' => 'wysiwyg', |
| 181 | + 'class' => 'widefat', |
| 182 | + 'default' => __( 'Default text for wysiwyg field', 'tws-core' ), |
| 183 | + 'sanitize_callback' => 'wp_kses_post', // IMPORTANT !!! |
| 184 | + ) |
| 185 | + ) |
| 186 | + ->add_field( |
| 187 | + 'file', |
| 188 | + 'advancedFields', |
| 189 | + array( |
| 190 | + 'label' => __( 'File Field', 'tws-core' ), |
| 191 | + 'desc' => __( 'This is a file field to select file uploaded.', 'tws-core' ), |
| 192 | + 'type' => 'file', |
| 193 | + 'class' => 'hz_file_control', |
| 194 | + ) |
| 195 | + ) |
| 196 | + ->add_field( |
| 197 | + 'color', |
| 198 | + 'advancedFields', |
| 199 | + array( |
| 200 | + 'label' => __( 'Color Picker', 'tws-core' ), |
| 201 | + 'desc' => __( 'A color picker field to select color', 'tws-core' ), |
| 202 | + 'type' => 'color', |
| 203 | + 'class' => '', |
| 204 | + ) |
| 205 | + ) |
| 206 | + ->add_field( |
| 207 | + 'password', |
| 208 | + 'advancedFields', |
| 209 | + array( |
| 210 | + 'label' => __( 'Password Field', 'tws-core' ), |
| 211 | + 'desc' => __( 'A password input field', 'tws-core' ), |
| 212 | + 'type' => 'password', |
| 213 | + 'class' => 'hz_password_control', |
| 214 | + ) |
| 215 | + ) |
| 216 | + ->add_section( // Create stylized fields section. |
| 217 | + 'stylizedFields', |
| 218 | + array( |
| 219 | + 'title' => 'Customized Form Fields', |
| 220 | + 'tab_title' => __( 'Stylized Fields', 'tws-core' ), |
| 221 | + 'desc' => __( 'Fields that have class applied to change the appearance', 'tws-core' ), |
| 222 | + ) |
| 223 | + ) |
| 224 | + ->add_field( |
| 225 | + 'checkbox_field_id', |
| 226 | + 'stylizedFields', |
| 227 | + array( |
| 228 | + 'label' => __( 'Checkbox field', 'tws-core' ), |
| 229 | + 'desc' => __( 'Check/Uncheck this field. Value is saved as "on" or "off"', 'tws-core' ), |
| 230 | + 'type' => 'checkbox', |
| 231 | + 'default' => 'on', |
| 232 | + 'class' => 'hz_switcher_control', // makes checkbox switch |
| 233 | + 'sanitize_callback' => 'sanitize_key', // IMPORTANT !!! |
| 234 | + ) |
| 235 | + ) |
| 236 | + ->add_field( |
| 237 | + 'radio_field_id', |
| 238 | + 'stylizedFields', |
| 239 | + array( |
| 240 | + 'label' => __( 'Radio button', 'tws-core' ), |
| 241 | + 'desc' => __( 'Select one of the radio button', 'tws-core' ), |
| 242 | + 'type' => 'radio', |
| 243 | + 'default' => 'radio_two', |
| 244 | + 'class' => 'hz_card_control', // makes radio card |
| 245 | + 'options' => array( |
| 246 | + 'radio_one' => __( 'Radio One', 'tws-core' ), |
| 247 | + 'radio_two' => __( 'Radio Two', 'tws-core' ), |
| 248 | + 'radio_three' => __( 'Radio Three', 'tws-core' ), |
| 249 | + ), |
| 250 | + ) |
| 251 | + ) |
| 252 | + ->add_field( |
| 253 | + 'select_field_id', |
| 254 | + 'stylizedFields', |
| 255 | + array( |
| 256 | + 'label' => __( 'Select dropdown field', 'tws-core' ), |
| 257 | + 'desc' => __( 'Select any option from dropdown', 'tws-core' ), |
| 258 | + 'type' => 'select', |
| 259 | + 'default' => 'select_three', |
| 260 | + 'class' => 'widefat hz_select_control', // adds select2 |
| 261 | + 'options' => array( |
| 262 | + 'select_one' => __( 'Select One', 'tws-core' ), |
| 263 | + 'select_two' => __( 'Select Two', 'tws-core' ), |
| 264 | + 'select_three' => __( 'Select Three', 'tws-core' ), |
| 265 | + ), |
| 266 | + ) |
| 267 | + ) |
| 268 | + ->add_field( |
| 269 | + 'multi_checkbox_id', |
| 270 | + 'stylizedFields', |
| 271 | + array( |
| 272 | + 'label' => __( 'Multi-checkbox Field', 'tws-core' ), |
| 273 | + 'desc' => __( 'A multi-checkbox field where multiple options can be selected.', 'tws-core' ), |
| 274 | + 'type' => 'multi_checkbox', |
| 275 | + 'class' => 'hz_switcher_control', // same as checkbox |
| 276 | + 'default' => array( 'checkbox_three' ), // default options value in an array |
| 277 | + 'options' => array( |
| 278 | + 'checkbox_one' => __( 'Checkbox One', 'tws-core' ), |
| 279 | + 'checkbox_two' => __( 'Checkbox Two', 'tws-core' ), |
| 280 | + 'checkbox_three' => __( 'Checkbox Three', 'tws-core' ), |
| 281 | + ), |
| 282 | + ) |
| 283 | + ) |
| 284 | + ->add_field( |
| 285 | + 'multi_select_id', |
| 286 | + 'stylizedFields', |
| 287 | + array( |
| 288 | + 'label' => __( 'Multi-select Field', 'tws-core' ), |
| 289 | + 'desc' => __( 'A multi-select field where multiple options can be selected. Ctrl + click to select multiple.', 'tws-core' ), |
| 290 | + 'type' => 'multi_select', |
| 291 | + 'class' => 'widefat hz_select_control', // same as select |
| 292 | + 'default' => array( 'select_two' ), // default option value in array |
| 293 | + 'options' => array( |
| 294 | + 'select_one' => __( 'Select One', 'tws-core' ), |
| 295 | + 'select_two' => __( 'Select Two', 'tws-core' ), |
| 296 | + 'select_three' => __( 'Select Three', 'tws-core' ), |
| 297 | + ), |
| 298 | + ) |
| 299 | + ) |
| 300 | + ->add_section( // Callback section for displaying only the contents (no fields but can be added). |
| 301 | + 'callbackOnly', |
| 302 | + array( |
| 303 | + 'title' => 'Content Only Section', |
| 304 | + 'tab_title' => __( 'Content Only', 'tws-core' ), |
| 305 | + 'callback' => function() { |
| 306 | + echo 'This is a callback content. You can display anything with the callback feature.'; |
| 307 | + } |
| 308 | + ) |
| 309 | + ); |
| 310 | +} |
| 311 | +add_action( 'admin_init', 'add_section_fields' ); |
| 312 | +``` |
| 313 | +Fields within each section are saved an an array values with section id. |
| 314 | + |
| 315 | +To get the saved values of above created sections and fields, use: |
| 316 | + |
| 317 | +```php |
| 318 | +<?php // functions.php file. |
| 319 | + |
| 320 | +// General section and text field inside it. |
| 321 | +$general = get_option( 'simpleField', array() ); |
| 322 | +$text_field = $general['text_field']; |
| 323 | + |
| 324 | +// Stylized section and multi checkbox field inside it. |
| 325 | +$stylized = get_option( 'stylizedFields', array() ); |
| 326 | +$multi_checkbox = $stylized['multi_checkbox_id']; |
| 327 | + |
| 328 | +// Alternatively, using framework method. |
| 329 | +use TheWebSolver\Core\Setting\Component\Options; |
| 330 | + |
| 331 | +$text_field = Options::get_option( 'text_field', 'simpleField', '' ); |
| 332 | +$multi_checkbox = Options::get_option( 'multi_checkbox_id', 'stylizedFields', array() ); |
| 333 | +``` |
0 commit comments