Skip to content

Instantly share code, notes, and snippets.

@benfavre
Created December 28, 2012 14:07
Show Gist options
  • Save benfavre/4398157 to your computer and use it in GitHub Desktop.
Save benfavre/4398157 to your computer and use it in GitHub Desktop.
Uses a hidden input and saves only the name. This one allows you to order the selection
<?php
wp_enqueue_style( 'pods-select2' );
wp_enqueue_script( 'pods-select2' );
$attributes = array();
$attributes[ 'type' ] = 'text';
$attributes[ 'value' ] = $value;
$attributes[ 'tabindex' ] = 2;
$attributes = PodsForm::merge_attributes( $attributes, $name, PodsForm::$field_type, $options );
if( class_exists( 'RGFormsModel' ) ) :
$forms = RGFormsModel::get_forms(1, "title");
if( count( $forms ) > 0 ) : ?>
<div class="pods-select2">
<input<?php PodsForm::attributes( $attributes, $name, PodsForm::$field_type, $options ); ?> style="width:95%;" />
</div>
<?php
// Get Form Titles
$form_titles = array();
foreach( $forms as $form ) :
$form_titles[] = $form->title;
endforeach;
?>
<script type="text/javascript">
jQuery( function ( $ ) {
var $element = $('#<?php echo $attributes[ 'id' ] ?>' );
<?php
if(count($form_titles) >= 0) : ?>
$element.select2({tags:["<?php echo implode('","', $form_titles); ?>"]});
<?php endif; ?>
$element.on("change", function() { $element.html($element.val());});
$element.select2("container").find("ul.select2-choices").sortable({
containment: 'parent',
start: function() { $element.select2("onSortStart"); },
update: function() { $element.select2("onSortEnd"); }
});
});
</script>
<?php else : ?>
<p>There are no forms available at this time. You may <a href="admin.php?page=gf_new_form">create one</a> now (<strong>warning</strong> entered data <em>will be lost</em> unless you save first)</p>
<?php endif;
else : ?>
<p>Gravity Forms is currently not available.</p>
<?php
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment