Permalink
Browse files

added support for select, checkbox and radio

  • Loading branch information...
1 parent c960117 commit 8fb440c5dfeaf34bfc7e22ec21fd230f62ac3736 @madalinungureanu committed Nov 3, 2011
Showing with 104 additions and 10 deletions.
  1. +39 −4 custom-fields-creator.js
  2. +53 −5 custom-fields-creator.php
  3. +12 −1 readme.txt
@@ -10,8 +10,24 @@ function addMeta(value, id, nonce){
var values = {};
jQuery('#'+value+' .mb-field').each(function(){
+
var key = jQuery(this).attr('name');
- values[key.toString()] = jQuery(this).val().toString();
+
+ if(jQuery(this).attr('type') == 'checkbox' || jQuery(this).attr('type') == 'radio' ) {
+
+ if( typeof values[key.toString()] === "undefined" )
+ values[key.toString()] = '';
+
+ if(jQuery(this).is(':checked')){
+ if( values[key.toString()] == '' )
+ values[key.toString()] += jQuery(this).val().toString();
+ else
+ values[key.toString()] += ', ' + jQuery(this).val().toString();
+ }
+ }
+
+ else
+ values[key.toString()] = jQuery(this).val().toString();
});
jQuery.post( ajaxurl , { action:"cfc_add_meta", meta:value, id:id, values:values, _ajax_nonce:nonce}, function(response) {
@@ -27,7 +43,10 @@ function addMeta(value, id, nonce){
mb_sortable_elements();
jQuery('#'+value+' .mb-field').each(function(){
- jQuery(this).val('');
+ if(jQuery(this).attr('type') == 'checkbox' || jQuery(this).attr('type') == 'radio' )
+ jQuery(this).removeAttr( 'checked' );
+ else
+ jQuery(this).val('');
});
jQuery('#'+value).parent().css('opacity','1');
@@ -143,8 +162,24 @@ function updateMeta(value, id, element_id, nonce){
jQuery('#'+value).parent().css({'opacity':'0.4', 'position':'relative'}).append('<div id="mb-ajax-loading"></div>');
var values = {};
jQuery('#update_container_'+value+'_'+element_id+' .mb-field').each(function(){
- var key = jQuery(this).attr('name');
- values[key.toString()] = jQuery(this).val().toString();
+ var key = jQuery(this).attr('name');
+
+ if(jQuery(this).attr('type') == 'checkbox' || jQuery(this).attr('type') == 'radio' ) {
+
+ if( typeof values[key.toString()] === "undefined" )
+ values[key.toString()] = '';
+
+ if(jQuery(this).is(':checked')){
+ if( values[key.toString()] == '' )
+ values[key.toString()] += jQuery(this).val().toString();
+ else
+ values[key.toString()] += ', ' + jQuery(this).val().toString();
+ }
+ }
+
+ else
+ values[key.toString()] = jQuery(this).val().toString();
+
});
jQuery.post( ajaxurl , { action:"cfc_update_meta", meta:value, id:id, element_id:element_id, values:values, _ajax_nonce:nonce}, function(response) {
@@ -19,10 +19,12 @@
$fint = array(
- array( 'type' => 'text', 'title' => 'Title', 'description' => 'Description for this input' ),
- array( 'type' => 'textarea', 'title' => 'Description' ),
- array( 'type' => 'upload', 'title' => 'Image', 'description' => 'Upload a image' ),
- array( 'type' => 'upload', 'title' => 'Video', 'description' => 'Upload a video' )
+ array( 'type' => 'text', 'title' => 'Title', 'description' => 'Description for this input' ),
+ array( 'type' => 'textarea', 'title' => 'Description' ),
+ array( 'type' => 'upload', 'title' => 'Image', 'description' => 'Upload a image' ),
+ array( 'type' => 'select', 'title' => 'Select This', 'options' => array( 'Option 1', 'Option 2', 'Option 3' ) ),
+ array( 'type' => 'checkbox', 'title' => 'Check This', 'options' => array( 'Option 1', 'Option 2', 'Option 3' ) ),
+ array( 'type' => 'radio', 'title' => 'Radio This', 'options' => array( 'Radio 1', 'Radio 2', 'Radio 3' ) ),
);
$args = array(
@@ -36,6 +38,10 @@
new Custom_Fields_Creator( $args );
+On the frontend:
+
+$meta = get_post_meta( $post->ID, 'rmscontent', true );
+
*/
class Custom_Fields_Creator{
@@ -158,10 +164,52 @@ function cfc_output_form_field( $meta, $details, $value = '', $context = '' ){
$element .= '<input type="text" name="'. esc_attr( sanitize_title_with_dashes( remove_accents( $details['title'] ) ) ) .'" value="'. $value .'" class="mb-text-input mb-field"/>';
}
- if($details['type'] == 'textarea'){
+ if($details['type'] == 'textarea'){
$element .= '<textarea name="'. esc_attr( sanitize_title_with_dashes( remove_accents( $details['title'] ) ) ) .'" style="vertical-align:top;" class="mb-textarea mb-field">'. $value .'</textarea>';
}
+ if($details['type'] == 'select'){
+ $element .= '<select name="'. esc_attr( sanitize_title_with_dashes( remove_accents( $details['title'] ) ) ) .'" class="mb-select mb-field" >';
+ $element .= '<option value="default">Select</option>';
+
+ if( !empty( $details['options'] ) ){
+ foreach( $details['options'] as $option ){
+ $element .= '<option value="'. $option .'" '. selected( $option, $value, false ) .' >'. $option .'</option>';
+ }
+ }
+
+ $element .= '</select>';
+ }
+
+ if($details['type'] == 'checkbox'){
+
+ if( !empty( $details['options'] ) ){
+ foreach( $details['options'] as $option ){
+ $found = false;
+
+ if ( strpos($value, $option) !== false )
+ $found = true;
+ $element .= '<input type="checkbox" name="'. esc_attr( sanitize_title_with_dashes( remove_accents( $details['title'] ) ) ) .'" value="'. $option .'" '. checked( $found, true, false ) .'class="mb-checkbox mb-field" />'. $option .' ' ;
+ }
+ }
+
+ }
+
+ if($details['type'] == 'radio'){
+
+ if( !empty( $details['options'] ) ){
+ foreach( $details['options'] as $option ){
+ $found = false;
+
+ if ( strpos($value, $option) !== false )
+ $found = true;
+ $element .= '<input type="radio" name="'. esc_attr( sanitize_title_with_dashes( remove_accents( $details['title'] ) ) ) .'" value="'. $option .'" '. checked( $found, true, false ) .'class="mb-radio mb-field" />'. $option .' ' ;
+ }
+ }
+
+ }
+
+
if($details['type'] == 'upload'){
$element .= '<input id="'. esc_attr($meta.$details['title']) .'" type="text" size="36" name="'. esc_attr( sanitize_title_with_dashes( remove_accents ( $details['title'] ) ) ) .'" value="'. $value .'" class="mb-text-input mb-field"/>';
$element .= '<a id="upload_'. esc_attr(strtolower($details['title'])) .'_button" class="button" onclick="tb_show(\'\', \'media-upload.php?type=file&amp;mb_type='. $var_prefix . esc_js(strtolower($meta.$details['title'])).'&amp;TB_iframe=true\');">Upload '. $details['title'] .' </a>';
View
@@ -6,7 +6,11 @@ Usage Example 1
array( 'type' => 'text', 'title' => 'Title', 'description' => 'Description for this input' ),
array( 'type' => 'textarea', 'title' => 'Description' ),
array( 'type' => 'upload', 'title' => 'Image', 'description' => 'Upload a image' ),
- array( 'type' => 'upload', 'title' => 'Video', 'description' => 'Upload a video' )
+ array( 'type' => 'select', 'title' => 'Select This', 'options' => array( 'Option 1', 'Option 2', 'Option 3' ) ),
+
+ array( 'type' => 'checkbox', 'title' => 'Check This', 'options' => array( 'Option 1', 'Option 2', 'Option 3' ) ),
+
+ array( 'type' => 'radio', 'title' => 'Radio This', 'options' => array( 'Radio 1', 'Radio 2', 'Radio 3' ) ),
);
$args = array(
@@ -21,6 +25,13 @@ Usage Example 1
?>
+For Frontend use like this:
+
+<?php $meta = get_post_meta( $post->ID, 'rmscontent', true ); ?>
+
+
+
+
Default Parameters
<?php $args = array(

0 comments on commit 8fb440c

Please sign in to comment.