| @@ -1,0 +1,227 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * CMB2 Metatabs Options (CMO) Example | |
| 4 | + * ----------------------------------- | |
| 5 | + * This file creates an options page located in under the WordPress "Settings" menu. | |
| 6 | + * | |
| 7 | + * Do not run this file directly; instead uncomment | |
| 8 | + * include(example.php); | |
| 9 | + * in the main plugin file to see the results of the code in this file. | |
| 10 | + * | |
| 11 | + * More information is available at this CMB2 Metatabs Option's wiki: | |
| 12 | + * https://github.com/rogerlos/cmb2-metatabs-options/wiki | |
| 13 | + * | |
| 14 | + * @since 1.0.1 Revised comments | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | + | |
| 18 | +// add action to hook option page creation to | |
| 19 | +add_action( 'cmb2_admin_init', 'cmb2_metatabs_options_go' ); | |
| 20 | + | |
| 21 | +/** | |
| 22 | + * Callback for 'cmb2_admin_init'. | |
| 23 | + * | |
| 24 | + * In this example, 'boxes' and 'tabs' call functions simply to separate "normal" CMB2 configuration | |
| 25 | + * from unique CMO configuration. | |
| 26 | + */ | |
| 27 | +function cmb2_metatabs_options_go() { | |
| 28 | + | |
| 29 | + $options_key = 'cmb2metatabs'; | |
| 30 | + | |
| 31 | + // use CMO filter to add an intro at the top of the options page | |
| 32 | + add_filter( 'cmb2metatabs_before_form', 'cmb2_metatabs_options_add_intro_via_filter' ); | |
| 33 | + | |
| 34 | + // configuration array | |
| 35 | + $args = array( | |
| 36 | + 'key' => $options_key, | |
| 37 | + 'title' => 'Example Options Page', | |
| 38 | + 'topmenu' => 'options-general.php', | |
| 39 | + 'cols' => 2, | |
| 40 | + 'boxes' => cmb2_metatabs_options_add_boxes( $options_key ), | |
| 41 | + 'tabs' => cmb2_metatabs_options_add_tabs(), | |
| 42 | + 'menuargs' => array( | |
| 43 | + 'menu_title' => 'CMO Sample', | |
| 44 | + ), | |
| 45 | + ); | |
| 46 | + | |
| 47 | + // create the options page | |
| 48 | + new Cmb2_Metatabs_Options( $args ); | |
| 49 | +} | |
| 50 | + | |
| 51 | + | |
| 52 | +/** | |
| 53 | + * Callback for CMO filter. | |
| 54 | + * | |
| 55 | + * The two filters in CMO do not send any content; simply return your HTML. | |
| 56 | + * | |
| 57 | + * @return string | |
| 58 | + */ | |
| 59 | +function cmb2_metatabs_options_add_intro_via_filter() { | |
| 60 | + return '<p>This is an options page created with CMB2 Metatabs Options. Learn more at ' | |
| 61 | + . '<a href="https://github.com/rogerlos/cmb2-metatabs-options/" target="_blank">github</a>.</p>'; | |
| 62 | +} | |
| 63 | + | |
| 64 | + | |
| 65 | +/** | |
| 66 | + * Add some boxes the normal CMB2 way. (Five boxes and their fields, in this example.) | |
| 67 | + * | |
| 68 | + * This is typical CMB2, but note two crucial extra items: | |
| 69 | + * | |
| 70 | + * - the ['show_on'] property is configured | |
| 71 | + * - a call to object_type method | |
| 72 | + * | |
| 73 | + * See the wiki for more detail on why these are important and what their values are. | |
| 74 | + * | |
| 75 | + * @param string $options_key | |
| 76 | + * | |
| 77 | + * @return array | |
| 78 | + */ | |
| 79 | +function cmb2_metatabs_options_add_boxes( $options_key ) { | |
| 80 | + | |
| 81 | + // holds all CMB2 box objects | |
| 82 | + $boxes = array(); | |
| 83 | + | |
| 84 | + // we will be adding this to all boxes | |
| 85 | + $show_on = array( | |
| 86 | + 'key' => 'options-page', | |
| 87 | + 'value' => array( $options_key ), | |
| 88 | + ); | |
| 89 | + | |
| 90 | + // first box | |
| 91 | + $cmb = new_cmb2_box( array( | |
| 92 | + 'id' => 'ex_dogs', | |
| 93 | + 'title' => __( 'Internet Doggies', 'cmb2' ), | |
| 94 | + 'show_on' => $show_on, // critical, see wiki for why | |
| 95 | + ) ); | |
| 96 | + $cmb->add_field( array( | |
| 97 | + 'name' => __( 'That\'s a Good Dog!', 'cmb2' ), | |
| 98 | + 'desc' => __( 'What do you say when you see a dog on the internet?', 'cmb2' ), | |
| 99 | + 'id' => 'ex_dogs_say', | |
| 100 | + 'type' => 'text', | |
| 101 | + ) ); | |
| 102 | + $cmb->add_field( array( | |
| 103 | + 'name' => __( 'Repeated How Many Times?', 'cmb2' ), | |
| 104 | + 'desc' => __( 'To the nearest multiple 3, how many times do you say it?', 'cmb2' ), | |
| 105 | + 'id' => 'ex_dogs_repeat', | |
| 106 | + 'type' => 'text_small', | |
| 107 | + ) ); | |
| 108 | + $cmb->object_type( 'options-page' ); // critical, see wiki for why | |
| 109 | + $boxes[] = $cmb; | |
| 110 | + | |
| 111 | + // second box | |
| 112 | + $cmb = new_cmb2_box( array( | |
| 113 | + 'id' => 'ex_cats', | |
| 114 | + 'title' => __( 'Internet Kitties', 'cmb2' ), | |
| 115 | + 'show_on' => $show_on, | |
| 116 | + ) ); | |
| 117 | + $cmb->add_field( array( | |
| 118 | + 'name' => __( 'Nice kitty!', 'cmb2' ), | |
| 119 | + 'desc' => __( 'What do you say when you see a cat on the internet?', 'cmb2' ), | |
| 120 | + 'id' => 'ex_cats_say', | |
| 121 | + 'type' => 'text', | |
| 122 | + ) ); | |
| 123 | + $cmb->add_field( array( | |
| 124 | + 'name' => __( 'Repeated How Many Times?', 'cmb2' ), | |
| 125 | + 'desc' => __( 'To the nearest multiple 3, how many times do you say it?', 'cmb2' ), | |
| 126 | + 'id' => 'ex_cats_repeat', | |
| 127 | + 'type' => 'text_small', | |
| 128 | + ) ); | |
| 129 | + $cmb->object_type( 'options-page' ); | |
| 130 | + $boxes[] = $cmb; | |
| 131 | + | |
| 132 | + // third box | |
| 133 | + $cmb = new_cmb2_box( array( | |
| 134 | + 'id' => 'ex_healthy', | |
| 135 | + 'title' => __( 'Eating for Good Health', 'cmb2' ), | |
| 136 | + 'show_on' => $show_on, | |
| 137 | + ) ); | |
| 138 | + $cmb->add_field( array( | |
| 139 | + 'name' => __( 'What is a healthy food?', 'cmb2' ), | |
| 140 | + 'desc' => __( 'Examples: Apple, Ding Dong', 'cmb2' ), | |
| 141 | + 'id' => 'ex_healthy_food', | |
| 142 | + 'type' => 'text', | |
| 143 | + ) ); | |
| 144 | + $cmb->add_field( array( | |
| 145 | + 'name' => __( 'How Many Servings?', 'cmb2' ), | |
| 146 | + 'desc' => __( 'How many times per day should you eat this?', 'cmb2' ), | |
| 147 | + 'id' => 'ex_healthy_servings', | |
| 148 | + 'type' => 'text_small', | |
| 149 | + ) ); | |
| 150 | + $cmb->object_type( 'options-page' ); | |
| 151 | + $boxes[] = $cmb; | |
| 152 | + | |
| 153 | + // fourth box | |
| 154 | + $cmb = new_cmb2_box( array( | |
| 155 | + 'id' => 'ex_bad', | |
| 156 | + 'title' => __( 'Foods to Avoid', 'cmb2' ), | |
| 157 | + 'show_on' => $show_on, | |
| 158 | + ) ); | |
| 159 | + $cmb->add_field( array( | |
| 160 | + 'name' => __( 'What is an unhealthy food?', 'cmb2' ), | |
| 161 | + 'desc' => __( 'Examples: Apple, not Ding Dong', 'cmb2' ), | |
| 162 | + 'id' => 'ex_bad_food', | |
| 163 | + 'type' => 'text', | |
| 164 | + ) ); | |
| 165 | + $cmb->add_field( array( | |
| 166 | + 'name' => __( 'How Many Pushups?', 'cmb2' ), | |
| 167 | + 'desc' => __( 'To the nearest 1, how many pushups do you need to counter your bad decision?', 'cmb2' ), | |
| 168 | + 'id' => 'ex_bad_servings', | |
| 169 | + 'type' => 'text_small', | |
| 170 | + ) ); | |
| 171 | + $cmb->object_type( 'options-page' ); | |
| 172 | + $boxes[] = $cmb; | |
| 173 | + | |
| 174 | + // fifth box | |
| 175 | + $cmb = new_cmb2_box( array( | |
| 176 | + 'id' => 'ex_side', | |
| 177 | + 'title' => __( 'Judging', 'cmb2' ), | |
| 178 | + 'show_on' => $show_on, | |
| 179 | + 'context' => 'side', | |
| 180 | + ) ); | |
| 181 | + $cmb->add_field( array( | |
| 182 | + 'name' => '', | |
| 183 | + 'desc' => __( 'This example page offers no judgment on your choices.', 'cmb2' ), | |
| 184 | + 'id' => 'ex_judge', | |
| 185 | + 'type' => 'title', | |
| 186 | + ) ); | |
| 187 | + $cmb->object_type( 'options-page' ); | |
| 188 | + $boxes[] = $cmb; | |
| 189 | + | |
| 190 | + return $boxes; | |
| 191 | +} | |
| 192 | + | |
| 193 | + | |
| 194 | +/** | |
| 195 | + * Add some tabs (in this case, two). | |
| 196 | + * | |
| 197 | + * Tabs are completely optional and removing them would result in the option metaboxes displaying sequentially. | |
| 198 | + * | |
| 199 | + * If you do configure tabs, all boxes whose context is "normal" or "advanced" must be in a tab to display. | |
| 200 | + * | |
| 201 | + * @return array | |
| 202 | + */ | |
| 203 | +function cmb2_metatabs_options_add_tabs() { | |
| 204 | + | |
| 205 | + $tabs = array(); | |
| 206 | + | |
| 207 | + $tabs[] = array( | |
| 208 | + 'id' => 'ex_tab1', | |
| 209 | + 'title' => 'Critters', | |
| 210 | + 'desc' => '<p>Everyone likes dogs and/or cats, right?</p>', | |
| 211 | + 'boxes' => array( | |
| 212 | + 'ex_cats', | |
| 213 | + 'ex_dogs', | |
| 214 | + ), | |
| 215 | + ); | |
| 216 | + $tabs[] = array( | |
| 217 | + 'id' => 'ex_tab2', | |
| 218 | + 'title' => 'Eats', | |
| 219 | + 'desc' => '', | |
| 220 | + 'boxes' => array( | |
| 221 | + 'ex_healthy', | |
| 222 | + 'ex_bad', | |
| 223 | + ), | |
| 224 | + ); | |
| 225 | + | |
| 226 | + return $tabs; | |
| 227 | +} | |