ACF Snippets for Visual Studio CodeA comprehensive collection of 85+ snippets for Advanced Custom Fields (ACF) development in WordPress. ✨ Features
📦 Installation
Or search for "ACF Snippets" in the Extensions sidebar. 🚀 UsageTemplate snippets follow the naming convention Type the prefix and press 📋 Available SnippetsBasic Fields (
|
| Prefix | Description |
|---|---|
field |
Display ACF field (the_field escapes unsafe HTML since ACF 6.2.7) |
field:text |
Display ACF Text field wrapped in <p> |
field:textarea |
Display ACF Text Area field (New Lines: No Formatting) |
field:header |
Display ACF field as a heading (h1-h6) |
field:number |
Display ACF Number field (localized format, 0 is shown) |
field:range |
Display ACF Range field as <progress> |
field:email |
Display ACF Email field as obfuscated mailto: link |
field:date |
Display ACF Date Picker field (independent of Return Format) |
field:datetime |
Display ACF Date Time Picker field |
field:time |
Display ACF Time Picker field (uses Return Format) |
field:if |
ACF conditional: if field has a value |
field:ifelse |
ACF conditional: if / else |
field:true_false |
ACF True / False field |
field:sub |
Display ACF sub field (inside have_rows loop) |
field:option |
Display ACF field from an options page |
field:post |
Display ACF field from a specific post |
field:term |
Display ACF field from a taxonomy term (WP_Term or 'term_' . $id) |
field:user_meta |
Display ACF field from a user profile |
field:link |
ACF Link field (Return Format: URL) |
field:link:array |
ACF Link field (Return Format: Array) |
field:url |
ACF URL field |
field:page_link |
ACF Page Link field |
field:page_link:multiple |
ACF Page Link field (multiple values) |
field:wysiwyg |
ACF WYSIWYG Editor field |
field:select |
ACF Select field (Return Format: Value or Label) |
field:select:array |
ACF Select / Radio / Button Group field (Return Format: Both) |
field:select:object |
ACF Select field – get label via get_field_object() |
field:checkbox |
ACF Checkbox field |
field:radio |
ACF Radio Button field |
field:button_group |
ACF Button Group field used as a CSS modifier |
field:color_picker |
ACF Color Picker field (Return Format: Hex string) |
field:icon_picker |
ACF Icon Picker field (ACF 6.3+) |
field:group |
ACF Group field (array access) |
field:group:loop |
ACF Group field (have_rows / get_sub_field) |
Image & Media Fields (image.code-snippets)
| Prefix | Description |
|---|---|
field:image |
ACF Image field (Return Format: URL) |
field:image:id |
ACF Image field (Return Format: ID) – responsive srcset |
field:image:array |
ACF Image field (Return Format: Array) |
field:image:array:example |
ACF Image field (Return Format: Array) – full example with caption |
field:gallery |
ACF Gallery field (Return Format: ID) |
field:gallery:captions |
ACF Gallery field with captions (Return Format: Array) |
field:file |
ACF File field (Return Format: URL) |
field:file:id |
ACF File field (Return Format: ID) |
field:file:object |
ACF File field (Return Format: Array) |
field:oembed |
ACF oEmbed field |
Repeater Fields (repeater.code-snippets)
| Prefix | Description |
|---|---|
field:repeater |
ACF Repeater field loop |
field:repeater:index |
ACF Repeater field loop with get_row_index() |
field:repeater:option |
ACF Repeater field loop from an options page |
field:repeater:grid |
ACF Repeater field as grid (chunked rows) |
field:repeater:slider |
ACF Repeater field as slider (Image Return Format: ID) |
field:repeater:foreach |
ACF Repeater field with foreach loop |
field:repeater:nested |
ACF nested Repeater fields |
field:repeater:first |
ACF Repeater field – get first row |
Flexible Content (flexible.code-snippets)
| Prefix | Description |
|---|---|
field:flex |
ACF Flexible Content field |
field:flex:nested |
ACF Flexible Content with nested Repeater |
field:flex:switch |
ACF Flexible Content with multiple layouts |
field:flex:template |
ACF Flexible Content – load a template part per layout |
Relational, Forms & Debug (misc.code-snippets)
| Prefix | Description |
|---|---|
ddfield |
Debug: dump ACF field and die |
field:form |
ACF Gravity Forms field |
field:cf7 |
ACF field with Contact Form 7 ID |
field:query |
WP_Query by ACF field value (meta_query) |
field:relationship |
ACF Relationship field (Return Format: Post Object) |
field:relationship:ids |
ACF Relationship / Post Object (multiple) field (Return Format: ID) |
field:post_object |
ACF Post Object field (single) |
field:user |
ACF User field (Return Format: Array) |
field:taxonomy |
ACF Taxonomy field (multiple, Return Format: Term Object) |
field:taxonomy:single |
ACF Taxonomy field (single, Return Format: Term Object) |
field:map |
ACF Google Map field – single marker |
ACF Blocks (blocks.code-snippets, block-json.code-snippets)
| Prefix | Description |
|---|---|
acf:block:register |
Register all ACF blocks from /blocks/*/block.json (place in functions.php) |
acf:block:template |
ACF Block render template (render.php) with get_block_wrapper_attributes() |
acf:block:innerblocks |
ACF Block InnerBlocks (one per block, inside render template) |
acf:register_block:legacy |
Legacy ACF block registration with acf_register_block_type() |
acf:block:json |
ACF Block block.json (Blocks v3, ACF PRO 6.6+) |
acf:block:jsonis available in JSON files (block.json); the other snippets work in PHP/HTML files.
Registration, Hooks & API (api.code-snippets)
These snippets contain plain PHP (no
<?phptag) – paste them insidefunctions.phpor a plugin file.
| Prefix | Description |
|---|---|
acf:options_page |
Register ACF options page (on acf/init) |
acf:options_subpage |
Register ACF options sub page |
acf:local_field_group |
Register ACF local field group in PHP (acf/include_fields) |
acf:get_fields |
Loop over all ACF fields of a post with get_fields() |
acf:dump |
Debug: print all ACF fields (admins only, no die) |
acf:update_field |
Update an ACF field value (use the field key for new posts) |
acf:add_row |
Add a row to an ACF Repeater / Flexible Content field |
acf:form |
ACF front-end form (acf_form_head + acf_form) |
acf:save_post |
ACF acf/save_post action |
acf:load_field |
ACF acf/load_field filter (e.g. dynamic choices) |
acf:format_value |
ACF acf/format_value filter |
acf:validate_value |
ACF acf/validate_value filter |
acf:json |
ACF Local JSON save/load path |
acf:google_map_api |
ACF Google Maps API key |
📖 Examples
Basic Field
<?php if ( get_field( 'subtitle' ) ) : ?>
<p><?php echo esc_html( get_field( 'subtitle' ) ); ?></p>
<?php endif; ?>
Image Field (ID – recommended)
<?php
$image_id = get_field( 'hero_image' );
if ( $image_id ) :
echo wp_get_attachment_image( $image_id, 'large', false, array( 'class' => 'image' ) );
endif;
?>
Repeater Field
<?php if ( have_rows( 'team_members' ) ) : ?>
<?php while ( have_rows( 'team_members' ) ) : the_row(); ?>
<?php the_sub_field( 'name' ); ?>
<?php endwhile; ?>
<?php endif; ?>
ACF Block (block.json, Blocks v3)
blocks/testimonial/block.json (acf:block:json):
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "acf/testimonial",
"title": "Testimonial",
"category": "text",
"icon": "admin-comments",
"acf": {
"blockVersion": 3,
"renderTemplate": "render.php"
},
"supports": { "anchor": true, "align": [ "wide", "full" ], "html": false }
}
functions.php (acf:block:register):
function theme_register_acf_blocks() {
foreach ( glob( get_template_directory() . '/blocks/*/block.json' ) as $block_json ) {
register_block_type( dirname( $block_json ) );
}
}
add_action( 'init', 'theme_register_acf_blocks' );
blocks/testimonial/render.php (acf:block:template):
<?php
$title = get_field( 'title' );
$wrapper_attributes = $is_preview ? ' ' : get_block_wrapper_attributes( array( 'class' => 'testimonial' ) );
?>
<div <?php echo $wrapper_attributes; ?>>
<h2><?php echo esc_html( $title ); ?></h2>
</div>
🔒 Escaping in ACF 6.2.7+
Since ACF 6.2.7, the_field() and the_sub_field() strip unsafe HTML (such as <script> and <iframe>) from the output. To stay predictable, the snippets:
- use
echo esc_html( get_field() )for plain text values; - use
echo wp_kses_post( get_field() )for WYSIWYG content; - echo oEmbed HTML from
get_field()directly, because WordPress generates it (field:oembed).
📁 File Structure
snippets/
├── basic.code-snippets # Basic field types
├── image.code-snippets # Images, galleries, files, oEmbed
├── repeater.code-snippets # Repeater variations
├── flexible.code-snippets # Flexible content
├── misc.code-snippets # Relational fields, forms, queries, debug
├── blocks.code-snippets # ACF Blocks (PHP)
├── block-json.code-snippets # ACF Blocks block.json (JSON)
└── api.code-snippets # Options pages, local fields, hooks & filters
scripts/
└── validate-snippets.js # npm run validate / npm run lint
🔧 Requirements
- VS Code 1.85.0 or higher
- ACF 6.3+ (
field:icon_pickerrequires 6.3,acf:block:jsonwithblockVersion: 3requires ACF PRO 6.6+) - WordPress 6.0+
ACF PRO Required For:
- Repeater fields
- Flexible Content
- Gallery field
- Options pages
- ACF Blocks
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/new-snippet) - Commit your changes (
git commit -m 'Add new snippet') - Push to the branch (
git push origin feature/new-snippet) - Run
npm run lint(requires PHP CLI) – it checks JSON, escaping of$and runsphp -lon every snippet - Open a Pull Request
📝 Changelog
See CHANGELOG.md for a list of changes.
👥 Contributors
| Anthony Hubble | Michael Mano | Neil (Ne Ne) | Vadym Mishchenko |
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
⭐ Support
If you find this extension helpful, please consider:
- ⭐ Starring the GitHub repository
- 📝 Leaving a review on the VS Code Marketplace
- 🐛 Reporting issues on GitHub