Skip to content

Instantly share code, notes, and snippets.

@amityweb
Created January 15, 2017 11:42
Show Gist options
  • Save amityweb/0fb48ae5e9737b61c299c31e38a62dfc to your computer and use it in GitHub Desktop.
Save amityweb/0fb48ae5e9737b61c299c31e38a62dfc to your computer and use it in GitHub Desktop.
Work in progress of using PHP to create ACF fields in Wordpress. Posting here in an effort to improve and make more efficient and easier to manage
<?php
if( function_exists('acf_add_local_field_group') ):
// Main Content Group
acf_add_local_field_group(array
(
'key' => 'content_1',
'title' => 'Content',
'fields' => array (
// Add Flexible Content Row Array
get_row_array(),
),
// Show only on post type Page
'location' => array (
array (
array (
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
// Hide Main WYSIWYG
'hide_on_screen' => array (
0 => 'the_content',
),
'description' => '',
));
// Column Settings Group
acf_add_local_field_group(array
(
'key' => 'column_settings',
'title' => 'Column Settings',
'fields' => array (
array(
'key' => 'column_widths_1',
'label' => 'Column Width',
'name' => 'column_width',
'type' => 'select',
'choices' => array (
'twelvecol' => '100%',
'ninecol' => '75%',
'sixcol' => '50%',
'threecol' => '25%',
),
'default_value' => 'twelvecol',
),
),
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
// Hide Main WYSIWYG
'hide_on_screen' => array (
0 => 'the_content',
),
'active' => 1,
'description' => '',
));
endif;
/********************
Flexible Content Field
********************/
function get_row_array()
{
$content_array = array (
'layout' => 'block',
'button_label' => '',
'key' => 'row_1',
'label' => 'Row',
'name' => 'row',
'type' => 'repeater',
'sub_fields' => array (
// Row Title for Admin purposes
array (
'key' => 'row_title_1',
'label' => 'Row Title',
'name' => 'row_title',
'type' => 'text',
'instructions' => 'This fields title to show in Admin. Not shown on front end. ',
'wrapper' => array (
'class' => 'collapse-row-title',
),
),
// Flexible Content Bocks
array (
'button_label' => 'Add Column',
'key' => 'field_587a54c91c153',
'label' => 'Content',
'name' => 'content',
'type' => 'flexible_content',
'wrapper' => array (
'width' => '100',
'class' => '',
'id' => '',
),
// Add All Layouts, defined using functions below
'layouts' => array (
rich_text(),
image_row(),
video_image_field(),
slideshow(),
testimonials(),
our_team(),
blog_list(),
services(),
downloads(),
call_to_action(),
featured_product_slider(),
mailchimp_form(),
gallery_slider(),
),
),
// Clone of Row Settings
array (
'clone' => array (
0 => 'group_584d8b74b8fdb',
),
'prefix_label' => 0,
'prefix_name' => 0,
'display' => 'seamless',
'layout' => 'block',
'key' => 'field_587a54ec1c154',
'label' => 'Settings',
'name' => 'settings',
'type' => 'clone',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
),
);
return $content_array;
}
function column_settings($key)
{
$column_settings = array (
'clone' => array (
0 => 'column_settings',
),
'layout' => 'block',
'key' => $key,
'label' => 'Column Settings',
'name' => 'column_settings',
'type' => 'clone',
'display' => 'seamless',
);
return $column_settings;
}
/*****************************************************************
Flexible Content Field Arrays
*****************************************************************/
/********************
Rich Text Field
********************/
function rich_text()
{
$field = array (
'key' => '587a54d687aa4',
'name' => 'rich_text',
'label' => 'Rich text',
'sub_fields' => array (
array (
'key' => 'rich_text_1',
'label' => 'Rich Text',
'name' => 'rich_text',
'type' => 'wysiwyg',
'display' => 'seamless',
),
column_settings('rich_text_column_settings'),
),
);
return $field;
}
/********************
Image Field
********************/
function image_row()
{
$field = array (
'key' => '584962533d669',
'name' => 'image_row',
'label' => 'Image Row',
'display' => 'block',
'sub_fields' => array (
array (
'return_format' => 'array',
'preview_size' => 'large',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'key' => 'field_5849625b3d66a',
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
column_settings('image_row_column_settings'),
),
);
return $field;
}
/********************
Video & Image Field
********************/
function video_image_field()
{
$field = array (
'key' => '584c23cdd334b',
'name' => 'video_image_with_content',
'label' => 'Video/Image with Side Content',
'display' => 'block',
'sub_fields' => array (
array (
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'key' => 'field_5863c5c2e6057',
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '50',
'class' => '',
'id' => '',
),
),
array (
'key' => 'field_58721f91bad46',
'label' => 'Image Link',
'name' => 'image_link',
'type' => 'text',
'instructions' => 'If a link is entered, the image will link to that (same window). This link will not work if a YouTube ID is entered to show a video.',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '50',
'class' => '',
'id' => '',
),
),
array (
'key' => 'field_584c23d8d334c',
'label' => 'YouTube ID',
'name' => 'youtube_id',
'type' => 'text',
'instructions' => 'If you enter a YouTube ID the above image will be linked to that video in a pop up',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'delay' => 0,
'key' => 'field_584c24b87dc27',
'label' => 'Content',
'name' => 'content',
'type' => 'wysiwyg',
),
array (
'layout' => 'horizontal',
'choices' => array (
'left' => 'Left',
'right' => 'Right',
),
'default_value' => 'left',
'other_choice' => 0,
'save_other_choice' => 0,
'allow_null' => 0,
'return_format' => 'value',
'key' => 'field_5863aadf722ae',
'label' => 'Video/Image Left/Right',
'name' => 'video_image_left_right',
'type' => 'radio',
),
array (
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'key' => 'field_5863ab22722af',
'label' => 'Content Background image',
'name' => 'content_background_image',
'type' => 'image',
),
column_settings('video_image_with_content_column_settings'),
),
);
return $field;
}
/********************
Slideshow
********************/
function slideshow()
{
$field = array (
'key' => '584d2ca1866e0',
'name' => 'slideshow',
'label' => 'Slideshow/Image',
'display' => 'block',
'sub_fields' => array (
array (
'min' => 0,
'max' => 0,
'layout' => 'block',
'button_label' => 'Add Row',
'collapsed' => '',
'key' => 'field_584d2ca7866e1',
'label' => 'Images',
'name' => 'images',
'type' => 'repeater',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '100',
'class' => '',
'id' => '',
),
'sub_fields' => array (
array (
'return_format' => 'array',
'preview_size' => 'thumbnail',
'library' => 'all',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'key' => 'field_584d2cb0866e2',
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'default_value' => '',
'delay' => 0,
'key' => 'field_5853b86633f1f',
'label' => 'Text',
'name' => 'text',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'layout' => 'horizontal',
'choices' => array (
'left' => 'Left',
'center' => 'Centre',
'right' => 'Right',
),
'default_value' => 'center',
'other_choice' => 0,
'save_other_choice' => 0,
'allow_null' => 0,
'return_format' => 'value',
'key' => 'field_587a52d4d34da',
'label' => 'Text Position',
'name' => 'text_position',
'type' => 'radio',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
),
),
column_settings('slideshow_column_settings'),
),
);
return $field;
}
/********************
Testimonials Field
********************/
function testimonials()
{
$field = array (
'key' => '584d3221683ce',
'name' => 'testimonials',
'label' => 'Testimonials',
'display' => 'block',
'sub_fields' => array (
array (
'layout' => 'horizontal',
'choices' => array (
'slideshow' => 'Slideshow',
'list' => 'List',
),
'default_value' => 'slideshow',
'other_choice' => 0,
'save_other_choice' => 0,
'allow_null' => 0,
'return_format' => 'value',
'key' => 'field_584dd0a651a94',
'label' => 'Layout',
'name' => 'layout',
'type' => 'radio',
),
column_settings('testimonials_column_settings'),
),
);
return $field;
}
/********************
Our Team Field
********************/
function our_team()
{
$field = array (
'key' => '584d57e0236b7',
'name' => 'our-team',
'label' => 'Our Team',
'display' => 'block',
'sub_fields' => array (
column_settings('our_team_column_settings'),
),
);
return $field;
}
/********************
Services Field
********************/
function services()
{
$field = array (
'key' => '584dd57820ca3',
'name' => 'services',
'label' => 'Services',
'display' => 'block',
'sub_fields' => array (
column_settings('services_column_settings'),
),
);
return $field;
}
/********************
Blog Field
********************/
function blog_list()
{
$field = array (
'key' => 'blog_list_1',
'name' => 'blog_list',
'label' => 'Blog List',
'display' => 'block',
'sub_fields' => array (
column_settings('blog_list_column_settings'),
),
);
return $field;
}
/********************
Events Field
********************/
function events()
{
$field = array (
'key' => '58506463da854',
'name' => 'events',
'label' => 'Events',
'display' => 'block',
'sub_fields' => array (
column_settings('events_column_settings'),
),
);
return $field;
}
/********************
Downloads Field
********************/
function downloads()
{
$field = array (
'key' => '58516b30de666',
'name' => 'downloads',
'label' => 'Downloads',
'display' => 'block',
'sub_fields' => array (
array (
'taxonomy' => 'media_category',
'field_type' => 'select',
'multiple' => 0,
'allow_null' => 0,
'return_format' => 'id',
'add_term' => 1,
'load_terms' => 0,
'save_terms' => 0,
'key' => 'field_5851728ba428d',
'label' => 'Media Category',
'name' => 'media_category',
'type' => 'taxonomy',
),
column_settings('downloads_column_settings'),
),
);
return $field;
}
/********************
Call to Action Field
********************/
function call_to_action()
{
$field = array (
'key' => '585956322a145',
'name' => 'call_to_action',
'label' => 'Call to action',
'display' => 'block',
'sub_fields' => array (
array (
'min' => 0,
'max' => 0,
'layout' => 'table',
'button_label' => '',
'collapsed' => '',
'key' => 'field_585956392a146',
'label' => 'Content',
'name' => 'content',
'type' => 'repeater',
'sub_fields' => array (
array (
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'default_value' => '',
'delay' => 0,
'key' => 'field_5859565d2a147',
'label' => 'Content',
'name' => 'content',
'type' => 'wysiwyg',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
),
),
array (
'multiple' => 0,
'allow_null' => 0,
'choices' => array (
'sixcol' => 'Two',
'fourcol' => 'Three',
'threecol' => 'Four',
'twocol' => 'Six',
),
'default_value' => array (
),
'ui' => 0,
'ajax' => 0,
'placeholder' => '',
'return_format' => 'value',
'key' => 'field_585956742a149',
'label' => 'Columns',
'name' => 'columns',
'type' => 'select',
),
column_settings('call_to_action_column_settings'),
),
);
return $field;
}
/********************
Featured Product Slider Field
********************/
function featured_product_slider()
{
$field = array (
'key' => '58625fa685c68',
'name' => 'featured_product_slider',
'label' => 'Featured Product Slider',
'display' => 'block',
'sub_fields' => array (
column_settings('featured_product_slider_column_settings'),
),
);
return $field;
}
/********************
Mailchimp Form Field
********************/
function mailchimp_form()
{
$field = array (
'key' => '586291384974b',
'name' => 'mailchimp_form',
'label' => 'Mailchimp Form',
'display' => 'block',
'sub_fields' => array (
array (
'post_type' => array (
0 => 'mc4wp-form',
),
'taxonomy' => array (
),
'min' => '',
'max' => '',
'filters' => array (
0 => 'search',
1 => 'post_type',
2 => 'taxonomy',
),
'elements' => '',
'return_format' => 'object',
'key' => 'field_5862913b4974c',
'label' => 'Mailchimp Form',
'name' => 'mailchimp_form',
'type' => 'relationship',
'instructions' => 'Requires plugin https://mc4wp.com/',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
),
array (
'delay' => 0,
'key' => 'field_5862a1669a8f7',
'label' => 'Intro Text',
'name' => 'intro_text',
'type' => 'wysiwyg',
),
column_settings('mailchimp_form_column_settings'),
),
);
return $field;
}
/********************
Gallery Slider Field
********************/
function gallery_slider()
{
$field = array (
'key' => '5863d94a36fff',
'name' => 'gallery_slider',
'label' => 'Gallery Slider',
'display' => 'block',
'sub_fields' => array (
array (
'library' => 'all',
'min' => '',
'max' => '',
'min_width' => '',
'min_height' => '',
'min_size' => '',
'max_width' => '',
'max_height' => '',
'max_size' => '',
'mime_types' => '',
'insert' => 'append',
'key' => 'field_5863d95037000',
'label' => 'Images',
'name' => 'images',
'type' => 'gallery',
),
column_settings('gallery_slider_column_settings'),
),
);
return $field;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment