ID, $field['id'], true ); // Get data to display in field if ( isset( $option ) ) { $data = $option; } } else { // Get saved option $option_name .= $field['id']; $option = get_option( $option_name ); // Get data to display in field if ( isset( $option ) ) { $data = $option; } } // Show default data if no option saved and default is supplied if ( $data === false && isset( $field['default'] ) ) { $data = $field['default']; } elseif ( $data === false ) { $data = ''; } $html = ''; switch( $field['type'] ) { case 'text': case 'url': case 'email': $html .= '' . "\n"; break; case 'password': case 'number': case 'hidden': $min = ''; if ( isset( $field['min'] ) ) { $min = ' min="' . esc_attr( $field['min'] ) . '"'; } $max = ''; if ( isset( $field['max'] ) ) { $max = ' max="' . esc_attr( $field['max'] ) . '"'; } $html .= '' . "\n"; break; case 'text_secret': $html .= '' . "\n"; break; case 'textarea_code': $html .= '
'. $data .'
'. "\n"; $html .= ''. "\n"; break; case 'textarea': $html .= '
'. "\n"; break; case 'checkbox': $checked = ''; if ( $data && 'on' == $data ) { $checked = 'checked="checked"'; } $html .= '' . "\n"; break; case 'checkbox_multi': foreach ( $field['options'] as $k => $v ) { $checked = false; if ( $data == false ) { $data = array(); } if ( in_array( $k, $data ) ) { $checked = true; } $html .= ' '; } break; case 'radio': foreach ( $field['options'] as $k => $v ) { $checked = false; if ( $k == $data ) { $checked = true; } $html .= ' '; } break; case 'select': $html .= ' '; break; case 'select_multi': $html .= ' '; break; case 'image': $image_thumb = ''; if ( $data ) { $image_thumb = wp_get_attachment_thumb_url( $data ); } $html .= '
' . "\n"; $html .= '' . "\n"; $html .= '' . "\n"; $html .= '
' . "\n"; break; case 'color': ?>
' . $field['description'] . ''; break; default: if ( ! $post ) { $html .= '' . "\n"; } break; } if ( ! $echo ) { return $html; } echo $html; } /** * Validate form field * @param string $data Submitted value * @param string $type Type of field to validate * @return string Validated value */ public function validate_field ( $data = '', $type = 'text' ) { switch( $type ) { case 'text': $data = esc_attr( $data ); break; case 'url': $data = esc_url( $data ); break; case 'email': $data = is_email( $data ); break; } return $data; } /** * Add meta box to the dashboard * @param string $id Unique ID for metabox * @param string $title Display title of metabox * @param array $post_types Post types to which this metabox applies * @param string $context Context in which to display this metabox ('advanced' or 'side') * @param string $priority Priority of this metabox ('default', 'low' or 'high') * @param array $callback_args Any axtra arguments that will be passed to the display function for this metabox * @return void */ public function add_meta_box ( $id = '', $title = '', $post_types = array(), $context = 'advanced', $priority = 'default', $callback_args = null ) { // Get post type(s) if ( ! is_array( $post_types ) ) { $post_types = array( $post_types ); } // Generate each metabox foreach ( $post_types as $post_type ) { add_meta_box( $id, $title, array( $this, 'meta_box_content' ), $post_type, $context, $priority, $callback_args ); } } /** * Display metabox content * @param object $post Post object * @param array $args Arguments unique to this metabox * @return void */ public function meta_box_content ( $post, $args ) { $fields = apply_filters( $post->post_type . '_custom_fields', array(), $post->post_type ); if ( ! is_array( $fields ) || 0 == count( $fields ) ) return; echo '
' . "\n"; foreach ( $fields as $field ) { if ( ! isset( $field['metabox'] ) ) continue; if ( ! is_array( $field['metabox'] ) ) { $field['metabox'] = array( $field['metabox'] ); } if ( in_array( $args['id'], $field['metabox'] ) ) { $this->display_meta_box_field( $field, $post ); } } echo '
' . "\n"; } /** * Dispay field in metabox * @param array $field Field data * @param object $post Post object * @return void */ public function display_meta_box_field ( $field = array(), $post ) { if ( ! is_array( $field ) || 0 == count( $field ) ) return; $field = '

' . $this->display_field( $field, $post, false ) . '

' . "\n"; echo $field; } /** * Save metabox fields * @param integer $post_id Post ID * @return void */ public function save_meta_boxes ( $post_id = 0 ) { if ( ! $post_id ) return; $post_type = get_post_type( $post_id ); $fields = apply_filters( $post_type . '_custom_fields', array(), $post_type ); if ( ! is_array( $fields ) || 0 == count( $fields ) ) return; foreach ( $fields as $field ) { if ( isset( $_REQUEST[ $field['id'] ] ) ) { update_post_meta( $post_id, $field['id'], $this->validate_field( $_REQUEST[ $field['id'] ], $field['type'] ) ); } else { update_post_meta( $post_id, $field['id'], '' ); } } } }