/** * 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 term fields once for all term forms * * @param WP_Fields_API $wp_fields */ public function register_term_fields($wp_fields) { static $registered; if ($registered) { return; } $registered = true; $wp_fields->add_field($this->object_type, 'name'); $wp_fields->add_field($this->object_type, 'slug'); $wp_fields->add_field($this->object_type, 'parent'); $wp_fields->add_field($this->object_type, 'description'); }
/** * 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); } } }