Skip to main content

Custom Fields

WordPress has only one content field. When editors need more input fields, add custom fields with Secure Custom Fields (SCF, compatible with Advanced Custom Fields).

Don't create field groups on the admin screens. Define one field group per file in settings/fields/. Each file returns the array passed to acf_add_local_field_group(), and nothing is stored in the database.

// settings/fields/company.php
return [
'key' => 'group_company',
'title' => '会社情報',
'fields' => [
[
'key' => 'field_company_tel',
'label' => '電話番号',
'name' => 'company_tel',
'type' => 'text',
],
],
'location' => [
[['param' => 'post_type', 'operator' => '==', 'value' => 'page']],
],
];
  • key must be unique across the site. Start group keys with group_ and field keys with field_.
  • The samples are settings/fields/seo.php (the SEO fields) and settings/fields/case.php (text, select, date, URL, repeater and gallery fields).
  • On production (WP_ENVIRONMENT_TYPE is production), the field group admin screens are hidden.

In templates​

Read the values with _get_field() and print them with _the_field(). They work like get_field() / the_field(), but return nothing instead of failing when the plugin is inactive.

<?php $tel = _get_field('company_tel'); ?>
<?php if ($tel): ?>
<p>TEL <?php echo esc_html($tel); ?></p>
<?php endif; ?>

Always escape the output. See Additional functions for the other helpers.