Skip to content

Instantly share code, notes, and snippets.

@mklasen
Last active March 19, 2018 12:19
Show Gist options
  • Save mklasen/877b544cee7d7cd0c10e5f04e9ac5b3d to your computer and use it in GitHub Desktop.
Save mklasen/877b544cee7d7cd0c10e5f04e9ac5b3d to your computer and use it in GitHub Desktop.
WPH Field Groups
<?php
$image_element = array(
// Adds first tab + tab content
array(
'key' => 'image_tab_1',
'label' => __('Image', 'hortus-field-groups'),
'type' => 'tab',
),
// The actual image
array(
'key' => 'image_upload',
'label' => __('Image', 'hortus-field-groups'),
'name' => 'image',
'type' => 'image',
),
// Adds second tab + tab content
array(
'key' => 'image_tab_2',
'label' => 'Info',
'name' => '',
'type' => 'tab',
),
// Image caption
array(
'key' => 'image_caption',
'label' => __('Caption', 'hortus-field-groups'),
'name' => 'caption',
'type' => 'textarea',
),
// Alternative text
array(
'key' => 'image_alttext',
'label' => __('Alternative text', 'hortus-field-groups'),
'label' => __('Description for visually impaired people', 'hortus-field-groups'),
'name' => 'alt-text',
'type' => 'text',
),
// Image credits
array(
'key' => 'image_credits',
'label' => __('Credits', 'hortus-field-groups'),
'name' => 'credits',
'type' => 'text',
)
);
// Register the image
acf_add_local_field_group(array(
'key' => 'image_element',
'title' => __('Single Image', 'hortus-field-groups'),
'fields' => $image_element,
'position' => 'normal',
'label_placement' => 'top',
'instruction_placement' => 'label',
'active' => 1,
));
<?php
// Register fields for posts
acf_add_local_field_group(array(
'key' => 'wph_post_extra_fields',
'title' => __('Extra fields for posts', 'hortus-field-groups'),
'fields' => array(
array(
'key' => 'wph_design',
'name' => 'wph_design',
'label' => __('Design', 'hortus-field-groups'),
'type' => 'select',
'choices' => array(
'old' => __('Old', 'hortus-field-groups'),
'new' => __('New', 'hortus-field-groups'),
),
'return_format' => 'value',
),
array(
'key' => 'wph_section',
'name' => 'wph_section',
'label' => __('Section', 'hortus-field-groups'),
'type' => 'flexible_content',
'layouts' => array(
array(
'key' => 'wph_paragraph',
'name' => 'wph_paragraph',
'label' => __('Paragraph', 'hortus-field-groups'),
'display' => 'row',
'sub_fields' => array(
array(
'key' => 'wph_paragraph_text',
'label' => __('Paragraph', 'hortus-field-groups'),
'name' => 'paragraph',
'type' => 'wysiwyg',
),
)
),
array(
'key' => 'wph_photo_left',
'name' => 'wph_photo_left',
'label' => __('Photo Left', 'hortus-field-groups'),
'display' => 'table',
'type' => 'clone',
'sub_fields' => array(
array(
'key' => 'wph_photo_left_image',
'name' => 'photo_left_image',
'type' => 'clone',
'clone' => array(
0 => 'image_element',
),
'display' => 'group',
'layout' => 'block',
),
)
),
array(
'key' => 'wph_photo_right',
'name' => 'wph_photo_right',
'label' => __('Photo Right', 'hortus-field-groups'),
'display' => 'table',
'sub_fields' => array(
array(
'key' => 'wph_photo_right_image',
'name' => 'photo_right_image',
'type' => 'clone',
'clone' => array(
0 => 'image_element',
),
'display' => 'group',
'layout' => 'block',
),
)
)
)
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
)
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'seamless',
'label_placement' => 'top',
'instruction_placement' => 'label',
'active' => 1,
));
<?php
defined( 'ABSPATH' ) or die( 'You can\'t access this file directly!');
/**
* Plugin Name: WPH Field Groups
* Description: Adds common fields with ACF
* Version: 0.1
*/
class Hortus_Field_Groups {
public function init() {
$this->hooks();
}
public function hooks() {
add_action('init', array($this, 'load_textdomain'), 10);
add_action('init', array($this, 'load_fields'), 20);
}
public function load_textdomain() {
$load = load_plugin_textdomain( 'hortus-field-groups', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
public function load_fields() {
require_once(plugin_dir_path(__FILE__).'includes/post-types/post.php');
require_once(plugin_dir_path(__FILE__).'includes/elements/image.php');
}
}
$hortus_field_groups = new Hortus_Field_Groups();
$hortus_field_groups->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment