Skip to content

Instantly share code, notes, and snippets.

@danielpost
Last active May 6, 2020 15:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danielpost/71548a016a7bd7c22aa762a4383eb9e7 to your computer and use it in GitHub Desktop.
Save danielpost/71548a016a7bd7c22aa762a4383eb9e7 to your computer and use it in GitHub Desktop.
ACF Wrapper - easier way to register ACF fields using PHP
<?php
$reusable_checkbox_field = array(
'type' => 'checkbox',
'choices' => array(
'red' => __('Red', THEME_SLUG),
'white' => __('White', THEME_SLUG),
'blue' => __('Blue', THEME_SLUG),
),
);
$additional_settings_meta_box = array(
'key' => 'additional_settings', // make sure the key is unique for each group
'title' => __('Additional Settings', THEME_SLUG),
'menu_order' => 1,
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
'fields' => array(
'standard_fields' => array(
'type' => 'tab',
),
'text' => array(
'required' => 1,
),
'text_field_with_automatic_label',
'message_field' => array(
'type' => 'message',
'esc_html' => 0,
'label' => __('Some extra info', THEME_SLUG),
'message' => __('This is an example of a message field. You can use it to insert <strong>extra information</strong>.', THEME_SLUG),
),
'textarea' => array(
'type' => 'textarea',
'rows' => 5,
'instructions' => __('This field is for testing purposes.', THEME_SLUG),
),
'number' => array(
'type' => 'number',
),
'url' => array(
'type' => 'url',
'label' => __('URL', THEME_SLUG),
),
'email' => array(
'type' => 'email',
),
'password' => array(
'type' => 'password',
),
'content_fields' => array(
'type' => 'tab',
),
'wysiwyg' => array(
'type' => 'wysiwyg',
'label' => __('WYSIWYG', THEME_SLUG),
),
'oembed' => array(
'type' => 'oembed',
'label' => __('oEmbed', THEME_SLUG),
),
'image' => array(
'type' => 'image',
),
'file' => array(
'type' => 'file',
),
'gallery' => array(
'type' => 'gallery',
),
'select_fields' => array(
'type' => 'tab',
),
'select' => array(
'type' => 'select',
'choices' => array(
'red' => __('Red', THEME_SLUG),
'white' => __('White', THEME_SLUG),
'blue' => __('Blue', THEME_SLUG),
),
),
'select_with_select2' => array(
'type' => 'select',
'ui' => 1,
'choices' => array(
'red' => __('Red', THEME_SLUG),
'white' => __('White', THEME_SLUG),
'blue' => __('Blue', THEME_SLUG),
),
),
'checkbox' => $reusable_checkbox_field,
'radio' => array(
'type' => 'radio',
'choices' => array(
'red' => __('Red', THEME_SLUG),
'white' => __('White', THEME_SLUG),
'blue' => __('Blue', THEME_SLUG),
),
),
'true_false' => array(
'type' => 'true_false',
'label' => __('True or false', THEME_SLUG),
'message' => __('Check for true, uncheck for false', THEME_SLUG),
),
'relational_fields' => array(
'type' => 'tab',
),
'post_object' => array(
'type' => 'post_object',
),
'page_link' => array(
'type' => 'page_link',
),
'relationship' => array(
'type' => 'relationship',
),
'taxonomy' => array(
'type' => 'taxonomy',
),
'user' => array(
'type' => 'user',
),
'jquery_fields' => array(
'type' => 'tab',
'label' => __('jQuery Fields', THEME_SLUG),
),
'google_map' => array(
'type' => 'google_map',
'center_lat' => '52.373668',
'center_lng' => '4.886844',
),
'date_picker' => array(
'type' => 'date_picker',
),
'date_time_picker' => array(
'type' => 'date_time_picker',
),
'time_picker' => array(
'type' => 'time_picker',
),
'color_picker' => array(
'type' => 'color_picker',
),
'advanced_fields' => array(
'type' => 'tab',
),
'repeater' => array(
'type' => 'repeater',
'layout' => 'block',
'button_label' => __('Add Row', THEME_SLUG),
'sub_fields' => array(
'name' => array(
'instructions' => __('Enter the name of the employee.', THEME_SLUG),
'wrapper' => array(
'width' => '50',
),
),
'manager' => array(
'type' => 'true_false',
'label' => __('Manager?', THEME_SLUG),
'instructions' => __('Check if the employee is a manager', THEME_SLUG),
'wrapper' => array(
'width' => '50',
),
),
),
),
'flexible_content' => array(
'type' => 'flexible_content',
'button_label' => __('Add Section', THEME_SLUG),
'layouts' => array(
'layout_1' => array(
'sub_fields' => array(
'text_field' => array(
'instructions' => __('This field is for testing purposes.', THEME_SLUG),
'wrapper' => array(
'width' => '60',
),
),
'true_false_field' => array(
'type' => 'true_false',
'message' => __('Check or not', THEME_SLUG),
'wrapper' => array(
'width' => '40',
),
),
),
),
'layout_2' => array(
'sub_fields' => array(
'text_field',
'image_field' => array(
'type' => 'image',
)
),
),
),
),
),
);
new Meta_Box($additional_settings_meta_box);
<?php
/*
* Disable ACF on frontend since we use get_post_meta instead of ACF functions for improved performance.
*/
function vo_disable_acf_on_frontend( $plugins ) {
if ( is_admin() ) {
return $plugins;
}
foreach( $plugins as $i => $plugin ) {
if ( 'advanced-custom-fields-pro/acf.php' == $plugin ) {
unset( $plugins[$i] );
}
}
return $plugins;
}
add_filter( 'option_active_plugins', 'vo_disable_acf_on_frontend' );
/*
* Hide ACF menu in admin
*/
add_filter('acf/settings/show_admin', '__return_false');
/*
* Redirect ACF pages to Admin dashboard
*/
function vo_acf_redirect() {
if ( ! is_admin() ) {
return '';
}
if ( isset( $_GET['post_type' ]) ) {
global $pagenow;
if ( in_array( $pagenow, array('edit.php', 'post-new.php') ) && ( $_GET['post_type'] == 'acf-field-group' ) ) {
wp_redirect( admin_url() );
die();
}
}
}
add_action('init','vo_acf_redirect');
<?php
class Meta_Box {
/**
* @var array
*/
protected $options = array();
/**
* Meta_Box constructor.
* @param array $options
*/
public function __construct( array $options = array() ) {
if ( ! is_admin() || ! function_exists('acf_add_local_field_group') ) {
return;
}
if ( ! empty( $options['key'] ) && ! empty( $options['title'] ) ) {
$this->set_options( $options );
$this->prepare_fields();
$this->register();
}
}
/**
* @param $options
*/
public function set_options( $options ) {
$this->options = $options;
}
/**
* @return array
*/
public function get_options() {
return $this->options;
}
/**
* @param $fields
*/
public function set_fields( $fields ) {
$this->options['fields'] = $fields;
}
/**
* @return mixed
*/
public function get_fields() {
return $this->options['fields'];
}
/**
* Set default options before the meta boxes are created
*/
protected function set_default_options() {
$options = $this->get_options();
// Set default options
$options = array_merge( array(
'label_placement' => 'left',
'active' => 1,
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
)
),
'fields' => array()
), $options );
$this->set_options( $options );
}
/**
* Convert field (add key, name and optional settings to fields)
*/
protected function convert_field( $field_id, $field, $parent_id = null, $layout_id = null, $is_layout = false ) {
$options = $this->get_options();
// Set defaults if no options are given
if ( is_int( $field_id ) && is_string( $field ) ) {
$field_id = $field;
$field = array();
}
// Check if the field is a sub field (repeater or flexible content field)
if ( isset( $parent_id ) && $parent_id ) {
$field['name'] = $field_id;
// Check if the field is a sub field of a flexible content layout
if ( isset( $layout_id ) && $layout_id ) {
$field['key'] = 'field_vo_' . $options['key'] . '_' . $parent_id . '_' . $layout_id . '_' . $field_id;
} else {
$field['key'] = 'field_vo_' . $options['key'] . '_' . $parent_id . '_' . $field_id;
}
} else {
$field['name'] = 'vo_' . $field_id;
$field['key'] = 'field_vo_' . $options['key'] . '_' . $field_id;
}
// If it's a tab with no placement setting, set default placement
if ( $field['type'] === 'tab' && ! isset( $field['placement'] ) ) {
$field['placement'] = 'left';
}
// Set defaults
if( $is_layout ) {
$field = array_merge( array(
'display' => 'row',
), $field );
} else {
$field = array_merge( array(
'type' => 'text',
), $field );
}
$field = array_merge( array(
'label' => ucwords( str_replace( '_', ' ', $field_id ) ),
), $field );
return $field;
}
/**
* Prepare fields
*/
public function prepare_fields() {
$this->set_default_options();
$options = $this->get_options();
$fields = $this->get_fields();
// Add field name and key
foreach ( $fields as $field_id => &$field ) {
if ( isset( $field['sub_fields'] ) && is_array( $field['sub_fields'] ) ) {
$sub_fields = $field['sub_fields'];
foreach ( $sub_fields as $sub_field_id => &$sub_field ) {
$sub_field = $this->convert_field( $sub_field_id, $sub_field, $field_id );
}
$field['sub_fields'] = $sub_fields;
}
if ( isset( $field['layouts'] ) && is_array( $field['layouts'] ) ) {
$layouts = $field['layouts'];
foreach ( $layouts as $layout_id => &$layout ) {
if ( isset( $layouts[ $layout_id ]['sub_fields'] ) && is_array( $layouts[ $layout_id ]['sub_fields'] ) ) {
$layout_sub_fields = $layouts[ $layout_id ]['sub_fields'];
foreach ( $layout_sub_fields as $layout_sub_field_id => &$layout_sub_field ) {
$layout_sub_field = $this->convert_field( $layout_sub_field_id, $layout_sub_field, $field_id, $layout_id );
}
$layouts[ $layout_id ]['sub_fields'] = $layout_sub_fields;
}
$layout = $this->convert_field( $layout_id, $layout, $field_id, null, true );
}
$field['layouts'] = $layouts;
}
$field = $this->convert_field( $field_id, $field );
}
$this->set_fields( $fields );
}
public function register() {
$options = $this->get_options();
acf_add_local_field_group( $options );
}
}
<?php
$repeater = (int) get_post_meta( get_the_ID(), 'vo_repeater_example', true );
$rows = get_post_meta( get_the_ID(), 'vo_builder', true );
if ( isset( $repeater ) && $repeater ) :
echo '<h1>' . __( 'Repeater Example', THEME_SLUG ) . '</h1>';
echo '<ul>';
for ( $i = 0; $i < $repeater; $i++ ) :
$content = get_post_meta( get_the_ID(), 'vo_repeater_example_' . $i . '_content', true );
if ( isset( $content ) && $content ) {
echo '<li>' . esc_html( $content ) . '</li>';
}
endfor;
echo '</ul>';
endif;
if ( isset( $rows ) && $rows ) :
echo '<h1>' . __( 'Flexible Content Example', THEME_SLUG ) . '</h1>';
foreach ( (array) $rows as $count => $row ) :
switch ( $row ) :
// Text Block
case 'text_block':
$text_field = get_post_meta( get_the_ID(), 'vo_builder_' . $count . '_text_field', true );
$content = (int) get_post_meta( get_the_ID(), 'vo_builder_' . $count . '_true_false_field', true );
if ( isset( $text_field ) && $text_field ) {
echo '<h3>' . esc_html( $text_field ) . '</h3>';
}
if ( isset( $content ) && 1 === $content ) {
echo '<h3>' . __( 'True!', THEME_SLUG ) . '</h3>';
} else {
echo '<h3>' . __( 'False...', THEME_SLUG ) . '</h3>';
}
break;
// Image Block
case 'image_block':
$text_field = get_post_meta( get_the_ID(), 'vo_builder_' . $count . '_text_field', true );
$image_id = (int) get_post_meta( get_the_ID(), 'vo_builder_' . $count . '_image_field', true );
$image = wp_get_attachment_image( $image_id, 'full' );
if ( isset( $text_field ) && $text_field ) {
echo '<h3>' . esc_html( $text_field ) . '</h3>';
}
if ( isset( $image ) && $image ) {
echo $image;
}
break;
// Video Block
case 'video_block':
$video_title = get_post_meta( get_the_ID(), 'vo_builder_' . $count . '_video_title', true );
$video = get_post_meta( get_the_ID(), 'vo_builder_' . $count . '_video_field', true );
if ( isset( $video_title ) && $video_title ) {
echo '<h3>' . esc_html( $video_title ) . '</h3>';
}
if ( isset( $video ) && $video ) {
echo wp_oembed_get( esc_url( $video ) );
}
break;
endswitch;
endforeach;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment