Skip Navigation

[Resolved] Generic select not loading custom type fields as options

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

Problem: When I use a View to output options for a generic select field, the select field disappears. If I copy the same output generated by the View and paste it as the value of "options" in my CRED form, the select field appears as normal.

Solution: Use a filter to strip out extra characters from your View's output that may be breaking the JSON structure of your options. Add the following code in 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 == '999' ) { //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;
}

Be sure to replace the $id variable with your View's ID.

Relevant Documentation: N/A

This support ticket is created 7 years, 2 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 2 replies, has 2 voices.

Last updated by nereaD 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#490675

Hi!

I´ve made a view to show all items from a custom type:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start --> 
 	<wpv-loop>
         [wpv-item index=1]
          {"value":"[types field='archivo-pdf'][/types]","label":"[wpv-post-title]"}
        [wpv-item index=other]
            ,{"value":"[types field='archivo-pdf'][/types]","label":"[wpv-post-title]"}
        </wpv-loop><!-- wpv-loop-end --> 
[/wpv-items-found]
[wpv-layout-end]

I need that items to populate a select field in a CRED form, so I added the following code:

[cred_generic_field field='dropdownmanual' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="selector-manual"] ]
}
[/cred_generic_field]

But it´s not working.
If I copy what the single view shows ( [wpv-view name="selector-manual"]) and I paste it after ("options":... ) it just works fine.

Is there something I´m missing?

Thank you very much!

#490688

Hi I think your code looks good, but there is one additional step to output "raw" text from your View. Please add the following code to your theme's 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 == '999' ) { //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;
}

Make sure to adjust the $id variable to match your View's ID. This will strip out any extra text that may break the JSON structure of your options. Please let me know how this works for you.

#490700

It worked!

Thank you very much.

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