Skip Navigation

[Resolved] Auto-populate checkboxes field with custom post type title

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to include a generic checkboxes field in my CRED form. Each post in a CPT should be shown as a checkbox.

Solution:
Use this format to create a generic checkboxes field that uses a View to provide each option:

[cred_generic_field field='provider-checkboxes-slug' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name='providers-checkboxes-options-view-slug'] ]
}
[/cred_generic_field]

Use this format in your View to generate the proper options format:

<wpv-loop>
      [wpv-item index=1]
        {"value":"[wpv-post-id]","label":"[wpv-post-title]"}
      [wpv-item index=other]
      ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

Add the raw text output filter in PHP to clean up the output a bit further:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
    
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '9999' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

Replace 9999 with your View ID.

Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

This support ticket is created 6 years, 11 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 11 replies, has 3 voices.

Last updated by scottL-3 6 years, 11 months ago.

Assisted by: Christian Cox.

Author
Posts
#595608

We are looking to Auto-populate checkboxes field with a custom post type title.

We created the custom post type "provider"

==============================================

In our CRED form is the code:

<div class="row" style="margin-bottom: 20px;">
<div class="col-sm-12">
<div class="form-group">
<label>Providers with this Practice</label>
(HERE IS WHERE WE WANT A FIELD OF CHECKBOXES AUTO-POPULATED WITH THE CPT "PROVIDER")
</div>
</div>
</div>

==============================================

URL:
hidden link

==============================================

Thanks in advance for your help.

#595732

Hi, the best way to do this is to use a generic field, and insert a View to provides the field options.

[cred_generic_field field='provider-checkboxes-slug' type='checkboxes' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name='providers-checkboxes-options-view-slug'] ]
}
[/cred_generic_field]

More information about generic fields here:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

This ticket goes into some detail about how to set up the View and apply the correct custom filter code:
https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

Let me know if you have questions about this approach.

#595935
Screen Shot 2017-12-05 at 7.58.13 AM.png
Screen Shot 2017-12-05 at 7.57.47 AM.png

Thank you for the reply Christian.

Just FYI I've gone through all of the documentation I can find related to this before contacting you. Of course I can't attest to whether or not I've understood it all sufficiently.

So, the code you sent doesn't seem to do it just yet.

Looks like when we use the code in our CRED post form, the name parameter throws an error because name='wpcf-provider-name' appears all in red color (please see screenshot).

Additionally, that field does not appear on the page in which we've placed the CRED post form.

Sure appreciate any thoughts on this.

#595946

Christian,

Just want to make sure that I'm explaining the purpose of all this from our side.

Looking to make an auto-populated set of checkboxes dynamically populated with the title of the custom post type 'provider'

Thanks again

#596065

Yes I think I understand what you'd like to do, however I think you're missing one critical step here. Take a look at the options definition you have used:

"options":[ [wpv-post-field name='wpcf-provider-name'] ]

The wpcf-provider-name field is not formatted appropriately to be used here, and even if it were you haven't included the field for every provider. That is why another View is required. You need to create a View of providers. Use this template to create the Loop Output for your View of providers:

<wpv-loop>
      [wpv-item index=1]
        {"value":"[wpv-post-id]","label":"[wpv-post-title]"}
      [wpv-item index=other]
      ,{"value":"[wpv-post-id]","label":"[wpv-post-title]"}
</wpv-loop>

Now you have created a View that outputs the correct format needed for the options definition, something like:

{"value":"provider-id-1","label":"Provider 1 Name"}, 
{"value":"provider-id-2","label":"Provider 2 Name"}, 
{"value":"provider-id-3","label":"Provider 3 Name"}

Then insert the PHP code here into your functions.php file:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
   
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '9999' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

Change '9999' to the numeric ID of your View of providers. This code cleans up the View output so it can be consumed as the generic field options.

Finally insert this new View in the options definition of your generic field:

"options":[ [wpv-view name='your-new-providers-view-slug'] ]
#596091
Screen Shot 2017-12-05 at 3.22.40 PM.png
Screen Shot 2017-12-05 at 3.22.30 PM.png
Screen Shot 2017-12-05 at 3.21.12 PM.png
Screen Shot 2017-12-05 at 3.20.32 PM.png

Christian,

Firstly, thank you ever so much for your patience.

Your explanation is perfect!
1. I followed your instructions and I entered the code in functions.php (screenshot 1). In that shot I also show the additional block of code that creates the provider CPT post title from custom fields.
2. The checkbox auto-population works! Thank you! (screenshot 2)
3. Only problem, is that when I now create a post and submit it returns a 404 error, which didn't happen before. (screenshot 3); I've included a screenshot of the URL of the error page in case that helps at all. (screenshot 4). BTW, even though the submit throws a 404 on the front end, it actually does create the post in the admin.

Once again, thank you in advance!

#596283

Okay great, I'm glad the checkboxes are working for you. You said the post is created successfully in wp-admin, but the post URL shows a 404? I can think of a few reasons this might be happening:
- The post is in draft or pending review status. You should be able to see this easily from wp-admin.
- The post URL is incorrect, either because of a bad redirect or some other permalink issue. If you edit the post in wp-admin, confirm that the URL is correct here. Then edit the CRED form and confirm the redirect settings are appropriate.
- The post is restricted using the Access plugin, and the current User does not have access to the post for some reason. If you disable Access, you could easily confirm this is the problem.
- A PHP error is blocking this page from loading. In order to tell this, you would need to enable server logs and check for server-side errors. I can show you how to create a log file if you are not familiar with your server's logs. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Then, create a new post using CRED and see if the 404 error appears. If any server-side errors are thrown displaying this post, an error_log.txt file will be created in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php. If no file is created, no error was thrown.

#596776

Christian,

So we followed your instructions.
When we checked each item you posted, things looked good.

Then, entirely by accident, we tested creating the CPT from the front end with a VALID EMAIL address. We'd been testing with made up email addresses.

And THAT DID IT!

Now, creating a CPT works!

Once again, thank you very much!

#596780
Screen Shot 2017-12-07 at 7.40.06 AM.png

Sorry, for re-opening.

Just discovered an issue when we now try to display our view for the CPT providers on the CPT practices.
We've made the content template for our CPTs using DIVI theme.

So, when we now display a post of the practice CPT we get the "provider" check box list as is on the screenshot (to the right).

Is there something that could be done to display the titles of the provider CPT correctly?

Thank you.

#596822

The best approach here is to create another View showing the Provider names in a different format. The View you created before should only be used to provide the options for checkboxes in your CRED form, since it uses special formatting and custom code. If you want, you can duplicate this View from the Views dashboard in wp-admin, then edit that duplicate View. Use the Loop Wizard to create a new Loop Output structure matching the design you want to show on this page. Then place this View on the page instead of your checkboxes option View.

#597233

Brilliant Christian!

Starting to get the hang of it...very much appreciated!

#621872

Is there anyway to use the titles of one custom post type for the options in a select field through Toolset Post Fields directly so that it works when editing the 2nd post type's post fields in WP Admin, not just in CRED?