Skip Navigation

[Resolved] Conditional Progress bar based on empty fields

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 13 replies, has 2 voices.

Last updated by Christian Cox 4 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1343957

Hi there,

I wondered if you could help.

I have a user dashboard I have setup using CRED fields based on a Custom post type of 'Business'.

What I want to acheive is basically a progress bar (bootstrap) which shows them how complete their profile is.

My first idea was to use (rather complex) conditional statements which check on empty fields for the users profile. Not sure if this is the best approach.

Basically for example, if the user has not uploaded a logo, progress bar will show 10%...
....if they've uploaded a logo and filled a Description (WYSIWYG) it will show 20%....
....if they uploaded a logo, filled a description, and entered an email address and phone number it will show 50%...

....etc etc.

Any ideas how this might be possible?

#1344131

Hi, it depends on where you want to show this progress bar, and when you want to update it. If you're talking about showing the progress bar on the same page as the Form, and updating it real-time as the User fills in their information, that would require custom JavaScript that falls outside the scope of support we provide here in the forums. Unfortunately there is no JavaScript API for Forms, so it's not something we can support here.

If you're talking about showing a progress bar on some other page that indicates how much of their profile is complete, then you could do something like create a custom field for Users that holds a number from 0-100 (for a percentage). Each other field they can fill out in the Form has a numeric value associated with it, and the total of all those numeric values should add up to 100. You would use the Forms API to automatically set the percentage number based on the numeric values of the fields that have been completed. Then on the front-end of the site you would use that percentage number to set the progress bar width. We have the cred_save_data hook you can use to perform some action when a Form is submitted successfully: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#1344149

Hi there,

Thanks for your answer and i understand why you mention those. However I'm hoping it might be a bit simpler than that!

It's independent of CRED by the way, not needed in this case.

But further to your idea....is there a way of giving each Custom Post Type field a value / a score. So the function adds all these and returns an overall number?

#1344153

So essentially a php function that loops through all the CPT Fields checking empty ones and producing a number at the end that gets increased each time an empty field is found? I'm not a great programmer so would struggle...hoping there might already be a similar snippet on here that could be modified maybe?

Thanks so much for your help...

#1344183

Or maybe I can have a listening field which I add to the CPT Field Group which will be a number field whose value is increased each time someone completes another field....

Sorry will wait for your reponse 🙂

#1344389

Any of these approaches will require a significant amount of custom code, not exactly like anything else I've seen in the forums (but it's worth searching!). If it's independent of CRED, then there's no Toolset API that will help you respond to changes in a CPT like this. You would need to use something like save_post: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

Each Types custom field is stored in the database with a 'wpcf-' prefix. So if the field in wp-admin is start-date, then the field slug is wpcf-start-date. I can show you how to get any custom field value directly from the db:

$field = get_post_meta( $post_id, 'wpcf-start-date', true );

Or to set any custom field value directly in the db:

update_post_meta( $post_id, 'wpcf-start-date', time() );

Looping over those fields and performing calculations is pretty far outside the scope of support we offer here. You can find professional help for smaller projects here: https://codeable.io/developers/toolset/
We also have a contractors portal available here: https://toolset.com/contractors

#1344875

Hello there - I think I can do something here with what I know already but can you help a little further?

Can you help with with a function that gets the fields I specify in to an array to check how many are empty? I could use a variable which stores the number of empty ones - then I can do something with this? I could store this value in a Custom Field associated with the CPT....

Can you help me?

#1344909

P.s. I need the function to look at each CPT for each user. Then record the number of empty fields in the CPT Field I have created called "wpcf-progress"

#1345529

Okay assuming you will use the save_post hook to trigger the count, here's a template you can begin with:

// automatically count the number of empty fields when your-cpt-slug is saved
add_action( 'save_post', 'auto_count_empties', 100, 3 );
function auto_count_empties( $post_id, $post, $update ) {
  if ( $post->post_type == 'your-cpt-slug' ) {
    $empty = 0;

    // Create a PHP array of all the field slugs and store it in some variable.

    // Create a foreach loop using your array of field slugs. Inside the loop, check the value of the custom field using get_post_meta. If it's empty, increment the $empty variable.

    // Finally, update the progress field using update_post_meta and $empty.
    
  }
}

