$box ) { add_meta_box( slt_cf_prefix( 'post' ) . $box['id'], $box['title'], 'slt_cf_display_box', $post_type, $box['context'], $box['priority'], array( 'box' => $box_key ) ); } } // Inline scripting for moving boxes above content add_action( 'admin_head-post.php', 'slt_cf_move_metaboxes' ); add_action( 'admin_head-post-new.php', 'slt_cf_move_metaboxes' ); function slt_cf_move_metaboxes() { global $slt_custom_fields; $output = array(); foreach ( $slt_custom_fields['boxes'] as $box_key => $box ) { if ( array_key_exists( 'above_content', $box ) && $box['above_content'] ) $output[] = 'slt_cf_metaboxes_above_content.push( "' . slt_cf_prefix( 'post' ) . $box['id'] . '" );'; } if ( ! empty( $output ) ) { ?> $box ) slt_cf_display_box( $user, $box_key, 'user' ); } /* Add fields to an attachment screen * No boxes / sections, just loop through all fields ***************************************************************************/ function slt_cf_add_attachment_fields( $form_fields, $post ) { global $slt_custom_fields; foreach ( $slt_custom_fields['boxes'] as $box_key => $box ) { foreach ( $box['fields'] as $field ) { // Only certain fields types allowed for now if ( ! in_array( $field['type'], array( 'text', 'select' ) ) ) continue; // Add into form fields array $field_name = slt_cf_prefix( 'attachment' ) . $field['name']; $form_fields[ $field_name ] = array(); $form_fields[ $field_name ]['label'] = $field['label']; $form_fields[ $field_name ]['value'] = slt_cf_field_value( $field['name'], 'attachment', $post->ID, '', '', false, $field['single'] ); $form_fields[ $field_name ]['input'] = 'html'; $input_field_name = "attachments[{$post->ID}][{$field_name}]"; switch ( $field['type'] ) { case 'select': { $form_fields[ $field_name ]['html'] = slt_cf_input_select( $input_field_name, $form_fields[ $field_name ]['value'], $field['input_prefix'], $field['input_suffix'], array(), array(), false, $field['options'], $field['multiple'], ( $field['options_type'] != 'static' && ! $field['required'] ), $field['empty_option_text'], $field['no_options'] ); break; } case 'text': { $form_fields[ $field_name ]['html'] = slt_cf_input_text( $input_field_name, $form_fields[ $field_name ]['value'], $field['prefix'], $field['suffix'], array(), array(), false ); break; } } if ( $field['description'] ) $form_fields[ $field_name ]['helps'] = $field['description']; } } return $form_fields; } /* Display a box's fields ***************************************************************************/ function slt_cf_display_box( $object, $custom_data, $request_type = 'post' ) { global $slt_custom_fields; static $date_output = false; // Initialize switch ( $request_type ) { case 'post': { // Get the key of the box we're in from the args $box_key = $custom_data['args']['box']; // Nonce for security wp_nonce_field( slt_cf_prefix( 'post' ) . $slt_custom_fields['boxes'][$box_key]['id'] . '_save', $slt_custom_fields['prefix'] . $slt_custom_fields['boxes'][$box_key]['id'] . '_wpnonce', false, true ); break; } case 'user': { // Get box key and output initial markup $box_key = $custom_data; echo '

' . $slt_custom_fields['boxes'][ $box_key ]['title']. '

'; echo ''; break; } } // Description if ( $slt_custom_fields['boxes'][ $box_key ][ 'description' ] ) echo '

' . $slt_custom_fields['boxes'][ $box_key ][ 'description' ] . '

