Skip Navigation

[Resolved] How use a shortcode instead of options for CRED forms?

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

Problem: How to display clean text raw output with views.

Solution: Since Views 1.11 we have had to wrap Views loop outputs in default HTML and CSS/JS in order to support things like infinite scrolling, and future to come features like "Front End Sorting and Ordering".

To return a "clean" Loop output is using the filter 'wpv_filter_wpv_view_shortcode_output' to your current theme's functions.php file.

You can find proposed solution with the following reply. https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms/#post-419301

Relevant Documentation:

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.

This topic contains 12 replies, has 3 voices.

Last updated by spiridonR 4 years, 10 months ago.

Assigned support staff: Minesh.

Author
Posts
#419277

Hello!

I want to create a CRED form with the use of Field type "Select". I want to use Views shortcode to dynamically populate the options in this field. Here is a description of this function:

"When options for a generic field need to be generated dynamically, a shortcode can be used to generate the field options on the fly. Using a View shortcode to generate options based on criteria is a usual case. Any shortcode which will generate a valid JSON format of options can be used."

I created a view, which generates the appropriate values:

{"value":"12","label":"POST Test 3"}, 
{"value":"10","label":"POST Test 2"}, 
{"value":"8","label":"POST Test 1"}, 
{"value":"demo","label":"demo"} ]

I put the shortcode in the options:

[cred_generic_field field='test2' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="views-test-1"] ]
}
[/cred_generic_field]

However, if I insert shortcode as an option, the generic field is not displayed in the form. You can help with the configuration form? I will provide data access to the demo site.

#419288

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To return a "clean" Loop output is using the filter 'wpv_filter_wpv_view_shortcode_output' to your current 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 == '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;
}

Where:
Replace '9999' with your original view ID.

I hope above solution will help you to resolve your issue.

#419295
cred.PNG

No, it does not help. Besides, as I understand it, this function should work without any changes to the code.

#419298

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I need to check on your install why its not working. It should work.

1)
Could you please share problem URL.

2)

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#419301

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I checked and view's output is wrapped with additional required divs. To have raw text output just like json as I suggested you should add following code to your current 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 == '6' ) { //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;
}

Where '6' is your original view ID.

I hope once you add above code to functions.php file, your issue will be resolved.

#419303

I did it again and it does not help. I can provide you with access via FTP.

#419304

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sure. Please share FTP access.

I have set the next reply to private which means only you and I have access to it.

#419317

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now.

I've adjusted your view code. There was an issue with the extra comma with the last item.

{"value":"12","label":"POST Test 3"} ,{"value":"10","label":"POST Test 2"} ,{"value":"8","label":"POST Test 1"}, -- this last comma was issue

So, I've adjusted your view code as:

<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>

This code will output.

{"value":"12","label":"POST Test 3"} ,{"value":"10","label":"POST Test 2"} ,{"value":"8","label":"POST Test 1"}  - without last comma

I hope this solution will help you to resolve your issue. Have a great day!!

#419329

Thank you, it works! However, it is possible to do without adding code to the file function.php? Alternatively, specify the slug of view, but not ID.

#419331

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As I explain to you in my previous reply, you need to add that code in order to get raw/text output.

No - you need to compare with ID, there is not option available to compare with view's slug.

#419400

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I hope your original issue is resolved. If so, could you please kindly mark resolve this ticket.

#419424

Yes, thank you!

#564245

Hi.
It would be great if you could implement the add_filter solution for raw output as a simple variable in the view shortcode. ex.

[wpv-view name="views-test-1" raw="true"]

I see a lot of people (including me) searching the solution for the same thing, again and again.