/** * Encapsulated registering of sections, controls, and fields for a form * * @param WP_Fields_API $wp_fields */ public function register_fields($wp_fields) { // @todo Remove this when done testing if (!defined('WP_FIELDS_API_EXAMPLES') || !WP_FIELDS_API_EXAMPLES) { return; } ////////////// // Examples // ////////////// $total_examples = 1; if (defined('WP_FIELDS_API_TESTING') && WP_FIELDS_API_TESTING && !empty($_GET['fields-api-memory-test'])) { $total_examples = 25; if (1 < $_GET['fields-api-memory-test']) { $total_examples = absint($_GET['fields-api-memory-test']); } } for ($x = 1; $x <= $total_examples; $x++) { // Section $section_id = $this->id . '-example-my-fields-' . $x; $section_args = array('label' => __('Fields API Example - My Fields', 'fields-api'), 'form' => $this->id); if (1 < $total_examples) { $section_args['label'] .= ' ' . $x; } if (in_array($this->object_type, array('post', 'comment'))) { $section_args['type'] = 'meta-box'; } $wp_fields->add_section($this->object_type, $section_id, $this->object_name, $section_args); // Add example for each control type $control_types = array('text', 'textarea', 'checkbox', 'multi-checkbox', 'radio', 'select', 'dropdown-pages', 'dropdown-terms', 'color', 'media', 'media-file'); $option_types = array('multi-checkbox', 'radio', 'select'); foreach ($control_types as $control_type) { $field_id = 'example_my_' . $x . '_' . $control_type . '_field'; $label = sprintf(__('%s Field'), ucwords(str_replace('-', ' ', $control_type))); $field_args = array('control' => array('type' => $control_type, 'id' => $this->id . '-' . $field_id, 'section' => $section_id, 'label' => $label, 'description' => 'Example field description')); if (in_array($control_type, $option_types)) { $field_args['control']['choices'] = array('' => 'N/A', 'option-1' => 'Option 1', 'option-2' => 'Option 2', 'option-3' => 'Option 3', 'option-4' => 'Option 4', 'option-5' => 'Option 5'); if ('multi-checkbox' == $control_type) { unset($field_args['control']['choices']['']); } } elseif ('checkbox' == $control_type) { $field_args['control']['checkbox_label'] = 'Example checkbox label'; } if ('dropdown-terms' == $control_type) { $field_args['control']['taxonomy'] = 'category'; } $wp_fields->add_field($this->object_type, $field_id, $this->object_name, $field_args); } } }
/** * Encapsulated registering of sections, controls, and fields for a form * * @param WP_Fields_API $wp_fields */ public function register_fields($wp_fields) { /* // Register control types $wp_fields->register_control_type( 'control-type-id', 'Control_Class_Name' ); // Add section(s) $wp_fields->add_section( $this->object_type, 'section-id', $this->object_name, array( 'title' => __( 'Section Heading' ), 'form' => $this->id, ) ); $field_args = array( // 'sanitize_callback' => array( $this, 'my_sanitize_callback' ), 'control' => array( 'type' => 'text', 'section' => 'section-id', 'label' => __( 'Control Label' ), 'description' => __( 'Description of control' ), // 'capabilities_callback' => array( $this, 'my_capabilities_callback' ), ), ); $wp_fields->add_field( $this->object_type, 'field-id', $this->object_name, $field_args ); */ ////////////// // Examples // ////////////// // Section $section_args = array('title' => __('Fields API Example - My Fields'), 'form' => $this->id); if (in_array($this->object_type, array('post', 'comment'))) { $section_args['type'] = 'meta-box'; } $wp_fields->add_section($this->object_type, $this->id . '-example-my-fields', $this->object_name, $section_args); // Add example for each control type $control_types = array('text', 'textarea', 'checkbox', 'multi-checkbox', 'radio', 'select', 'dropdown-pages', 'dropdown-terms', 'color', 'media', 'media-file'); $option_types = array('multi-checkbox', 'radio', 'select'); foreach ($control_types as $control_type) { $id = 'example_my_' . $control_type . '_field'; $label = sprintf(__('%s Field'), ucwords(str_replace('-', ' ', $control_type))); $field_args = array('control' => array('type' => $control_type, 'id' => $this->id . '-' . $id, 'section' => $this->id . '-example-my-fields', 'label' => $label)); if (in_array($control_type, $option_types)) { $field_args['control']['choices'] = array('' => 'N/A', 'option-1' => 'Option 1', 'option-2' => 'Option 2', 'option-3' => 'Option 3', 'option-4' => 'Option 4', 'option-5' => 'Option 5'); } $wp_fields->add_field($this->object_type, $id, $this->object_name, $field_args); } }
/** * Register fields config for sections/fields created using this plugin * * @param WP_Fields_API $wp_fields */ protected function register_fields_config($wp_fields) { // Get supported forms/control types $fields_api_forms = $this->get_fields_api_forms(); $fields_api_control_types = $this->get_fields_api_control_types(); $args = array('post_type' => 'fields_api_section', 'posts_per_page' => 50, 'meta_query' => array(array('key' => 'fields_api_form', 'compare' => 'EXISTS'))); $sections = get_posts($args); foreach ($sections as $section) { $form = get_post_meta($section->ID, 'fields_api_form', true); // Skip sections without forms or forms that aren't supported if (empty($form) || empty($fields_api_forms[$form])) { continue; } $object_type = $fields_api_forms[$form]['object_type']; $object_subtype = get_post_meta($section->ID, 'fields_api_object_subtype', true); // Set empty Object subtype to null if (empty($object_subtype)) { $object_subtype = null; } // Section config $section_id = get_post_meta($section->ID, 'fields_api_section_id', true); if (empty($section_id)) { $section_id = sanitize_key('fields-admin-ui-' . $section->ID . '-' . $section->post_name); } $section_config = array('form' => $form, 'label' => $section->post_title, 'controls' => array()); $section_desc = get_post_meta($section->ID, 'fields_api_section_desc', true); if (!empty($section_desc)) { $section_config['description'] = $section_desc; } // Get controls for section $args = array('post_type' => 'fields_api_control', 'posts_per_page' => 50, 'meta_query' => array(array('key' => 'fields_api_section', 'value' => $section->ID))); $controls = get_posts($args); if (!empty($controls)) { foreach ($controls as $control) { // Control config $control_id = get_post_meta($control->ID, 'fields_api_control_id', true); if (empty($control_id)) { $control_id = sanitize_key($section->post_name . '_' . $control->post_name); } $section_config['controls'][$control_id] = array('label' => $control->post_title); $control_type = get_post_meta($control->ID, 'fields_api_control_type', true); if (!empty($control_type) && isset($fields_api_control_types[$control_type])) { $section_config['controls'][$control_id]['type'] = $control_type; } $control_desc = get_post_meta($control->ID, 'fields_api_control_desc', true); if (!empty($control_desc)) { $section_config['controls'][$control_id]['description'] = $control_desc; } } $wp_fields->add_section($object_type, $section_id, $object_subtype, $section_config); } } }