[+] Publish all posts connected with one Order when using Forms Commerce multiple times to create multiple posts in the same Order

In this case, Forms Commerce is used to submit multiple posts with the same Order. The Form setting indicates that the post should be published when the Order is completed. However, only one post will be published automatically due to a limitation in the Forms Commerce system. Only one post is expected per Order, so only the newest post will be published when the Order is completed.

To automatically publish all posts connected with the Order, a custom code snippet is required:

// publish all posts created when one commerce form is submitted mutliple times in the same order
add_action( 'cred_commerce_after_order_completed', 'publish_all_ordered_posts', 10, 1 );
function publish_all_ordered_posts( $data ) {
  // which order was completed?
  $order_id = $data['transaction_id'];
  // which posts were created in this order?
  $all_order_posts = get_post_meta( $order_id, '_cred_post_id');
  // loop over all the created posts
  if( !is_array( $all_order_posts ) )
    return;
  foreach( $all_order_posts as $all_order_post ) {
    // check the post status
    $this_post_status = get_post_status( $all_order_post );
    // publish any unpublished posts
    if ( $this_post_status != 'publish' ) {
      $args = array(
        'ID' => $all_order_post,
        'post_status' => 'publish'
      );
      wp_update_post( $args );
    }
  }
}

Redirect to parent post after editing Repeatable Field Group (RFG) in Forms

In this example, we have a repeatable field group (RFG) assigned to some custom post type. On the single post page, we are showing a View of the RFG related to the current post. We want to give some Users the ability to edit those RFGs on the front-end using Forms. In the current system, the recommended approach is to use an Edit Post link in the View to send the User to a page containing a Form. This can be a tedious workflow if you must edit multiple RFGs, going back and forth from the parent post to the Form editor. This code snippet can be used to automatically redirect the User to the post containing the RFG, saving one step in the process. Note that you must set the Form to redirect to some existing Page in the Form configurations. Otherwise, the redirect code will not have any effect on the Form.

/* ----------------------------------------------------------------------- */
// REDIRECT TO RFG PARENT POST AFTER EDITING RFG IN FORMS
// After submitting edit RFG Form, redirect to the post containing the RFG
// Be sure to set some redirect in wp-admin for this Form
//
add_filter('cred_success_redirect', 'custom_redirect_rfg_editor',10,3);
function custom_redirect_rfg_editor($url, $post_id, $form_data)
{
  $forms = array( 12345 );
  $rfg_slug = 'your-rfg-slug';
  if ( in_array( $form_data['id'], $forms ) ) {
    $parent_id = toolset_get_related_post( $post_id, $rfg_slug );
    return get_permalink($parent_id);
  }
  return $url;
}

Require at least one checkbox from a checkboxes group custom field in a CRED form

In a CRED form, you may want to require the User to select at least one checkbox in a checkboxes group custom field. It’s not possible to set up this requirement in wp-admin, but it’s possible to enforce it with the cred_form_validate API hook. Add the following code to your child theme’s functions.php file:

add_filter('cred_form_validate','at_least_one_checkbox_validation', 10, 2);
function at_least_one_checkbox_validation( $field_data, $form_data ) {
    // field data are field values and errors
    list($fields,$errors)=$field_data;
    // forms is a comma-separated list of form IDs where this validation should be applied
    $forms = array( 12345,67890 );
    // validate if the correct CRED form ID
    if ( in_array( $form_data['id'], $forms ) ) {
      if ( !isset($fields['wpcf-cbs']['value'][0]))
        $errors['cbs'] = 'At least one checkbox is required';
    }

    return array( $fields, $errors );
}

Replace 12345,67890 with a comma-separated list of CRED form IDs where you want to apply this validation. Replace ‘cbs’ in both places to match your checkboxes field slug. For example if your checkboxes field slug is “features” then your code should be as follows:

      if ( !isset($fields['wpcf-features']['value'][0]))
        $errors['features'] = 'At least one checkbox is required';

See the documentation for the cred_form_validate API hook for more information.

How to setup a Custom cron job for checking a Post Expiry – CRED

In Toolset >> Settings >> Forms >> Check for expired content >> we have options like “Once hourly, Daily, etc…”

You can enforce that checking using the below code snippet. It will enforce this check and you will see the newly added option in Toolset >> Settings >> Form, like check expiry after “Every 2 minutes” (you can change the value in this code as needed):


add_filter ('cred_post_expiration_cron_schedules', 'my_cred_post_exp_cron_func', 10,1);

function my_cred_post_exp_cron_func ($schedules) {
$schedules['twomin'] = array( 'interval' => 2 * MINUTE_IN_SECONDS, 'display' => __( 'Every 2 minutes' ) );
return $schedules;
}

Remove Toolset buttons for certain User Roles from WP Post Editor

This code allows you to Remove buttons (fields and views, Content Layout Editor, CRED form, Access, Bootstrap buttons) from backend WP editor for certain user roles.

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:


add_filter( 'mce_buttons_3', 'remove_bootstrap_buttons', 999 );
function remove_bootstrap_buttons($buttons) {
if( current_user_can('editor') || current_user_can('contributor') ) {
return array();
}
else {
return $buttons;
}
}

add_filter( 'mce_buttons', 'remove_toggle_button', 999 );
function remove_toggle_button($buttons) {
if( current_user_can('editor') || current_user_can('contributor') ) {
$remove = array( 'css_components_toolbar_toggle' );
return array_diff( $buttons, $remove );
}
else {
return $buttons;
}
}

