Advanced Custom Fields (ACF) Shortcode

Below, you’ll find a detailed guide on how to add the Advanced Custom Fields (ACF) Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Advanced Custom Fields (ACF) Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Advanced Custom Fields (ACF) Plugin and the shortcodes it provides:

Plugin Icon
Advanced Custom Fields (ACF)

"Advanced Custom Fields (ACF) is an essential plugin for WordPress that allows you to customize your website by adding and managing various custom fields. It is a must-have for a personalized website experience."

★★★★☆ (1192) Active Installs: 2000000+ Tested with: 6.3.2 PHP Version: 7.0
Included Shortcodes:
  • [acf]

Advanced Custom Fields (ACF) [acf] Shortcode

The Advanced Custom Fields shortcode is a powerful tool that allows you to display custom field data. It works by retrieving the field value based on the ‘field’ and ‘post_id’ attributes. This shortcode is also equipped with security measures to prevent unauthorized users from previewing or manipulating data. It restricts AJAX requests and limits data previews to users with ‘publish_posts’ permissions. Shortcode: [acf] Furthermore, it includes a filter to prevent access to unknown fields, ensuring data integrity and security. If the field value is an array, it converts it into a string for display. Shortcode: [acf]

Shortcode: [acf]

Parameters

Here is a list of all possible acf shortcode parameters and attributes:

  • field – Specifies the field name from which data is fetched.
  • post_id – Identifies the post to fetch the field data from.
  • format_value – Controls whether to format the field value or not.

Examples and Usage

Basic example – Displaying a field value by using the field name as the attribute.

[acf field="my_field_name"]

Advanced examples:

Displaying a field value by using the field name and post ID as the attributes. This will fetch the field value from a specific post.

[acf field="my_field_name" post_id="123"]

Displaying a field value by using the field name, post ID, and format_value attributes. The format_value attribute is used to control whether the value returned by the shortcode should be formatted or not. If set to true, the value will be formatted based on the field type. If set to false, the raw value stored in the database will be returned.

[acf field="my_field_name" post_id="123" format_value="true"]

PHP Function Code

In case you have difficulties debugging what causing issues with [acf] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'acf', 'acf_shortcode' );

Shortcode PHP function:

function acf_shortcode( $atts ) {
	// Return if the ACF shortcode is disabled.
	if ( ! acf_get_setting( 'enable_shortcode' ) ) {
		return;
	}

	if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
		// Prevent the ACF shortcode in FSE block template parts by default.
		if ( ! doing_filter( 'the_content' ) && ! apply_filters( 'acf/shortcode/allow_in_block_themes_outside_content', false ) ) {
			return;
		}
	}

	// Limit previews of ACF shortcode data for users without publish_posts permissions.
	$preview_capability = apply_filters( 'acf/shortcode/preview_capability', 'publish_posts' );
	if ( is_preview() && ! current_user_can( $preview_capability ) ) {
		return apply_filters( 'acf/shortcode/preview_capability_message', __( '[ACF shortcode value disabled for preview]', 'acf' ) );
	}

	// Mitigate issue where some AJAX requests can return ACF field data.
	$ajax_capability = apply_filters( 'acf/ajax/shortcode_capability', 'edit_posts' );
	if ( wp_doing_ajax() && ( $ajax_capability !== false ) && ! current_user_can( $ajax_capability ) ) {
		return;
	}

	$atts = shortcode_atts(
		array(
			'field'        => '',
			'post_id'      => false,
			'format_value' => true,
		),
		$atts,
		'acf'
	);

	$access_already_prevented = apply_filters( 'acf/prevent_access_to_unknown_fields', false );
	$filter_applied           = false;

	if ( ! $access_already_prevented ) {
		$filter_applied = true;
		add_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
	}

	// Try to get the field value.
	$value = get_field( $atts['field'], $atts['post_id'], $atts['format_value'] );

	if ( $filter_applied ) {
		remove_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
	}

	if ( is_array( $value ) ) {
		$value = implode( ', ', $value );
	}

	return $value;
}

Code file location:

advanced-custom-fields/advanced-custom-fields/includes/api/api-template.php

Conclusion

Now that you’ve learned how to embed the Advanced Custom Fields (ACF) Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *