Last active
          September 10, 2021 00:07 
        
      - 
      
 - 
        
Save hereswhatidid/eaee026f172e53b8d79c20d6c1666be6 to your computer and use it in GitHub Desktop.  
    Populate ACF drop down with Gravity Forms forms
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?php | |
| namespace HWID\ACFGravity; | |
| class ACF_GF_DropDown { | |
| public static function init() { | |
| add_filter( 'acf/load_field/key=FIELDKEY', [ 'HWID\ACFGravity\ACF_GF_DropDown', 'populate_gform_dropdown' ] ); | |
| } | |
| /** | |
| * Populate an ACF drop down field with the current active Gravity Forms | |
| * | |
| * @param array $field | |
| * | |
| * @return array | |
| */ | |
| static function populate_gform_dropdown( $field ) { | |
| // if GFAPI doesn't exist, GravityForms is not running so bail early | |
| if ( ! class_exists( 'GFAPI' ) ) { | |
| return $field; | |
| } | |
| $forms = \GFAPI::get_forms(); | |
| // Set the initial 'Select a Form' drop down value | |
| $choices = [ | |
| '' => __( 'Select a Form', 'textdomain' ), | |
| ]; | |
| // Add each form to an arrach with the form ID as the array key | |
| foreach( $forms as $form ) { | |
| $choices[$form['id']] = $form['title']; | |
| } | |
| $field['choices'] = $choices; | |
| return $field; | |
| } | |
| } | |
| ACF_GF_DropDown::init(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment