Skip Navigation

[Resolved] Generate Default Value for CRED Generic Field from Shortcode

This support ticket is created 2 years, 12 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 10 replies, has 2 voices.

Last updated by julieP 2 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#2236839

I'm having trouble generating a default value for a CRED generic field from a shortcode. I've created a really simple shortcode for the purposes of testing this:-

add_shortcode( 'test_shortcode', 'fd_test_code_shortcode');
function fd_test_code_shortcode() {
    
        $value = 'Hello';
        return $value;
}

created a Types custom field wpcf-test (type = Single Line) and added this to my forms:-

[cred_generic_field type='select' field='wpcf-test']
{
"required":0,
"default":"[test_shortcode]",
"persist":1,
"options":[
          {"value":"Bye","label":"Bye"},
          {"value":"See Ya","label":"See Ya"}
          ]
}
[/cred_generic_field]

The value from the shortcode doesn't display. Are you able to replicate please?

#2237431

Hello and thank you for contacting the Toolset support.

Can you try with the values that are present in the options of the field("Bye" or "See Ya") instead of "Hello"?

If that does not help, allow me temporary access to your website to check this further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please provide the following info:
- Where the shortcode's code is added(Snippet, theme, or a plugin)?
- The URL to the form on the backend.
- A URL where we can see the form in the frontend.

#2237757

Hi Jamal

I added Hello as an option to the generic field shortcode but it made no difference.

I haven't provided access details because the site contains sensitive data.

What I'm actually asking is for you to firstly confirm whether my code is incorrect in any way and then to test it at your end to see if you can replicate it. Can you do this and and let me know your findings please?

By the way, if I add value='[test_shortcode]' to a CRED field shortcode (of the right type), the value displayed on the frontend is Hello.

Thanks

#2237987

I created a test site so we can work on it together. Please reproduce the issue on it and I'll check it further. You can log in with the following URL hidden link

#2238703

Hi Jamal

I've put the shortcode in the functions file for themes 2021 and 2019 via the Theme Editor. You can see the form to create the post on page "Create Test Post". The select field correctly defaults to the shortcode. I've also created an edit form to see what happens but I would need Views (not Blocks) so I can create a Content Template. Are you able to make this available please?

#2238727

I've now tested this on a post form to create a post and the value of my custom shortcode is displayed correctly. It would seem therefore that the issue is with the edit version of the form. If you could make Views available in the sandbox please, I'm happy to check it there too.

Thanks

#2238901

Thank you for your feedback. I activated Toolset Blocks, which is virtually the same plugin as Toolset Views except for two differences:
- Loading priority: Toolset Blocks is loaded before other plugins.
- Editing experience: Toolset Blocks enables the blocks editor by default. But, it can enable the legacy editor too on Toolset->Settings->General(tab)->Editing experience.

I activated the legacy experience, so you can build the view or content template using the legacy editor. I'll let you continue. Thank you for your collaboration.

#2239001

Hi Jamal

So I added the edit form to a Content Template which I assigned to the page 'Update Test Post'. I then created a page 'All Test Posts' to which I added a View to display the posts and provide edit links but the link took me to the backend to edit the post not the Update Test Post page. It's been a while since I used Views and I couldn't remember how to add a frontend edit post link so, for the purposes of this exercise, I just added the edit form to the 'Update Test Post' page and specified the post ID within the CRED shortcode. Should have done it that way to start with really - sorry!

My shortcode returns 'Hello' (for both 2019 and 2021 themes).
If you visit hidden link you'll see the value displayed is 'Hello' (which is correct)
If you visit hidden link you'll see the value displayed is 'See Ya' (which is the value for the post and not the desired default value from the shortcode)

Would you like to take a look and check what I've done and let me know your findings please?

#2239035

To be able to generate a frontend edit link, you will need to have an unassigned content template that holds the edit form. Check this article https://toolset.com/course-lesson/front-end-forms-for-editing-content/

If I understood well, you expect to have the shortcode's value(Hello) selected in the edit form of the post, right?
That would not happen, because the default value will be used only if the field does not actually have a value. As long as the post has the value(See Ya), it will be selected on the dropdown. If the field does not have a value, then the default value will be selected.

I emptied the value from the field on the post hidden link
Now, the dropdown selects the default value (Hello) hidden link

Does it make sense?

Can you elaborate more on what you want to achieve? Maybe there is another way to do.

#2239089

Mmm there is a logic to displaying the actual value for the post rather than something else!! Well at least I know I'm getting expected output and there isn't a problem/conflict anywhere.

I might be making life more difficult than it needs to be so I'll go back to the drawing board. Thanks for your help; much appreciated.

#2239091

My issue is resolved now. Thank you!