Submit CRED Form(s) with AJAX

Download and add to your theme:
http://malsup.github.com/jquery.form.js

Add to your Functions PHP:

wp_enqueue_script("jquery");
wp_enqueue_script( 'jquery-form-js', get_template_directory_uri() . '/js/jquery.form.js', array('jquery'), '3.51.0-2014.06.20', true );

In any SINGLE CRED form JS Editor:

jQuery(document).ready(function() { 
            
            jQuery('#cred_form_4988_1').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
            }); 
        }); 

Replace #cred_form_4988_1 with the actual ID of the CRED Form you’ll see in your browser console asa in this:

<form enctype="multipart/form-data" id="cred_form_4988_1" class="cred-form cred-keep-original" action="/toolset/2015/07/08/ajax-edit/?cred-edit-form=4988&amp;_tt=1436396327" method="post">

If you have MULTIPLE CRED FORMS (of same form), outputted in a Views Loop
Means, you got x posts, and therefore x CRED Forms of same type,the ID of CRED is continuously increased by one, so the a above jQuery breaks. You should use here:

jQuery(document).ready(function() { 
jQuery("[id^=cred_form_1096]").ajaxForm(function() { 
    alert("Thank you for your comment!"); 
}); 
}); 

We use now ("[id^=PART_OF_ID]")

You can also seek for all elements which contain (*=) or end up with ($=) known part of ID/class/type.
(example, id^, id*, id$)

DOC Links:
http://malsup.com/jquery/form/#getting-started

Default a taxonomy term in CRED

Select a taxonomy term as default for all posts when submitting a CRED form with jQuery code.

jQuery( document ).ready(function() {
    jQuery('#category').hide();
    jQuery('#category select option[value="5"]').attr("selected",true);
});

You should wrap the taxonomy shortcode with any HTML element, containing the ID you need:

<span id="category">[cred_field field="category" display="select" single_select="true"]</span>

Extending CRED allowed tags

add_filter( 'wp_kses_allowed_html', 'esw_author_cap_filter',1,1 );

function esw_author_cap_filter( $allowedposttags ) {

//Here put your conditions, depending your context

if ( !current_user_can( 'publish_posts' ) )
return $allowedposttags;

// Here add tags and attributes you want to allow

$allowedposttags['iframe']=array(

'align' => true,
'width' => true,
'height' => true,
'frameborder' => true,
'name' => true,
'src' => true,
'id' => true,
'class' => true,
'style' => true,
'scrolling' => true,
'marginwidth' => true,
'marginheight' => true,
);
return $allowedposttags;
}

Change DatePicker in Views

Add some parameter on datepicker when it has been initialized already, in a View.

jQuery(window).bind("load", function() {
jQuery( ".wpv-date-front-end" ).datepicker( "option", "yearRange", "2002:2012" ).datepicker("option", "defaultDate", "01012012");
});

Change DatePicker in CRED

Change a datepicker after initialization in CRED.

jQuery(window).bind("cred_form_ready", function() {
   jQuery( ".js-wpt-date" ).datepicker( "option", "yearRange", "2002:2015" , ).datepicker( "option", "dateFormat", 'yy-M-dd' ).datepicker("setDate", 'November 24, 2017');
});

Validate width and height of image with CRED

You can retrieve that data through tmp_name. The first and second indexes of this array are width and height, respectively

function validate_image_width_height($field_data, $form_data){
  
list($fields,$errors)=$field_data;  

$image_info = $fields['wpcf-image-1']['value']['tmp_name'];
$image_width = $image_info[0];
$image_width = $image_info[1];

if ($image_width < 100){
     $errors['image-1'] = __('Wrong width', 'nbt');
}

var_dump($fields['wpcf-image-1']);
 
return array($fields,$errors);
}

add_filter('cred_form_validate','validate_image_width_height',10,2);

Use the Code Snippet button to enter php and html snippets.

Limit a ‘n’ of posts users can submit with CRED

Limit the number of featured posts which user can submit. This Code limit to 1 featured post for each user.
The “wpcf-mls” is the custom field which define if the post is featured or not. It should store 1 for featured.

This hook will be triggered as soon as this custom field sent “1”.
Then check how many featured posts the current user has. Limit this number to “1”.

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data){
global $wpdb; 
list($fields,$errors)=$field_data;
   if ($form_data['id']==241){	
      if ($fields['wpcf-mls']['value'] == 1){
         $results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->posts.' post, '.$wpdb->postmeta.' meta WHERE post.ID = meta.post_id and post.post_status = "publish" and meta.meta_key = "wpcf-mls" and meta.meta_value = "1" AND post.post_author = "'.get_current_user_id().'"', OBJECT );
         if (count($results) > 1){
            $errors['mls']='Your limit for featured posts is 1';
         }    
      }
      return array($fields,$errors);
   }
}