-
Notifications
You must be signed in to change notification settings - Fork 99
Adding Fields with Fieldmanager
There are a number of different field types that can be set up. Here we will show some code samples that we have used to add the different fields to some sample post types.
All fields are added as meta boxes to the post type admin area. This can be done with the following code:
$fields = get_fields();
add_meta_box( 'sample_text_meta_box', __('Sample Text'), array( $fields['sample-text'], 'render_meta_box' ), 'the-post-type', 'normal', 'high' );
In the above example we are adding a text input box called Sample Text for the custom post type 'the-post-type'. You can use this for post, page and custom post type. You can see we declare a variable called $fields and populate it using get_fields. get_fields() returns an array of FM fields. In the sample get_fields() function below we will show you how to set up a number of custom FM fields. For example the 'sample-text' field above can be added with the following code:
public function get_fields() {
return array(
'sample-text' => new Fieldmanager_Textfield( array(
'label' => __('Text Sample'),
'name' => 'sample-text',
) )
);
}
Now lets look at some of the different input types:
'sample-text-area' => new Fieldmanager_TextArea( array(
'label' => __('Some Label'),
'name' => 'sample-text-area',
'attributes' => array(
'cols' => 80,
'rows' => 5
)
) )
'sample-check-box' => new Fieldmanager_Checkbox( array(
'label' => __('Sample Check Box'),
'name' => 'sample-check-box',
) )
Select boxes can be populated with taxonomy data. Below uses categories.
'sample-select' => new Fieldmanager_Select( array(
'first_element' => array( '', '' ),
'label' => null,
'name' => 'sample-select',
'taxonomy' => 'category',
'taxonomy_args' => array(
'orderby' => 'name',
'hide_empty' => false
)
) )
You can also populate the selects with custom data.
'sample-select' => new Fieldmanager_Select( array(
'first_element' => array( '', '' ),
'label' => null,
'name' => 'sample-select',
'data' => some_custom_data_function(),
) ),
where the data function returns keyed array with a name and a value key.
function some_custom_data_function() {
return array(
'0' => array('name' => 'some-name', 'value'=>'some-value'),
);
}
##Grouping Fields Fields can also be grouped. Below is an array that groups multiple text fields and a select.
array(
'sample-group' => new Fieldmanager_Group( array(
'name' => 'some-group',
'label' => null,
'children' => array(
'text-group1' => new Fieldmanager_Textfield( array(
'label' => __('Text Group 1'),
'name' => 'text-group1',
) ),
'text-group2' => new Fieldmanager_Textfield( array(
'label' => __('Text Group 2'),
'name' => 'text-group2',
) ),
'sample-select' => new Fieldmanager_Select( array(
'first_element' => array( '', '' ),
'label' => null,
'name' => 'sample-select',
'taxonomy' => 'category',
'taxonomy_args' => array(
'orderby' => 'name',
'hide_empty' => false
)
) ),
)
) )
);
###Draggable Groups We can also make it so that items within a group can be dragged and re-ordered and have new items added on the fly.
'custom_dynamic_group' => new Fieldmanager_Group( array(
'name' => 'some-name',
'label' => 'Custom Draggable Dynamic Group',
'limit' => 0,
'starting_count' => 0,
'add_more_label' => 'Add more',
'sortable' => True,
'collapsible' => True,
'children' => array(
'text-group1' => new Fieldmanager_Textfield( array(
'label' => __('Text Group 1'),
'name' => 'text-group1',
) ),
)
) )