GitXplorerGitXplorer
d

acfnext

public
20 stars
2 forks
0 issues

Commits

List of commits on branch master.
Unverified
cb073d386612a7bd172caa0a6dd4cd885dba8be9

Fix parentheses on defaults

ddanielpost committed 8 years ago
Unverified
eb6633c9a4fc4b04b97a0ed40d76da430f9ad71c

Cleaner defaults and key generation

ddanielpost committed 8 years ago
Unverified
0d8496f9e8e29e03a852fea0c233b8438e59cf96

Tiny modification

ddanielpost committed 8 years ago
Unverified
3668120f1ce6b62474849468e0942e8894544435

Update README

ddanielpost committed 8 years ago
Unverified
6b915004e524e8b8a436616fe7848afb3c2ec9ea

Add $key variable

ddanielpost committed 8 years ago
Unverified
2d47695e408776ff7437b01eb3fc8d93d9e3696d

Add option for easier location rules

ddanielpost committed 8 years ago

README

The README file for this repository.

ACF PHP

A framework to register Advanced Custom Fields fields using PHP.

Why use ACF PHP?

ACF PHP makes it easier to register ACF fields using PHP by automating the tedious parts of adding new fields and adding defaults that make sense.

Without ACF PHP:

array (
	'key' => 'field_random_key',
	'label' => 'Header Title',
	'name' => 'header_title',
	'type' => 'text',
),
array (
	'key' => 'field_another_random_key',
	'label' => 'Header Subtitle',
	'name' => 'header_subtitle',
	'type' => 'text',
),
array (
	'key' => 'field_yet_another_random_key',
	'label' => 'Header Background Image',
	'name' => 'header_bg_image',
	'type' => 'image',
)
'location' => array(
	array(
		array(
			'param' => 'post_type',
			'operator' => '==',
			'value' => 'page',
		),
	),
),

With ACF PHP:

'header_title',
'header_subtitle'
'header_bg_image' => array(
	'label' => 'Header Background Image',
	'type' => 'image',
)
'location' => 'post_type == page',

Features

  • Automatically generates a unique key for each field using the field group key and field name.
  • Adds a label automatically if you don’t add one using the field name, and adds a default field type (example: header_title becomes a Text field with label Header Title).
  • Easily reuse fields by putting them in a variable.
  • Options to hide the "Custom Fields" menu (including redirects when accessed directly) and to disable ACF on the front end (useful when using native WordPress function to retrieve post meta, eg. get_post_meta()).
  • All ACF fields are supported, including Repeaters and Flexible Content.

Usage

Download the .zip file from GitHub to install in your WordPress admin.

All options for ACF fields are supported. For more information, please check out the official Advanced Custom Fields website.

Basic example:

Setting up the metabox:

$page_settings_metabox = array(
	'key' => 'page_settings', // make sure the key is unique for each group
	'title' => 'Page Settings',
	'location' => array(
		array(
			array(
				'param' => 'post_type',
				'operator' => '==',
				'value' => 'page',
			),
		),
	),
	'fields' => array(
		'text' => array(
			'required' => 1,
		),
		'textarea' => array(
			'type' => 'textarea',
			'rows' => 5,
			'instructions' => 'Add optional field instructions.',
		),
	),
);

Register the metabox:

if ( function_exists( 'acf_php_add_local_field_group' ) ) {
	acf_php_add_local_field_group( $page_settings_metabox );
}

Retrieve the meta values:

$text_content = get_post_meta( get_the_ID(), 'text', true );
$textarea_content = get_post_meta( get_the_ID(), 'textarea', true );

Alternatively, use standard ACF functions.

Repeater example:

'fields' => array(
	'repeater' => array(
		'type' => 'repeater',
		'button_label' => 'Add Row',
		'sub_fields' => array(
			'text' => array(
				'required' => 1,
			),
			'textarea' => array(
				'type' => 'textarea',
				'rows' => 5,
				'instructions' => 'Add optional field instructions.',
			),
		),
	),
),

Flexible Content example:

'fields' => array(
	'flexible_content' => array(
		'type' => 'flexible_content',
		'button_label' => 'Add Section',
		'layouts' => array(
			'example_layout' => array(
				'sub_fields' => array(
					'text_field',
					'true_false_field' => array(
						'type' => 'true_false',
					),
				),
			),
		),
	),
),

Reusable field example:

$checkbox = array(
	'type' => 'checkbox',
	'choices' => array(
		'red' => 'Red',
		'white' => 'White',
		'blue' => 'Blue',
	),
);

'fields' => array(
	'checkbox_field' => $checkbox,
),

To-do

  • Improve ACF_PHP_Metabox class (leaner code).
  • Add support for multiple fields in a single variable.
  • Improve field group location settings.