Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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();
$forms = RGFormsModel::get_forms( null, 'title' );
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
You can’t perform that action at this time.