Support

Account

Home Forums General Issues Generate php – Where and how to put the code?

Solved

Generate php – Where and how to put the code?

  • Hi,

    I want to the ACF tool to generate php of a couple of field groups which are only used in a certain single blog template (so not on all the pages of my website). Where do I put the code and what’s the best way to do this?

    Thanks for your help!

  • The code needs to go into your functions.php file.

  • But I only want to use the field group on specific template files.

    I’ve created a new php file acf-deals.php and uploaded it to the /php-includes/ directory in the root directory. It includes the following code:

    <?php
    
    if( function_exists('acf_add_local_field_group') ):
    
    acf_add_local_field_group(array(
    	'key' => 'group_deal1',
    	'title' => 'Deal One (displayed on homepage)',
    	'fields' => array(
    		array(
    			'key' => 'field_5c66e017f8359',
    			'label' => 'Title',
    			'name' => 'deal_title_one',
    			'type' => 'text',
    			'instructions' => '(max. 100 characters)',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-title',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '',
    			'append' => '',
    			'maxlength' => 100,
    		),
    		array(
    			'key' => 'field_5c6d43781bcb1',
    			'label' => 'Deal or Discount',
    			'name' => 'discount_deal_or_special_one',
    			'type' => 'radio',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => array(
    				array(
    					array(
    						'field' => 'field_5c66e017f8359',
    						'operator' => '!=empty',
    					),
    				),
    			),
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'choices' => array(
    				'Discount' => 'Discount',
    				'Deal' => 'Deal',
    			),
    			'allow_null' => 1,
    			'other_choice' => 0,
    			'default_value' => '',
    			'layout' => 'vertical',
    			'return_format' => 'value',
    			'save_other_choice' => 0,
    		),
    		array(
    			'key' => 'field_5c66e094f835a',
    			'label' => 'Description',
    			'name' => 'deal_description_one',
    			'type' => 'textarea',
    			'instructions' => '(max. 600 characters)',
    			'required' => 0,
    			'conditional_logic' => array(
    				array(
    					array(
    						'field' => 'field_5c66e017f8359',
    						'operator' => '==empty',
    					),
    				),
    			),
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-description',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'maxlength' => 600,
    			'rows' => '',
    			'new_lines' => '',
    		),
    		array(
    			'key' => 'field_5c66e0d4f835b',
    			'label' => 'Regular price',
    			'name' => 'regular_price_one',
    			'type' => 'number',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-regular-price',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '$',
    			'append' => '',
    			'min' => '',
    			'max' => '',
    			'step' => '',
    		),
    		array(
    			'key' => 'field_5c66e130f835c',
    			'label' => 'Discounted price / deal price',
    			'name' => 'discounted_price_one',
    			'type' => 'number',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => 'deals-discounted-price',
    				'id' => '',
    			),
    			'default_value' => '',
    			'placeholder' => '',
    			'prepend' => '$',
    			'append' => '',
    			'min' => '',
    			'max' => '',
    			'step' => '',
    		),
    		array(
    			'key' => 'field_5c703c1fd5851',
    			'label' => 'Valid until',
    			'name' => 'valid_until_one',
    			'type' => 'date_picker',
    			'instructions' => '',
    			'required' => 0,
    			'conditional_logic' => 0,
    			'wrapper' => array(
    				'width' => '',
    				'class' => '',
    				'id' => '',
    			),
    			'display_format' => 'm/d/Y',
    			'return_format' => 'm/d/Y',
    			'first_day' => 1,
    		),
    	),
    	'location' => array(
    		array(
    			array(
    				'param' => 'post_type',
    				'operator' => '==',
    				'value' => 'post',
    			),
    			array(
    				'param' => 'post_format',
    				'operator' => '==',
    				'value' => 'aside',
    			),
    		),
    		array(
    			array(
    				'param' => 'post_format',
    				'operator' => '==',
    				'value' => 'status',
    			),
    		),
    	),
    	'menu_order' => 1,
    	'position' => 'acf_after_title',
    	'style' => 'default',
    	'label_placement' => 'top',
    	'instruction_placement' => 'label',
    	'hide_on_screen' => '',
    	'active' => 1,
    	'description' => '',
    ));
    
    endif;
    
    ?>

    I’ve added this code to my single-aside.php file:

    <?php get_template_part( '/php-includes/acf-deals.php', 'acf-deals' ); ?>

    Should that not work?

  • That’s what location rules are for. You must set a location rule so that the field group only applies to those templates. ACF field groups must all be registered when the site is initialized in order to use them anywhere.

  • Hi John, you are truly helpful! I saw you also replied on my other question. Thanks for that!

    So what would be the best way to go? I guess adding the php script (including the code to make certain fields required from my other question) to my functions.php file adding the location rule?

    How do I add the location rule? Do I add it to if( function_exists('acf_add_local_field_group') ):? What would be the code?

    Sorry if this question might be stupid but my coding skills are not that great…

  • When I’m creating a field group in PHP I generally create an example group in ACF with the location rules I want, make sure it works and then export the code to see how it looks and then copy and paste the location value for the field group to the group I’m creating in PHP.

    Location rules can be dynamically generated. Not exactly sure what you’re trying to do, so I can’t really give an example, but I usually still use ACF to give me examples of what I need to do.

    You can modify a field group using the filter acf/load_field_group, this is an undocumented filter.

    
    add_filter('acf/load_field_group', 'my_custom_field_group_function');
    function my_custom_field_group_function($group) {
      if ($group['key'] != 'group_deal1') {
        // not the group you want to modify
        return $group;
      }
      // modify group attributes here
      return $group;
    }
    
  • Well, to be honest I don’t think this questions is ‘valid’ anymore. I thought I had to change the code of the field group and put it somewhere manually to make my other question work (https://support.advancedcustomfields.com/forums/topic/required-field-based-on-input-other-fields/#post-84436). But since you helped me with that amazingly well I can just retrieve the original field keys from the php export and add the filter you mentioned in the other question to my functions.php file. I tested it and it works. Now only need to add the other validation rules. Thanks a lot. I will mark this question as answered

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Generate php – Where and how to put the code?’ is closed to new replies.