Be sure to change your-cpt-slug to match the slug of the custom post type.

#1346125

Is this something that can be altered slightly and added to the above?

// shortcode to count number of repeating field instances
add_shortcode( 'count-repeater', 'counter_func' );
function counter_func($atts) {
    $values = get_post_meta( get_the_ID(), 'wpcf-' . $atts['field'], false );
    $res = 0;
    if(is_array($values) && !empty($values[0])){
        $res = sizeof($values);
    }
    return $res;
}
#1346435

Hi there,

Wondered if you could help me a bit more get this tied up?

1. How do I get all the fields from a custom post type and put them in an array?
2. I then need to loop through using a foreach - each time it finds an empty field, it should increment the variable by a given number
3. I then want to return this number via the shortcode.

I've got various snippets but none of them quite meet exactly what I want to do and i'm having trouble linking them all up....

Can you help?

#1346511

Okay let me try to clarify what I can offer here. I can help troubleshoot any code you write that involves the Toolset APIs. I can help give you code examples and give you advice on which APIs do what functions. I cannot give you cut and paste solutions, or write a function that does exactly what you need from scratch. You need a fairly significant amount of code written, and it's outside the scope of what we offer here in the forums. Here's our policy regarding support: https://toolset.com/toolset-support-policy

So again, feel free to send over any code you are troubleshooting or debugging. I suspect, however, you need a developer to do this in a reasonable timeframe.

#1346601

Understood.

Ok. Please can you help me debug the following and help me amend slightly to apply to my situation based on our previous discussions?

function get_custom_fields_by_post_type($post_type) {
    global $wpdb;
    $query = "
        SELECT * 
        FROM  $wpdb->postmeta AS pm1, $wpdb->postmeta AS pm2, $wpdb->posts
        WHERE pm1.meta_key = '_wp_types_group_post_types'
        AND pm1.meta_value LIKE '%$post_type%'
        AND pm2.post_id = pm1.post_id
        AND pm2.meta_key = '_wp_types_group_fields'
        AND $wpdb->posts.ID = pm2.post_id
        ORDER BY $wpdb->posts.post_title ASC
    ";
 
    $results = $wpdb->get_results ( $query );
    $cf_meta = get_custom_field_meta();
    $i=0;
    $my_cfs[post_type] = $post_type;
    foreach($results as $result) {
        $my_cfs[data][$i][group_name] = $result->post_title;
        $my_cfs[data][$i][group_slug] = $result->post_name;
        $the_fields = explode(',',$result->meta_value); // custom fields stored as csv string, but with commas at front and back
        $the_fields = array_filter($the_fields); // deletes empty array elements
        $x=0;
        foreach($the_fields as $the_field) {
            $my_cfs[data][$i][fields][$x] = $cf_meta[$the_field];
            $x++;
        }
        $i++;
    }
    return $my_cfs;
}

Helper function:

function get_custom_field_meta() {
    global $wpdb;
    $cf_meta = $wpdb->get_row("SELECT option_value FROM $wpdb->options WHERE option_name = 'wpcf-fields'");
    $cf_meta = unserialize($cf_meta->option_value);
    return $cf_meta;
}
#1347459

Hmm, I was thinking you would just go into the custom field group editor screen and manually create a comma-separated list of the field slugs you want to count for each post. Then split that into an array in PHP:

$slug_list = "slug1,slug2,slug3";
$slugs = explode("," , $slug_list);
// now $slugs is an array of field slugs

Then in a save_post callback you would loop over such a slug array and perform some calculations based on the value of the current field in the loop, the number of fields, and so on:

// modify this to set a specific post id
$post_id = 12345;

// loop over all the possible slugs and check to see if they are 
foreach( $slugs as $slug ) {
  // field_exists variable set to true or false depending on whether or not the custom field exists in the database for some arbitrary post:
  $field_exists = metadata_exists( 'post', $post_id, 'wpcf-' . $slug );
  
  // the raw field value is available like this:
  $field_value = get_post_meta( $post_id, 'wpcf-' . $slug, true);

  // the number of fields is available like this:
  $field_count = count( $slugs );
}

I'm trying to keep it simple because there are no public APIs for querying Types structures like this on-the-fly...what do you think?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.