Skip Navigation

[Resolved] How can I cound the characters or length of a custom field from Content Template

This support ticket is created 5 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 3 replies, has 2 voices.

Last updated by jeffS-2 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1174004

Tell us what you are trying to do?
I want to count the number of characters in a custom field, and if less than 3 do not display the field.

Is there any documentation that you are following?
I searched Support issues but could not find it.

I tried using create_shortcode but that didn't work either.

Thanks,
Jeff

#1174102

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeff

We are talking about outputting an existing field, not entering data, right?

This should be straightforward—registering the strlen function to be used in conditional shortcodes, then adding a conditional shortcode that uses it—but I'm having a problem getting it to work.

Give me a little more time to keep testing and I'll get back to you.

#1174126

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeff

It turns out we have an issue using native PHP functions in conditional shortcodes: https://toolset.com/errata/issues-when-registering-and-using-php-functions-with-conditional-display-that-pass-less-than-2-arguments/

The workaround is to register a custom function that essentially does the same thing—return the number of characters—and use that.

So, add the following code to your site:

/**
 * Calc string length for use in wpv-conditional
 */
function calc_len($str) {

	return mb_strlen($str);
}

Then register the function calc_len for use in conditional shortcodes at Toolset > Settings > Front-end Content.

You can then use it like so:

[wpv-conditional if="( calc_len( [types field='arbitrary-text'][/types] ) gt 3 )"]
[types field='arbitrary-text'][/types]
[/wpv-conditional]

(In my example I have a Types string field with slug 'arbitrary-text'.)

#1174374

My issue is resolved now. Thank you!