Skip Navigation

[Resolved] Test for custom field length in a wpv-conditional shortcode

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

Problem:
How to add a conditional test for the length of the value of a custom field.

Solution:
Register a custom shortcode to get the length of a field passed as an attribute for the current post by adding the following to your theme's functions.php:

/**
 * Register shortcode "fieldlength"
 * 
 * requires attribute field = "slug" of field to measure length of
 */
add_shortcode( 'fieldlength', function( $atts ){
 
    global $post;
 
    $string = get_post_meta( $post->ID, $atts['field'], true);
 
    return strlen( $string );
});

Register the custom shortcode at Toolset > Settings > Front-end Content and then use it inside a wpv-conditional shortcode like so:

[wpv-conditional if="( '[fieldlength field='wpcf-author-name']' gt '15' )"]
Length > 15
[/wpv-conditional]

100% of people find this useful.

This support ticket is created 6 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 Timothy 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#596829

Hi,

I'm trying to use a conditional in a View that checks if a text field is great than X characters, and if so to show some text. I found this from another thread:
[wpv-if evaluate="strlen('[wpv-post-excerpt]') > '100'"] Do Something [/wpv-if]

And tried to update it with the wpv-conditional and my field in a few different ways but none work:

[wpv-conditional evaluate="strlen('[wpv-place-description]') > '200' "] Do Something [/wpv-conditional]

[wpv-conditional if="strlen('[wpv-place-description]') > '200' "] Do Something [/wpv-conditional]

[wpv-conditional if="( strlen('wpcf-place-description') > '200' )"] Do Something [/wpv-conditional]

I also tried using wpcf prefix instead of wpv. And I registered strlen with 3rd party "Functions inside conditional evaluations" in the Settings. But nothing appears on the front-end.

Some attempts showed this error:
Warning: strlen() expects exactly 1 parameter, 3 given in /home/hometita/public_html/happysaigon.com/wp-content/plugins/wp-views/embedded/inc/wpv-condition.php on line 707

Not sure what I'm doing wrong.

Thanks,

Tim

#597040

Nigel
Supporter

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

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

Hi Tim

I've just tried every combination or permutation I can think of and have been unable to get it to work, it is proving trickier than I expected.

I've asked the developers if there is a format I should expect to work, otherwise we might need to register a custom shortcode for this.

I'll update you again.

#597226

Nigel
Supporter

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

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

Hi Tim

I didn't get anything useful back from the developers, so let me propose you add the following to your theme's functions.php file (or use a plugin such as Code Snippets) to register a custom shortcode "fieldlength":

/**
 * Register shortcode "fieldlength"
 * 
 * requires attribute field = "slug" of field to measure length of
 */
add_shortcode( 'fieldlength', function( $atts ){

	global $post;

	$string = get_post_meta( $post->ID, $atts['field'], true);

	return strlen( $string );
});

You'll need to register the custom shortcode at Toolset > Settings > Front-end Content.

You pass the slug of the field whose length you want to measure, so in the conditional it would look like this:

[wpv-conditional if="( '[fieldlength field='wpcf-author-name']' gt '15' )"]
Length > 15
[/wpv-conditional]

You need to edit the slug of the field and the length.

I tested it and it works, but let me know if you have problems.

#597299

This works! Thanks.

Tim