WordPress Custom Fields Plugin Snippet
A little snippetto show how customer fields works in WordPress Expected Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
new SY_Custom_Fields(array( 'page_title' => 'Admin Settings', 'menu_title' => 'SY:: Admin Settings', 'slug' => 'sy_admin_settings', 'icon' => 'dashicons-admin-plugins', 'add_to' => 'settings', 'section_title' => 'Theme Settings', 'section' => 'admin_theme_settings', 'options' => array( array( 'uid' => 'enable_admin_theme', 'label' => 'Enable Admin Theme', 'type' => 'checkbox', 'options' => array( 'true' => '', ), 'default' => array( 'false' ), ), ) )); |
Class
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
class Custom_Fields { // ========== Variables =========== public $page_title = ''; public $menu_title = ''; public $capability = ''; public $slug = ''; public $callback = array(); public $icon = ''; public $position = 0; // custom variables public $add_to = array(); public $options = array(); public $section_title = ''; public $section = ''; public $parent_slugs = array( 'dashboard' => 'index.php', 'posts' => 'edit.php', 'media' => 'upload.php', 'pages' => 'edit.php?post_type=page', 'comments' => 'edit-comments.php', 'custom_page_types' => 'edit.php?post_type=your_post_type', 'appearance' => 'themes.php', 'plugins' => 'plugins.php', 'users' => 'users.php', 'tools' => 'tools.php', 'settings' => 'options-general.php', 'network_settings' => 'settings.php' ); /** * Constructor * @param String $args['page_title'] * @param String $args['menu_title'] * @param String $args['slug'] * @param String $args['icon'] * @param String $args['add_to'] * @param String $args['section_title'] * @param String $args['section'] * @param Array $args['options'] */ public function __construct($args) { // initialize variables $this->init($args); // register CSS wp_register_style( 'sy-custom-settings-css', plugins_url( 'sy-custom-settings/sy-custom-settings.css' )); wp_enqueue_style( 'sy-custom-settings-css' ); // Hook into the admin menu add_action( 'admin_menu', array( $this, 'create_plugin_settings_page')); // Add Settings and Fields add_action( 'admin_init', array( $this, 'setup_sections' ) ); add_action( 'admin_init', array( $this, 'setup_fields' ) ); } /** * ========== Initialize variables ========== * @param String $args['page_title'] * @param String $args['menu_title'] * @param String $args['slug'] * @param String $args['icon'] * @param String $args['add_to'] * @param String $args['section_title'] * @param String $args['section'] * @param Array $args['options'] */ public function init($args){ $this->page_title = $args['page_title']; $this->menu_title = $args['menu_title']; $this->capability = 'manage_options'; $this->slug = $args['slug']; $this->callback = array( $this, 'plugin_settings_page_content' ); $this->icon = $args['icon']; $this->position = 100; // custom $this->add_to = $args['add_to']; $this->options = $args['options']; $this->section_title = $args['section_title']; $this->section = $args['section']; } /** * ========== Plugin Settings ========== * Add option under a parent if it exists * Otherwise, create a new option menu */ public function create_plugin_settings_page() { // add_submenu_page( $slug, $page_title, $menu_title, $capability, $slug . '-sub', $callback ); // check if parent slugs exists in the array already $parent_slug_exists = in_array($this->add_to, array_keys($this->parent_slugs)); // add it under parent slug if($parent_slug_exists){ $parent_slug = $this->parent_slugs[$this->add_to]; add_submenu_page($parent_slug, $this->page_title, $this->menu_title, $this->capability, $this->slug, $this->callback ); return; } // add itself as a parent slug add_menu_page( $this->page_title, $this->menu_title, $this->capability, $this->slug, $this->callback, $this->icon, $this->position ); } /** * ========== Display =========== * The HTML side of the option page */ public function plugin_settings_page_content() {?> <div class="wrap"> <h2><?php echo $this->page_title; ?></h2> <hr> <!-- Display admin notice ----> <?php if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){ $this->admin_notice(); } ?> <!-- form --> <form method="POST" action="options.php"> <?php settings_fields( $this->slug ); do_settings_sections( $this->slug ); submit_button(); ?> </form> </div> <?php } // =========== Admin Notice =========== public function admin_notice() { ?> <div class="notice notice-success is-dismissible"> <p>Your settings have been updated!</p> </div><?php } // =========== Setup Sections ============= public function setup_sections() { add_settings_section( $this->section, $this->section_title, array( $this, 'section_callback' ), $this->slug ); // add_settings_section( 'theme_settings', 'Theme Settings', array( $this, 'section_callback' ), $this->slug ); // add_settings_section( 'our_third_section', 'My Third Section Title', array( $this, 'section_callback' ), $this->slug ); } // ========== Section function callback =========== public function section_callback( $args ) { switch( $args['id'] ){ case $this->section: // echo 'Settings for your Live TV'; break; // case 'theme_settings': // echo 'This one is number two'; // break; // case 'our_third_section': // echo 'Third time is the charm!'; // break; } } // ========== Setup Fields =========== public function setup_fields() { $fields = $this->options; foreach( $fields as $field ){ add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), $this->slug, $this->section, $field ); register_setting( $this->slug, $field['uid'] ); } // foreach( $fields as $field ){ // add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), $this->slug, $field['section'], $field ); // register_setting( $this->slug, $field['uid'] ); // } } // =========== Field functions callback ========== public function field_callback( $args ) { $value = get_option( $args['uid'] ); if( ! $value || !isset($value)) { $value = isset($args['default']) ? $args['default'] : null; } switch( $args['type'] ){ case 'text': case 'password': case 'number': printf( '<input class="sy_custom_input" name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />', $args['uid'], $args['type'], $args['placeholder'], $value ); break; case 'textarea': printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>', $args['uid'], $args['placeholder'], $value ); break; case 'select': case 'multiselect': if( ! empty ( $args['options'] ) && is_array( $args['options'] ) ){ $attributes = ''; $options_markup = ''; foreach( $args['options'] as $key => $label ){ $options_markup .= sprintf( '<option value="%s" %s>%s</option>', $key, selected( $value[ array_search( $key, $value, true ) ], $key, false ), $label ); } if( $args['type'] === 'multiselect' ){ $attributes = ' multiple="multiple" '; } printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>', $args['uid'], $attributes, $options_markup ); } break; case 'radio': case 'checkbox': if( ! empty ( $args['options'] ) && is_array( $args['options'] ) ){ $options_markup = ''; $i = 0; foreach( $args['options'] as $key => $label ){ $i++; $checked = !isset($value) ? false : checked( $value[ array_search( $key, $value, true ) ], $key, false ); $options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $args['uid'], $args['type'], $key, $checked, $label, $i ); } printf( '<fieldset>%s</fieldset>', $options_markup ); } break; } if(isset($args['helper'])){ printf( '<span class="helper"> %s</span>', $args['helper'] ); } if(isset($args['supplimental'])){ printf( '<p class="description">%s</p>', $args['supplimental'] ); } } } |