Skip Navigation

[Resolved] Use Character Count in Conditional

This support ticket is created 6 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 6 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1104851

Tell us what you are trying to do?
I am trying to display a "middle name" field if related "first name" field is 3 or fewer characters. e.g., if first name is "A.", show the middle name "John" with a space after, then show the last name field containing "Smith". If the first name field is "Dave", skip the middle name and space, then show last name field containing "Jones".

I tried -
[types field='first-name'][/types] [wpv-conditional if="( $(wpcf-first-name) eq '/^[a-zA-Z]{1,3}$/' )"][types field='middle-name'][/types] [/wpv-conditional][types field='last-name'][/types]

And i tried "lt '3'" for the conditional but of course that didn't work either.

I could probably fake it and do >= '.' to catch all the first names with periods in them, but of course there are exceptions to this that would make it impractical.

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

What is the link to your site?
hidden link - This is an example of a page which requires the middle name field added.
hidden link - This page would not need the middle name field.
hidden link - An example that shows why checking for a period in first name would produce a false positive and add an unnecessary space ("V.L." here is contained entirely in the first name field, middle name field was left blank.)

Does this require custom code to first do the count? Or is there a way of writing the conditional that I'm not picking up out of the documentation?

Thank you!

#1105329

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - There is no native feature available to use REGEX with the conditional statement but what you should do is try to create custom shortcode OR function and pass your field value to it, validate your REGEX and pass boolean value true or false and use that custom shortcode or function with [wpv-conditional] shortcode.

Please check following Docs that will help you to understand how you can use custom shortcode or function with [wpv-conditional] shortcode.
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/

#1105592

Is there an example I could reference for doing the shortcode or function? I think I'm a little out of my depth. (I know just enough regex to use it in notepad++ and such. I'm very much a copy/paste/adjust level of coder.)

The only thing I found in the forums that's even kind of like what I'm trying to do is here:
https://toolset.com/forums/topic/character-count-counting-up-cred-and-types-fields/
But that's javascript and related to adding a character count to a form field rather than getting a character count on a database item. So I guess...... not really what I'm trying to do at all!

I tried doing some searching but I only got as far as being pretty sure I need strlen.
So it's going to be something like a function that checks if strlen of first name is less than three then true, if not then false?
And then doing the conditional to check for true, and if yes, then print middle name.

#1106541

Minesh
Supporter

Languages: English (English )

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

Well - that is why I shared you the doc links where examples are given:

Using custom function with [wpv-conditional] example:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-custom-functions-in-conditions/#checking-for-empty-post-content

Using custom shortcode with [wpv-conditional] example:
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes

The only thing I found in the forums that's even kind of like what I'm trying to do is here:
https://toolset.com/forums/topic/character-count-counting-up-cred-and-types-fields/
But that's javascript and related to adding a character count to a form field rather than getting a character count on a database item. So I guess...... not really what I'm trying to do at all!
==> The above solution is to display the total number of characters within the Toolset form field.

Here is another code - its not exactly what you want but it may give you idea how you can use it:
==> https://toolset.com/forums/topic/limit-post-body-content-by-words-cannot-use-exerpt-because-content-has-an-link/#post-359735