1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
// Theme Option if ( ! defined( 'ABSPATH' ) ) { exit; } // Start Class if ( ! class_exists( 'BWT_Theme_Options' ) ) { class BWT_Theme_Options { /** * Start things up * * @since 1.0.0 */ public function __construct() { // We only need to register the admin panel on the back-end if ( is_admin() ) { add_action( 'admin_menu', array( 'BWT_Theme_Options', 'add_admin_menu' ) ); add_action( 'admin_init', array( 'BWT_Theme_Options', 'register_settings' ) ); } } /** * Returns all theme options * * @since 1.0.0 */ public static function get_theme_options() { return get_option( 'theme_options' ); } /** * Returns single theme option * * @since 1.0.0 */ public static function get_theme_option( $id ) { $options = self::get_theme_options(); if ( isset( $options[$id] ) ) { return $options[$id]; } } /** * Add sub menu page * * @since 1.0.0 */ public static function add_admin_menu() { add_menu_page( esc_html__( 'BWT Mercator', 'text-domain' ), esc_html__( 'BWT Mercator', 'text-domain' ), 'manage_options', 'theme-settings', array( 'BWT_Theme_Options', 'create_admin_page' ) , get_template_directory_uri().'/assets/images/bootstrap-web-teamplates.png' ); } /** * Register a setting and its sanitization callback. * * We are only registering 1 setting so we can store all options in a single option as * an array. You could, however, register a new setting for each option * * @since 1.0.0 */ public static function register_settings() { register_setting( 'theme_options', 'theme_options', array( 'BWT_Theme_Options', 'sanitize' ) ); } /** * Sanitization callback * * @since 1.0.0 */ public static function sanitize( $options ) { // If we have options lets sanitize them if ( $options ) { // Checkbox if ( ! empty( $options['checkbox_example'] ) ) { $options['checkbox_example'] = 'on'; } else { unset( $options['checkbox_example'] ); // Remove from options if not checked } // Input if ( ! empty( $options['input_example'] ) ) { $options['input_example'] = sanitize_text_field( $options['input_example'] ); } else { unset( $options['input_example'] ); // Remove from options if empty } // Select if ( ! empty( $options['select_example'] ) ) { $options['select_example'] = sanitize_text_field( $options['select_example'] ); } } // Return sanitized options return $options; } /** * Settings page output * * @since 1.0.0 */ public static function create_admin_page() { ?> <div class="wrap"> <h1><?php esc_html_e( 'Theme Options', 'text-domain' ); ?></h1> <form method="post" action="options.php"> <?php settings_fields( 'theme_options' ); ?> <table class="form-table wpex-custom-admin-login-table"> <?php // Checkbox example ?> <tr valign="top"> <th scope="row"><?php esc_html_e( 'Checkbox Example', 'text-domain' ); ?></th> <td> <?php $value = self::get_theme_option( 'checkbox_example' ); ?> <input type="checkbox" name="theme_options[checkbox_example]" <?php checked( $value, 'on' ); ?>> <?php esc_html_e( 'Checkbox example description.', 'text-domain' ); ?> </td> </tr> <?php // Text input example ?> <tr valign="top"> <th scope="row"><?php esc_html_e( 'Input Example', 'text-domain' ); ?></th> <td> <?php $value = self::get_theme_option( 'input_example' ); ?> <input type="text" name="theme_options[input_example]" value="<?php echo esc_attr( $value ); ?>"> </td> </tr> <?php // Select example ?> <tr valign="top" class="wpex-custom-admin-screen-background-section"> <th scope="row"><?php esc_html_e( 'Select Example', 'text-domain' ); ?></th> <td> <?php $value = self::get_theme_option( 'select_example' ); ?> <select name="theme_options[select_example]"> <?php $options = array( '1' => esc_html__( 'Option 1', 'text-domain' ), '2' => esc_html__( 'Option 2', 'text-domain' ), '3' => esc_html__( 'Option 3', 'text-domain' ), ); foreach ( $options as $id => $label ) { ?> <option value="<?php echo esc_attr( $id ); ?>" <?php selected( $value, $id, true ); ?>> <?php echo strip_tags( $label ); ?> </option> <?php } ?> </select> </td> </tr> </table> <?php submit_button(); ?> </form> </div><!-- .wrap --> <?php } } } new BWT_Theme_Options(); // Helper function to use in your theme to return a theme option value function myprefix_get_theme_option( $id = '' ) { return BWT_Theme_Options::get_theme_option( $id ); } |
Leave a reply