12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Jul 9, 2019

[Condition] ACF Field Not Empty

Sridhar Katakam

Update: This is no longer necessary since it is now built into Oxygen.

Use Dynamic Data condition. In the “Insert Dynamic Data” dialog, you can select your field from either “Advanced Custom Field” or “Custom Field/Meta Options”, insert it and set the operator to is_not_blank.


This tutorial provides the code to add a custom condition using which we can output any element (like a Section) in Oxygen only if a custom field (created with Advanced Custom Fields) has a value.

This is based on the following screencast by Elijah (5:33 onwards):

Step 1

Install and activate Code Snippets plugin.

Step 2

Go to Snippets > Add New.

Title: [Condition] ACF Field Not Empty

Code:

<?php

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

	oxygen_vsb_register_condition(
		// Condition Name
		'ACF Field Not Empty',

		// Values: The array of pre-set values the user can choose from.
		// Set the custom key's value to true to allow users to input custom values.
		array( 
			'options' => array(),
			'custom' => true
		),

		// Operators
		array( '--' ),
		
		// Callback Function: Name of function that will be used to handle the condition
		'acf_field_not_empty_callback',

		// Condition Category: Default ones are Archive, Author, Other, Post, User
		'ACF'
	);

}

/**
 * Callback function to handle the condition.
 * @param  mixed 	$value    	Input value - in this case, name of the ACF custom field.
 * @param  string 	$operator 	Comparison operator selected by the user.
 *
 * @return boolean 				true or false.
 */
function acf_field_not_empty_callback( $value, $operator ) {

	return ( get_field( $value ) ) ? true : false;

}

Set the snippet to run everywhere.

Save changes and activate.

Step 3

Now whichever Section you would like to be output on the front end when a specific custom field is populated, set a Condition to it like this:

replace “banner_text” with the name of your ACF custom field

and the Section will be printed on the front end only if that custom field associated to the current view (like a Page) has a value.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right