Mini Tutorial: Custom Dynamic Tag

Last week our Education department released a video tutorial showing how you can use the Dynamic User Picture and Dynamic Number Tags added in Elementor Pro 2.7

And after getting tens of questions and requests:

We decided to share this little Custom Dynamic Tag that lets you set the ACF Number fields you want to calculate the average of.

Please note: that this is an example custom code snippet that comes with no support or guarantee and it is your responsibility to make sure it has no bad side-effects, meaning: Use at Your Own Risk!

Simply copy this into your functions.php and or custom plugin file and you should be set to go:

// custom Custom_Avg_Tag dynamic tag
add_action( 'elementor/dynamic_tags/register_tags', function( $dynamic_tags ) {
	class Custom_ACF_Avg_Tag extends \Elementor\Core\DynamicTags\Tag {

		public function get_name() {
			return 'Custom_ACF_Avg_Tag';
		}

		public function get_categories() {
			return [ 'number' ];
		}

		public function get_group() {
			return [ 'site' ];
		}

		public function get_title() {
			return 'ACF Average';
		}

		protected function _register_controls() {
			$this->add_control(
				'fields',
				[
					'label' => __( 'Fields', 'text-domain' ),
					'type' => 'text',
				]
			);
		}

		public function render() {
			$fields = $this->get_settings( 'fields' );
			$sum = 0;
			$count = 0;
			$value = 0;

			// Make sure that ACF if installed and activated
			if ( ! function_exists( 'get_field' ) ) {
				echo 0;
				return;
			}

			foreach ( explode( ',', $fields ) as $index => $field_name ) {
				$field = get_field( $field_name );
				if ( (int) $field > 0 ) {
					$sum += (int) $field;
					$count++;
				}
			}

			if ( 0 !== $count ) {
				$value = $sum / $count;
			}

			echo $value;
		}
	}
	$dynamic_tags->register_tag( 'Custom_ACF_Avg_Tag' );
} );

This simply adds a new dynamic Tag named “ACF Average”:

computed ACF dynamic field

And to use it, you enter the ACF field names you want to calculate their average, separated by a comma “,”:

computed ACF dynamic field

Enjoy.

Liked This Article?

We have a lot more where that came from! Join 880,651 subscribers who stay ahead of the pack.
By entering your email, you agree to our Terms of Service and Privacy Policy.

About the Author

Ohad Raz
Ohad Raz

Share on

Share on facebook
Share on google
Share on twitter
Share on linkedin

Comments

One Response

  1. I would like to thank you for providing this custom code snippet to add an ACF Average field! This is super useful, and I’m already putting it to work.

    However, I would like to note that I made one modification for my own purposes — rounding off the number so I didn’t end up with numbers like 68.33333333333%, messing up the aesthetic.

    Line 71:
    echo round($value,0);

    This is the only change I made, and wanted to share it with everyone that wants to keep their numbers looking nice!

Leave a Reply

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