| 1083 | | * @param string $callback Function that echos out any content at the top of the section (between heading and fields). |
| 1084 | | * @param string $page The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using add_options_page(); |
| | 1083 | * @param string $pre_callback Function that echos out any content at the top of the section (between heading and fields). |
| | 1084 | * @param string $page_id The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using add_options_page(); |
| 1096 | | if ( !isset($wp_settings_sections[$page]) ) |
| 1097 | | $wp_settings_sections[$page] = array(); |
| 1098 | | if ( !isset($wp_settings_sections[$page][$id]) ) |
| 1099 | | $wp_settings_sections[$page][$id] = array(); |
| | 1097 | if( !isset( $wp_settings_sections[$page_id] ) ) |
| | 1098 | $wp_settings_sections[$page_id] = array(); |
| | 1099 | if( isset( $wp_settings_sections[$page_id][$section_id] ) ) |
| | 1100 | return; //avoid overwriting the previous registration and adding additional pre-section callback. If output other than a description is needed before the section, the action can be hooked into on it's own |
| 1121 | | * @param string $callback Function that fills the field with the desired form inputs. The function should echo its output. |
| 1122 | | * @param string $page The slug-name of the settings page on which to show the section (general, reading, writing, ...). |
| 1123 | | * @param string $section The slug-name of the section of the settingss page in which to show the box (default, ...). |
| | 1125 | * @param string $display_callback Function that fills the field with the desired form inputs. The function should echo its output. |
| | 1126 | * @param string $page_id The slug-name of the settings page on which to show the section (general, reading, writing, ...). |
| | 1127 | * @param string $section_id The slug-name of the section of the settingss page in which to show the box (default, ...). |
| 1134 | | if ( !isset($wp_settings_fields) ) |
| 1135 | | $wp_settings_fields = array(); |
| 1136 | | if ( !isset($wp_settings_fields[$page]) ) |
| 1137 | | $wp_settings_fields[$page] = array(); |
| 1138 | | if ( !isset($wp_settings_fields[$page][$section]) ) |
| 1139 | | $wp_settings_fields[$page][$section] = array(); |
| | 1140 | //we set the field data here because I want the option_name to still be empty if it wasn't passed in |
| | 1141 | if( $option_name ) |
| | 1142 | $the_option_name = $option_name; |
| | 1143 | else |
| | 1144 | $the_option_name = $field_id; |
| 1141 | | $wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args); |
| | 1146 | if( !isset( $wp_settings_fields ) ) |
| | 1147 | $wp_settings_fields = array( ); |
| | 1148 | if( !isset( $wp_settings_fields[ $page_id ] ) ) |
| | 1149 | $wp_settings_fields[ $page_id ] = array( ); |
| | 1150 | if( !isset( $wp_settings_fields[ $page_id ][ $section_id ] ) ) |
| | 1151 | $wp_settings_fields[ $page_id ][ $section_id ] = array( ); |
| | 1152 | |
| | 1153 | if( !isset( $wp_settings_fields[ $page_id ][ $section_id ][ $the_option_name ] ) ) |
| | 1154 | $wp_settings_fields[ $page_id ][ $section_id ][ $the_option_name ] = array( ); |
| | 1155 | |
| | 1156 | if( !isset( $wp_settings_fields[ $page_id ][ $section_id ][ $the_option_name ][ $field_id ] ) ) |
| | 1157 | $wp_settings_fields[ $page_id ][ $section_id ][ $the_option_name ][ $field_id ] = array( 'id' => $field_id, 'title' => $title, 'callback' => $display_callback, 'args' => $args, 'option_name' => $option_name ); //don't override a previously registered field |
| | 1158 | |
| | 1159 | if( $sanitize_callback ) |
| | 1160 | add_filter( "sanitize_setting_{$page_id}_{$section_id}_{$field_id}", $sanitize_callback, 10, 3 ); //args planned to be $new_value, $old_value, $args from previous |
| 1163 | | foreach ( (array) $wp_settings_sections[$page] as $section ) { |
| 1164 | | if ( $section['title'] ) |
| 1165 | | echo "<h3>{$section['title']}</h3>\n"; |
| 1166 | | call_user_func($section['callback'], $section); |
| 1167 | | if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section['id']]) ) |
| 1168 | | continue; |
| 1169 | | echo '<table class="form-table">'; |
| 1170 | | do_settings_fields($page, $section['id']); |
| 1171 | | echo '</table>'; |
| | 1182 | foreach( ( array ) $wp_settings_sections[ $page_id ] as $section ) { |
| | 1183 | if( $section[ 'title' ] ) |
| | 1184 | echo "<h3>{$section[ 'title' ]}</h3>\n"; |
| | 1185 | |
| | 1186 | do_action( "pre_setting_section_{$page_id}_{$section[ 'id' ]}" ); |
| | 1187 | |
| | 1188 | if( isset( $wp_settings_fields ) && isset( $wp_settings_fields[ $page_id ] ) && isset( $wp_settings_fields[ $page_id ][ $section[ 'id' ] ] ) ) { |
| | 1189 | echo '<table class="form-table">'; |
| | 1190 | do_settings_fields( $page_id, $section[ 'id' ] ); |
| | 1191 | echo '</table>'; |
| | 1192 | } |
| | 1193 | do_action( "post_setting_section_{$page_id}_{$section[ 'id' ]}" ); |
| 1195 | | foreach ( (array) $wp_settings_fields[$page][$section] as $field ) { |
| 1196 | | echo '<tr valign="top">'; |
| 1197 | | if ( !empty($field['args']['label_for']) ) |
| 1198 | | echo '<th scope="row"><label for="' . $field['args']['label_for'] . '">' . $field['title'] . '</label></th>'; |
| 1199 | | else |
| 1200 | | echo '<th scope="row">' . $field['title'] . '</th>'; |
| 1201 | | echo '<td>'; |
| 1202 | | call_user_func($field['callback'], $field['args']); |
| 1203 | | echo '</td>'; |
| 1204 | | echo '</tr>'; |
| | 1217 | foreach( ( array ) $wp_settings_fields[ $page_id ][ $section_id ] as $option_group ) { //downside here obviously is that items that share option_names have to be grouped |
| | 1218 | foreach( ( array ) $option_group as $field ) { |
| | 1219 | echo '<tr valign="top">'; |
| | 1220 | if( !empty( $field[ 'args' ][ 'label_for' ] ) ) |
| | 1221 | echo '<th scope="row"><label for="' . $field[ 'args' ][ 'label_for' ] . '">' . $field[ 'title' ] . '</label></th>'; |
| | 1222 | else |
| | 1223 | echo '<th scope="row">' . $field[ 'title' ] . '</th>'; |
| | 1224 | echo '<td>'; |
| | 1225 | if( empty( $field[ 'option_name' ] ) ) |
| | 1226 | $current_value = get_option( $field[ 'id' ] ); |
| | 1227 | else { |
| | 1228 | $option_value = get_option( $field[ 'option_name' ] ); |
| | 1229 | if( is_array( $option_value ) && isset( $option_value[ $field[ 'id' ] ] ) ) |
| | 1230 | $current_value = $option_value[ $field[ 'id' ] ]; |
| | 1231 | else |
| | 1232 | $current_value = false; |
| | 1233 | } |
| | 1234 | call_user_func( $field[ 'callback' ], $field[ 'args' ], $current_value, $field, $section_id, $page_id ); |
| | 1235 | echo '</td>'; |
| | 1236 | echo '</tr>'; |
| | 1237 | } |
| | 1242 | * Saves the registered settings fields for the given page_id |
| | 1243 | * |
| | 1244 | * Part of the Settings API. This method should be called on 'load-{$page_slug}' |
| | 1245 | * to save any fields that have been registered for that page. |
| | 1246 | * |
| | 1247 | * @global $wp_settings_fields $wp_settings_fields |
| | 1248 | * @staticvar array $saved_settings_pages |
| | 1249 | * |
| | 1250 | * @since 3.3 |
| | 1251 | * |
| | 1252 | * @param string $page_id slug name of the page to identify the page used for the settings |
| | 1253 | */ |
| | 1254 | function save_settings_page( $page_id ) { |
| | 1255 | global $wp_settings_fields; |
| | 1256 | |
| | 1257 | static $saved_settings_pages = array( ); //local storage to keep pages from being processed multiple times |
| | 1258 | |
| | 1259 | if( !isset( $saved_settings_pages[ $page_id ] ) && isset( $wp_settings_fields[ $page_id ] ) && is_array( $wp_settings_fields[ $page_id ] ) ) { |
| | 1260 | foreach( $wp_settings_fields[ $page_id ] as $section_id => $section ) { |
| | 1261 | if( is_array( $section ) ) { |
| | 1262 | foreach( $section as $option_name => $fields ) { |
| | 1263 | $option_value = get_option( $option_name ); |
| | 1264 | if( is_array( $fields ) && count( $fields ) ) { |
| | 1265 | if( 1 === count( $fields ) && isset( $fields[ $option_name ] ) && $fields[ $option_name ][ 'id' ] == $option_name ) { |
| | 1266 | //if there is only one field for the option and it matches the option_name, this is a singular option |
| | 1267 | $new_value = isset( $_POST[ $fields[ $option_name ][ 'id' ] ] ) ? $_POST[ $fields[ $option_name ][ 'id' ] ] : false; |
| | 1268 | $option_value = apply_filters( "sanitize_setting_{$page_id}_{$section_id}_{$fields[ $option_name ][ 'id' ]}", $new_value, $option_value, $fields[ $option_name ] ); |
| | 1269 | } else { |
| | 1270 | //this is an set of fields stored under one option |
| | 1271 | if( !is_array( $option_value ) ) |
| | 1272 | $option_value = array( ); |
| | 1273 | |
| | 1274 | foreach( $fields as $field ) { |
| | 1275 | $new_value = isset( $_POST[ $field[ 'id' ] ] ) ? $_POST[ $field[ 'id' ] ] : false; |
| | 1276 | $old_value = isset( $option_value[ $field[ 'id' ] ] ) ? $option_value[ $field[ 'id' ] ] : false; |
| | 1277 | $option_value[ $field[ 'id' ] ] = apply_filters( "sanitize_setting_{$page_id}_{$section_id}_{$field[ 'id' ]}", $new_value, $old_value, $field ); |
| | 1278 | } |
| | 1279 | } |
| | 1280 | update_option( $option_name, $option_value ); |
| | 1281 | } |
| | 1282 | } |
| | 1283 | } |
| | 1284 | } |
| | 1285 | $saved_settings_pages[ $page_id ] = true; |
| | 1286 | } |
| | 1287 | if( !count( get_settings_errors( $page_id ) ) ) |
| | 1288 | add_settings_error( $page_id, 'settings_updated', __( 'Settings saved.' ), 'updated' ); |
| | 1289 | } |
| | 1290 | |
| | 1291 | /** |