function vm_wptypes_remove() {

if( current_user_can('editor') || current_user_can('contributor') ) {
wp_enqueue_style( 'wp-types-special', get_stylesheet_directory_uri() . '/wp-types-special.css' );

}
}
add_action( 'admin_init', 'vm_wptypes_remove' );

==> In above code, I have used ‘editor’ and ‘contributor’ level roles as example, you can replace those with your roles as needed. These are in the if conditional statement.

2. Download & extract this file >> and place this css file in the folder in your site:
wp-content/themes/et/ (inside your theme folder)
CSS file:
https://drive.google.com/file/d/0B5EmJQ1qcuyqTU4xZ3paM01ERmc/view?usp=sharing

Related tickets:
https://wp-types.com/forums/topic/removing-buttons-for-certain-user-roles-for-the-post-editor/
https://wp-types.com/forums/topic/how-to-remove-fields-and-views-button-above-post-edit-for-non-admin-users/

CRED Multi checkbox frontend (Javascript , jQuery) validation

I have added a Types multicheckboxes field and want to add two custom validations to this field:
–The first one is to make the field required
–The second one is to force the client to select maximum two checkboxes

jQuery(document).ready(function() {
\This adds the validation method to our API so that CRED can recognize it
    jQuery.validator.addMethod("maxselections", function(value, element, param) {
                                var element_name = jQuery(element).attr('name');
                                return (jQuery('input[name="' + element_name + '"]:checked').length !=0 &&jQuery('input[name="' + element_name + '"]:checked').length <= param[1]);
                              });
\This adds the validations methods as attributes on the element itself.                               
    jQuery('input[name="wpcf-postscheckboxesfield[]"]').attr('data-wpt-validate', '{}')
        .data('wpt-validate', {
          maxselections: {
            args: {
              1: 2
            },
            message: 'You have to choose at least 1, maximum two items'
          }
    }); 
     
});

Validations on WP native fields in a CRED User form

Problem:
Validation and Required field option should work at once in CRED User form for native WP Fields (e.g. Last Name), same as builtin CRED validation style.

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

add_filter('cred_filter_field_before_add_to_form', 'required_fields_func', 10, 2);
function required_fields_func($field, $computed_values){
    if(in_array($field['id'], array('last_name', 'adresse'))){
        $field['data']['validate']['required'] = array ( 
            'active' => 1, 
            'value' => 1, 
            'message' => 'This field is required.'
        ) ;
    }
    return $field;
}

— Make sure to replace ‘last_name’, ‘adresse’ with your field slugs on line #3
— You can modify the message text as needed in above code on line #7.

2. OR If you want to have JS validation, you can use this (modification will be needed):
https://wp-types.com/forums/topic/cred-user-field-not-required/#post-567100

Related Ticket:
https://wp-types.com/forums/topic/cred-user-field-not-required/

RESET button for a Select Field only, in a CRED form

How to add Reset/Clear button for a Select Field only, in a CRED form?

1. Add this JS code in your CRED form >> JS editor section:


jQuery(document).ready(function( $ ){
jQuery('#reset_select').on('click',function(e){
jQuery(this).parent('.form-group').find("option:selected").removeAttr("selected");
});
});

2. Then use the below code in your CRED form, Reset button for the field, please replace field=’status’ with your field name and post=’book’ with your Custom Post Type:


<div class="form-group">
<label>Field Name</label>
[cred_field field='status' post='book' value='' urlparam='' class='form-control' output='bootstrap']

<button type="button" id="reset_select" value="Reset">Reset</button>
</div>

Custom post expiration cron intervals

Sometimes during testing you want the post expiration cron to run at very short intervals, but no short interval is available in the CRED editor. To enable custom cron intervals, add the following code to functions.php:

/* --------------------------------------------- */
// CUSTOM POST EXPIRATION CRON INTERVALS
add_filter ('cred_post_expiration_cron_schedules', 'my_cred_post_exp_cron_func', 10,1);
function my_cred_post_exp_cron_func ($schedules) {
  $schedules['onemin'] = array( 'interval' => MINUTE_IN_SECONDS, 'display' => __( 'Every Minute' ) );
  $schedules['twomin'] = array( 'interval' => 2 * MINUTE_IN_SECONDS, 'display' => __( 'Every 2 Minutes' ) );
  return $schedules;
}

Add or remove custom intervals as needed.

Order taxonomy terms in ASC order while generating dynamic post title using CRED

To get hierarchical taxonomy terms sort in a ASC order you need to add “orderby” clause with WP function: wp_get_post_terms(). you can use term_id with orderby clause. Could you please try following code:


add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==XXXX) {
$term_list = wp_get_post_terms($post_id, 'listing-type', array("fields" => "names","orderby"=>"term_id"));
$auctiontype = join("-",$term_list);
$auctiondate = get_post_meta($post_id, 'wpcf-auction-ending', true);
$auctiondate = date('d-m-Y',$auctiondate);

$term_list= wp_get_post_terms($post_id, 'car-brand', array("fields" => "names","orderby"=>"term_id"));
$carbrand = join("-",$term_list);

$title= $auctiontype. ': le ' . $auctiondate. ' - ' . $carbrand;
$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}

Ticket URL: https://wp-types.com/forums/topic/get-custom-taxonomy-in-auto-title/#post-519488