Skip to content

Instantly share code, notes, and snippets.

@bjornjohansen
Last active March 15, 2017 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjornjohansen/880648dcdbc82eabb319b2737ff475b8 to your computer and use it in GitHub Desktop.
Save bjornjohansen/880648dcdbc82eabb319b2737ff475b8 to your computer and use it in GitHub Desktop.
Mimic ACF flexible content fields with Field Manager
<?php
/**
* Mimic ACF flexible content fields with Field Manager
*/
add_action( 'fm_post_post', function() {
$fm = new Fieldmanager_Group( [
'name' => 'flexible-content',
'label' => __( 'Content Section', 'your-textdomain' ),
'limit' => 0,
'sortable' => 1,
'add_more_label' => __( 'Add Content Section', 'your-textdomain' ),
'children' => [
'section-title' => new Fieldmanager_TextField( __( 'Section Title', 'your-textdomain' ) ),
],
] );
$flexible_content_select = new Fieldmanager_Select( __( 'Section Type', 'your-textdomain' ), [ 'name' => 'section_type' ] );
$fm->add_child( $flexible_content_select );
$flexfields = [];
$flexfields[] = new Fieldmanager_Group( [
'name' => 'demo-group',
'label' => __( 'Demo Section', 'your-textdomain' ),
'children' => [
'field-one' => new Fieldmanager_TextField( __( 'First Field', 'your-textdomain' ) ),
'field-two' => new Fieldmanager_TextField( __( 'Second Field', 'your-textdomain' ) ),
],
] );
$flexfields[] = new Fieldmanager_Group( [
'name' => 'youtube',
'label' => __( 'YouTube Video', 'your-textdomain' ),
'children' => [
'youtube-url' => new Fieldmanager_TextField( __( 'YouTube URL', 'your-textdomain' ) ),
'preview-image' => new Fieldmanager_Media( __( 'Preview Image', 'your-textdomain' ), [
'preview_size' => 'medium',
] ),
],
] );
$flexfields[] = new Fieldmanager_Group( [
'name' => 'all-fields',
'label' => __( 'All Field Types', 'your-textdomain' ),
'children' => [
'autocomplete' => new Fieldmanager_Autocomplete( __( 'Autocomplete', 'your-textdomain' ), [ 'datasource' => new Fieldmanager_Datasource( [ 'options' => [ 'alt1' => __( 'Alternative One', 'your-textdomain' ), 'alt2' => __( 'Alternative Two', 'your-textdomain' ), 'alt3' => __( 'Alternative Three', 'your-textdomain' ) ] ] ) ] ),
'checkbox' => new Fieldmanager_Checkbox( __( 'Checkbox', 'your-textdomain' ) ),
'checkboxes' => new Fieldmanager_Checkboxes( __( 'Checkboxes', 'your-textdomain' ), [ 'options' => [ 'alt1' => __( 'Alternative One', 'your-textdomain' ), 'alt2' => __( 'Alternative Two', 'your-textdomain' ), 'alt3' => __( 'Alternative Three', 'your-textdomain' ) ] ] ),
'colorpicker' => new Fieldmanager_Colorpicker( __( 'Colorpicker', 'your-textdomain' ) ),
'datepicker' => new Fieldmanager_Datepicker( __( 'Datepicker', 'your-textdomain' ) ),
'draggablepost' => new Fieldmanager_DraggablePost( __( 'DraggablePost', 'your-textdomain' ), [ 'repositories' => [ [ 'label' => __( 'Available posts', 'your-textdomain' ), 'post_type' => 'post' ], [ 'label' => __( 'Available pages', 'your-textdomain' ), 'post_type' => 'page' ] ], 'bins' => [ 'featured_posts' => __( 'Featured Posts', 'your-textdomain' ), 'really_featured_posts' => __( 'Really Featured Posts', 'your-textdomain' ) ] ] ),
'grid' => new Fieldmanager_Grid( __( 'Grid', 'your-textdomain' ) ),
'link' => new Fieldmanager_Link( __( 'Link', 'your-textdomain' ) ),
'media' => new Fieldmanager_Media( __( 'Media', 'your-textdomain' ) ),
'password' => new Fieldmanager_Password( __( 'Password', 'your-textdomain' ) ),
'radios' => new Fieldmanager_Radios( __( 'Radios', 'your-textdomain' ), [ 'options' => [ 'alt1' => __( 'Alternative One', 'your-textdomain' ), 'alt2' => __( 'Alternative Two', 'your-textdomain' ), 'alt3' => __( 'Alternative Three', 'your-textdomain' ) ] ] ),
'richTextArea' => new Fieldmanager_RichTextArea( __( 'RichTextArea', 'your-textdomain' ) ),
'select' => new Fieldmanager_Select( __( 'Select', 'your-textdomain' ), [ 'options' => [ 'alt1' => __( 'Alternative One', 'your-textdomain' ), 'alt2' => __( 'Alternative Two', 'your-textdomain' ), 'alt3' => __( 'Alternative Three', 'your-textdomain' ) ] ] ),
'textarea' => new Fieldmanager_TextArea( __( 'TextArea', 'your-textdomain' ) ),
'textfield' => new Fieldmanager_TextField( __( 'TextField', 'your-textdomain' ) ),
],
] );
foreach ( $flexfields as $flexfield ) {
$flexible_content_select->add_options( [ $flexfield->name => $flexfield->label ] );
$flexfield->display_if = [ 'src' => 'section_type', 'value' => $flexfield->name ];
$fm->add_child( $flexfield );
}
$fm->add_meta_box( __( 'Flexible Content', 'your-textdomain' ), 'post' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment