CRED plugin provides an API, making it easy to customize your post or user forms. The API includes hooks (actions and filters) to accomplish specific tasks using PHP code.
When you ask for help or report issues, make sure to tell us all related information about your form and what you want to achieve.
Viewing 15 topics - 811 through 825 (of 861 total)
Problem: I am using the cred_save_data hook to update the status of a post, but I am having trouble adding a conditional to restrict the callback to more than one Form ID.
Solution: Check your callback syntax. You can create an array of Form IDs, then test the current Form ID against that array. You must include the $form_data parameter as an input to your callback function to access the current Form's ID.
// CRED PUBLISH STATUS
add_action('cred_save_data', 'Select_Post_Status_func',10,2);
function Select_Post_Status_func($post_id, $form_data) {
if($_REQUEST['post_status'] == ''){
//Do nothing
return;
}
//create an array of values with all ID's of the Forms you want to check:
$ids = array("101","102","103");
//Check if the current form ID is one of in the above array:
if (in_array($form_data['id'], $ids) ){
$my_post['ID'] = $post_id;
$my_post['post_status'] = $_REQUEST['post_status'];
// Update the post into the database
wp_update_post( $my_post );
}
}
Problem: I have a Form that allows Users to create a new Post. They can select a different post in the Form. I would like to copy all the taxonomy terms applied to the selected post and apply them to the new post.
Solution:
Use the cred_save_data API, along with wp_get_object_terms and wp_set_object_terms, to copy the terms from post A in to post B.
add_action('cred_save_data_2187', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
// get data of original post
$post = get_post( $form_data['container_id'], ARRAY_A );
// update Post Status to PENDING
$post['post_status'] = 'pending';
// set author as current user
$author_id = get_current_user_id();
$post['post_author'] = $author_id;
// update the new post with this data
$post['ID'] = $post_id;
$post['post_title'] = 'Copy of ' . $post['post_title'];
wp_update_post( $post );
// get fields of original post
$fields = get_post_custom( $form_data['container_id'] );
// update the new post with these fields
foreach ($fields as $key => $values) {
foreach ($values as $value) {
add_post_meta($post_id, $key, $value, false);
}
}
// update Taxonomies & Categories
$original_post_id = $form_data['container_id'];
$new_post_id = $post_id;
$original_post = get_post($original_post_id);
$taxonomies = get_object_taxonomies($original_post->post_type); // returns array of taxonomy slugs for this post type
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($original_post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
}
Problem: I have a Form that creates child posts. I would like to use the cred_form_validate API to ensure that the parent post chosen in the Form has specific terms and custom field values.
Solution: Use custom code to query the parent post and compare its terms and custom field values.
add_filter('cred_form_validate','validate_parent_consultant',10,2);
function validate_parent_consultant($field_data, $form_data)
{
list($fields,$errors)=$field_data;
//error_log(print_r($fields, true));
$forms = array( 6 );
// validate if correct CRED form ID
if ( in_array( $form_data['id'], $forms ) ) {
// edit these 6 lines as needed
$parent_field_post_name = '@consultant-event_parent';
$parent_field_name = '@consultant-event.parent';
$checkbox_slug = 'parent-checkbox-field';
$taxonomy_slug = 'consultant-taxonomy';
$taxonomy_terms = array('a', 'b');
$test_cbs = array( 'a', 'b', 'c' );
// get the parent ID from the form field
$parent_id = $_POST[$parent_field_post_name];
// check if parent has all matching checkboxes
$vals = types_render_field($checkbox_slug, array( "separator"=>",", "id"=>$parent_id));
$any = strlen($vals) > 0 ? explode(',', $vals) : array();
$cbs_match = sizeof($any) > 0 && sizeof(array_intersect( $any, $test_cbs)) == sizeof($test_cbs);
// Test if the parent has all the right term(s)
$matching_terms = [];
foreach($taxonomy_terms as $tax_term) {
if( is_object_in_term( $parent_id, $taxonomy_slug, $tax_term ) ){
$matching_terms[]= $tax_term;
}
}
$terms_match = sizeof($matching_terms) == sizeof($taxonomy_terms);
// combine parent validations
if( !$cbs_match || !$terms_match ) {
$errors[$parent_field_name] = __('Select a different parent post.', 'your-theme-domain');
}
}
//return result
return array($fields,$errors);
}
Problem: I would like to customize the title of a post created with Toolset Forms. I would like to use the format "postid-posttimestamp-postreferencetitle", where post reference title is the title of the post selected in a post reference field in the Form.
Solution:
Use the cred_save_data hook to update the post title. Access the post ID from the hook inputs, get the post time using get_post_time, and get the reference field post title using get_the_title and toolset_get_related_post.
Problem: I have a Form that creates new child posts. I would like to use the cred_save_data API and the new Post Relationships API to update a custom field value in the parent post. I also have a Form that edits a parent post. I would like to use the APIs to update custom field values in all its child posts.
Solution:
To update the parent from the child post Form:
Problem: I would like to use cred_before_save_data to prevent a User from submitting the same form multiple times to create multiple posts, or to edit the same post more than once.
Solution: The cred_before_save_data hook should not be used to validate form submissions. Instead, use the cred_form_validate hook. This will allow you to return meaningful error messages to the front-end of the site. Here is an example showing how to prevent a User from creating more than one post, or how to prevent a User from editing a post where a specific custom field already has a value:
add_filter('cred_form_validate','profile_exists_validation',10,2);
function profile_exists_validation($error_fields, $form_data)
{
global $current_user;
//field data are field values and errors
list($fields,$errors)=$error_fields;
//uncomment this if you want to print the field values
//print_r($fields);
//validate if specific form
if ($form_data['id']==12345)
{
//check custom field value
if (isset($fields['wpcf-sel1']['value']) && $fields['wpcf-sel1']['value']!='')
{
//set error message for my_field
$errors['wpcf-sel1']='This custom field has already been defined for the post.';
}
//check if current User is already the author of a "Profile" custom post
$args = array(
'author' => $current_user->ID,
'orderby' => 'post_date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_type' => 'profile'
);
$profiles = get_posts( $args );
if ( sizeof($profiles) > 0 )
{
//set error message for post title
$errors['post_title'] = 'Profile already exists, so you cannot create another one.';
}
}
//return result
return array($fields,$errors);
}