'; // Loop through fields for this box foreach ( $slt_custom_fields['boxes'][ $box_key ]['fields'] as $field ) { $field_name = slt_cf_prefix( $slt_custom_fields['boxes'][ $box_key ]['type'] ) . $field['name']; // Skip fields not allowed in this scope if ( $field['type'] == 'file' && $request_type == 'user' ) continue; if ( ( $request_type == 'post' && $object->post_status == 'auto-draft' ) || ! slt_cf_field_exists( $field['name'], $request_type, $object->ID ) ) { // Field doesn't exist yet, use a default if set $field_value = $field['default']; } else { // Get field value $field_value = slt_cf_field_value( $field['name'], $request_type, $object->ID, '', '', false, $field['single'] ); } // Reverse autop? if ( $field['autop'] ) $field_value = slt_cf_reverse_wpautop( $field_value ); // Set defaults for styles and classes $field_classes = array( 'slt-cf', 'slt-cf-' . $field['type'], 'slt-cf-field_' . $field['name'] ); $label_classes = array( 'slt-cf-label' ); $input_classes = array( 'slt-cf-input' ); $input_styles = array(); $legend_classes = array(); if ( in_array( $field['type'], array( 'checkboxes', 'radio' ) ) ) { $multi_field_classes = array( 'slt-cf-multifield' ); $multi_field_styles = array(); if ( $field['width'] > 0 ) { $multi_field_classes[] = 'slt-cf-fixed-width'; $multi_field_styles[] = 'width:' . $field['width'] . 'em'; } } else { if ( $field['width'] && $field['type'] != 'wysiwyg' ) $input_styles[] = 'width:' . $field['width'] . 'em'; if ( $field['label_layout'] == 'block' ) $field_classes[] = 'label-block'; } if ( $field['height'] && $field['type'] != 'wysiwyg' ) $input_styles[] = 'height:' . $field['height'] . 'em'; // This will hide the label / legend if ( $request_type == 'post' && $field['hide_label'] ) { $label_classes[] = 'screen-reader-text'; $legend_classes[] = 'screen-reader-text'; } // Markup to wrap field switch ( $request_type ) { case 'post': { echo '
'; $before_label = ''; $after_label = ''; $before_input = ''; $after_input = ''; break; } case 'user': { echo '
'; $before_label = ''; $before_input = ''; break; } } // Cloning // [under development] /*if ( $field['cloning'] ) { $after_input = '

' . __( "Clone field", 'slt-custom-fields' ) . '

' . $after_input; }*/ // Description $field_description = ''; if ( $field['type'] == 'textile' ) { $field_description .= '

' . __( 'You can apply the following simple formatting codes: **bold**  __italic__  "Link text":http://domain.com', 'slt-custom-fields' ) . '

'; } else if ( ! current_user_can( 'unfiltered_html' ) ) { // If user can't submit any HTML, display any that is allowed if ( isset( $field['allowed_html'] ) && is_array( $field['allowed_html'] ) && count( $field['allowed_html'] ) ) { $field_description .= '

' . __( "You can use the following HTML tags:", 'slt-custom-fields' ); // Switcheroo to use the WP allowed_tags function global $allowedtags; $temp = $allowedtags; $allowedtags = $field['allowed_html']; $field_description .= '' . allowed_tags() . '

'; $allowedtags = $temp; } else if ( isset( $field['allowtags'] ) && is_array( $field['allowtags'] ) && count( $field['allowtags'] ) ) { // Deprecated $field_description .= '

' . __( "You can use the following HTML tags:", 'slt-custom-fields' ); foreach ( $field['allowtags'] as $tag ) $field_description .= '<' . $tag . '> '; $field_description .= '

'; } } if ( isset( $field['autop'] ) && $field['autop'] ) $field_description .= '

' . __( "Line and paragraph breaks will be maintained.", 'slt-custom-fields' ) . '

'; if ( $field['description'] ) $field_description .= '

' . $field['description'] . '

'; // Which type of field? switch ( $field['type'] ) { case "checkbox": { /* Single checkbox *****************************************************************/ // Input $input = $before_input . '' . $field['label'] . '' . $after_label; if ( $request_type == 'post' ) { echo $input; echo $label; } else if ( $request_type == 'user' ) { echo $label; echo $input; } echo $field_description; break; } case "checkboxes": { /* Multiple checkboxes *****************************************************************/ if ( $request_type == 'post' ) { echo '
'; echo '' . $field['label'] . ''; } else { echo $before_label . $field['label'] . $after_label; } // Loop through options echo $before_input; // No options? if ( empty( $field['options'] ) ) { echo '

' . $field['no_options'] . '

'; echo ''; } else { foreach ( $field['options'] as $key => $value ) { echo '
'; // Input echo ''; // Label echo ' '; echo '
'; } } if ( $request_type == 'post' ) echo '
'; echo $field_description; echo $after_input; break; } case "radio": { /* Radio buttons *****************************************************************/ if ( $request_type == 'post' ) { echo '
'; echo '' . $field['label'] . ''; } else { echo $before_label . $field['label'] . $after_label; } // Loop through options echo $before_input; // No options? if ( empty( $field['options'] ) ) { echo '

' . $field['no_options'] . '

'; echo ''; } else { foreach ( $field['options'] as $key => $value ) { echo '
'; // Input echo ''; // Label echo ' '; echo '
'; } } if ( $request_type == 'post' ) echo '
'; echo $field_description; echo $after_input; break; } case "textarea": case "textile": case "wysiwyg": { /* Text area / textile / WYSIWYG *****************************************************************/ // Label echo $before_label . '' . $after_label; // Input echo $before_input; // Make sure textarea isn't output for WYSIWYG for 3.3 and above, wp_editor handles that if ( $field['type'] != 'wysiwyg' || ! SLT_CF_WP_IS_GTE_3_3 ) { echo ''; // Character counter if ( $field['type'] != "wysiwyg" && isset( $field['charcounter'] ) && $field['charcounter'] ) echo '

' . __( "Characters so far:", 'slt-custom-fields' ) . '

'; } // WYSIWYG if ( $field['type'] == 'wysiwyg' ) { if ( SLT_CF_WP_IS_GTE_3_3 ) { // For 3.3 and above - simple :) wp_editor( $field_value, $field_name, $field['wysiwyg_settings'] ); } else { // For versions below 3.3 ?> ' . $field['label'] . '' . $after_label; // Input echo $before_input; slt_cf_input_select( $field_name, $field_value, $field['input_prefix'], $field['input_suffix'], $input_styles, $input_classes, true, $field['options'], $field['multiple'], ( $field['options_type'] != 'static' && ! $field['required'] ), $field['empty_option_text'], $field['no_options'] ); echo $field_description; echo $after_input; break; } case 'file': { /* File upload field *****************************************************************/ // Label echo $before_label . '' . $after_label; // Input echo $before_input; slt_cf_file_select_button( $field_name, $field_value, $field['file_button_label'], $field['preview_size'], $field['file_removeable'], $field['file_attach_to_post'] ); echo $field_description; echo $after_input; break; } case 'gmap': { /* Google Map field *****************************************************************/ // Label echo $before_label . '' . $after_label; // Input echo $before_input; slt_cf_gmap( 'input', $field_name, $field_value, $field['width'], $field['height'], $field['location_marker'], $field['gmap_type'], true, '', $field['required'] ); echo $field_description; echo $after_input; break; } case 'date': { /* Date field *****************************************************************/ // Label echo $before_label . '' . $after_label; // Input $input_classes[] = 'slt-cf-date'; echo $before_input; echo ''; echo ' ' . str_replace( "y", "yy", $field['datepicker_format'] ) . ''; if ( ! $date_output ) { ?> ' . $field['label'] . '' . $after_label; echo $before_input . $field_description . $after_input; break; } default: { /* Plain text field *****************************************************************/ // Label echo $before_label . '' . $after_label; // Input $input_classes[] = 'regular-text'; echo $before_input; slt_cf_input_text( $field_name, $field_value, $field['input_prefix'], $field['input_suffix'], $input_styles, $input_classes ); echo $field_description; echo $after_input; break; } } // End switch // Markup to wrap field switch ( $request_type ) { case 'post': { echo ''; break; } case 'user': { echo ''; break; } } } // Fields foreach // Round off any markup if ( $request_type == 'user') echo '
'; $after_label = ''; $after_input = '
'; } /* Field output functions ***************************************************************************/ // Plain text function slt_cf_input_text( $field_name, $field_value = '', $prefix = '', $suffix = '', $input_styles = array(), $input_classes = array(), $echo = true ) { $output = ''; if ( $prefix ) $output .= $prefix . ' '; $output .= ''; if ( $suffix ) $output .= ' ' . $suffix; if ( $echo ) echo $output; else return $output; } // Select function slt_cf_input_select( $field_name, $field_value, $prefix = '', $suffix = '', $input_styles = array(), $input_classes = array(), $echo = true, $options, $multiple = false, $empty_option = false, $empty_option_text = '', $no_options = '' ) { $output = ''; // No options? if ( empty( $options ) ) { if ( ! $no_options ) $no_options = SLT_CF_NO_OPTIONS; $output .= '

' . $no_options . '

'; $output .= ''; } else { if ( $multiple ) { $input_styles[] = 'height:auto'; } else if ( $prefix ) { $output .= $prefix . ' '; } $output .= ''; if ( ! $multiple && $suffix ) $output .= ' ' . $suffix; } if ( $echo ) echo $output; else return $output; }