Skip to content

Functions and values ​

Function names ​

Every function has two names:

  • wpff_ prefix: wpff_get_field(), wpff_have_rows(), and so on. These are always available.
  • ACF names: get_field(), have_rows(), and so on. These are defined only when ACF itself is not active.

To turn the ACF names off, use define( 'WPFF_ACF_COMPAT', false ); or the wpff/acf_compat filter.

The $post_id argument ​

$post_id accepts ACF's forms:

  • a post ID;
  • 'term_5' (or 'category_5');
  • 'user_3';
  • 'comment_7';
  • 'option' / 'options';
  • an options page storage key such as 'site_settings';
  • a WP_Post, WP_Term or WP_User object.

Without the argument, functions use the current post. Inside a block template, they use the current block.

Registering from code ​

php
add_action( 'wpff/init', function () {
	wpff_add_local_field_group( array(
		'key'      => 'group_book',
		'title'    => 'Book',
		'fields'   => array(
			array( 'key' => 'field_book_isbn', 'name' => 'isbn', 'label' => 'ISBN', 'type' => 'text', 'required' => 1 ),
			array(
				'key'           => 'field_book_chapters',
				'name'          => 'chapters',
				'label'         => 'Chapters',
				'type'          => 'repeater',
				'pagination'    => 1,
				'rows_per_page' => 20,
				'sub_fields'    => array(
					array( 'key' => 'field_book_ch_title', 'name' => 'title', 'label' => 'Title', 'type' => 'text' ),
				),
			),
		),
		'location' => array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'book' ) ) ),
	) );

	wpff_add_options_page( array( 'page_title' => 'Theme settings', 'menu_slug' => 'theme-settings' ) );

	wpff_register_block_type( array(
		'name'            => 'hero',
		'title'           => 'Hero',
		'render_template' => get_template_directory() . '/blocks/hero.php',
	) );
} );

Field keys. You can leave out a field's key: a stable key is then generated from the group key and the field name. If the field already has data saved by ACF, give it the old key.

ACF format. Field groups use the same format as ACF exports, so PHP code from ACF's "Tools → Generate PHP" works as it is.

Reading and writing values ​

FunctionWhat it does
get_field( $name, $post_id = false, $format = true )Returns the value formatted by the field's return format.
the_field( $name, $post_id )Prints the value, escaped with wp_kses_post().
get_field_object( $name, $post_id )Returns the field settings plus its value.
get_fields( $post_id )Returns all values of an object as name => value.
update_field( $name_or_key, $value, $post_id )Writes a value. For a repeater, pass a list of rows: [ [ 'sub' => … ], … ]. For flexible content, give each row an acf_fc_layout.
delete_field( $name, $post_id )Deletes the value, including sub values in rows.

Storage ​

Values are stored as ACF stores them:

WhatStorage
A field{name}, plus _{name} holding the field key
Group sub fields{group}_{sub}
Repeater rows{name} holds the number of rows (for flexible content, the list of layouts); each value is {name}_{i}_{sub}
Options pagesoptions_{name} in wp_options

Row loops ​

php
while ( have_rows( 'sections' ) ) : the_row();
	if ( 'gallery' === get_row_layout() ) {
		while ( have_rows( 'slides' ) ) : the_row();   // nested repeater
			echo esc_html( get_sub_field( 'caption' ) );
		endwhile;
	}
	echo get_row_index(); // 1, 2, …
endwhile;

Other row functions (row numbers start at 1):

  • the_sub_field(), get_sub_field_object();
  • get_row( $format ), reset_rows();
  • add_row( $name, $row, $post_id );
  • update_row( $name, $i, $row, $post_id );
  • delete_row( $name, $i, $post_id ).

Settings ​

wpff_update_setting( $name, $value ) and wpff_get_setting( $name, $default ) hold settings for the current request:

  • call wpff_update_setting() from your theme or plugin on every load;
  • a setting can be changed with the wpff/settings/{name} filter;
  • without ACF, acf_update_setting() and acf_get_setting() are available too.

GPL-2.0-or-later. Not affiliated with ACF or WP Engine.