Skip to content

Instantly share code, notes, and snippets.

@daronspence
Forked from tomazzaman/acf-gravity-forms-v5.php
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daronspence/5aa334f241a8ccdbef6f to your computer and use it in GitHub Desktop.
Save daronspence/5aa334f241a8ccdbef6f to your computer and use it in GitHub Desktop.
<?php
class acf_field_gravity_forms extends acf_field {
function __construct() {
$this->name = 'gravity_forms_field';
$this->label = __( 'Gravity Forms', 'acf' );
$this->category = __( "Relational", 'acf');
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0
);
parent::__construct();
}
function render_field( $field ) {
$field = array_merge($this->defaults, $field);
$choices = array();
if ( class_exists( 'RGFormsModel' ) ){
$forms = RGFormsModel::get_forms( null, 'title' );
} else {
return false;
}
if($forms) {
foreach( $forms as $form ) {
$choices[ $form->id ] = ucfirst($form->title);
}
}
// override field settings and render
$field['choices'] = $choices;
$field['type'] = 'select';
?>
<select name="<?php echo acf_esc_attr( $field['name'] ); ?>" id="<?php echo $field['name'];?>">
<?php
foreach ( $field['choices'] as $key => $value ):
$selected = '';
if ( $field['value'] == $key )
$selected = ' selected="selected"';
?>
<option value="<?php echo $key; ?>"<?php echo $selected;?>><?php echo $value; ?></option>
<?php endforeach;
?>
</select>
<?php
}
}
new acf_field_gravity_forms();
<?php
class acf_field_sidebar_selector extends acf_field {
function __construct() {
$this->name = 'sidebar_selector';
$this->label = __( 'Sidebar Selector', 'acf' );
$this->category = __( "Choice", 'acf' );
$this->defaults = array(
'allow_null' => '1',
'default_value' => ''
);
parent::__construct();
}
function render_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __( 'Allow Null?', 'acf-sidebar_selector' ),
'type' => 'radio',
'name' => 'allow_null',
'layout' => 'horizontal',
'choices' => array(
'1' => __( 'Yes', 'acf' ),
'0' => __( 'No', 'acf' ),
)
));
acf_render_field_setting( $field, array(
'label' => __( 'Default Value', 'acf-sidebar_selector' ),
'type' => 'text',
'name' => 'default_value',
));
}
function render_field( $field ) {
global $wp_registered_sidebars;
?>
<div>
<select name='<?php echo $field['name'] ?>'>
<?php if ( !empty( $field['allow_null'] ) ) : ?>
<option value=''><?php _e( 'Select a Sidebar', 'acf' ) ?></option>
<?php endif ?>
<?php
foreach( $wp_registered_sidebars as $sidebar ) :
$selected = ( ( $field['value'] == $sidebar['id'] ) || ( empty( $field['value'] ) && $sidebar['id'] == $field['default_value'] ) ) ? 'selected="selected"' : '';
?>
<option <?php echo $selected ?> value='<?php echo $sidebar['id'] ?>'><?php echo $sidebar['name'] ?></option>
<?php endforeach; ?>
</select>
</div>
<?php
}
}
new acf_field_sidebar_selector();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment