Skip Navigation

[Resolved] Conditions based off off characters numbers

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 11 replies, has 2 voices.

Last updated by mikeH-3 3 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2012511

Shane had helped me with this https://toolset.com/forums/topic/toolset-chat-support-ticket-by-mikeh-3-1617723847/#new-post

But I don't have any actual text in excerpts. I thought it would make the excerpt dynamically but it doesn't. So instead, how do I use this code to apply to the post content instead of the excerpt? So:

[wpv-conditional if="('[cust_count_chars text='wpv-post-excerpt]' gt '100' ) "] Do this [/wpv-conditional]

to instead be this:

[wpv-conditional if="('[cust_count_chars text='wpv-post-body]' gt '100' ) "] Do this [/wpv-conditional]

I tried that last piece of code but it doesn't do anything. Do I need to adjust the shortcode you sent?

#2012579

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

The correct code should be

[wpv-conditional if="('[cust_count_chars text='[wpv-post-body]']' gt '100' ) "] Do this [/wpv-conditional]


Please let me know if this format helps.

Thanks,
Shane

#2012633

Thanks. I tried that but it doesn't seem to work. All my posts are greater than 100 characters and yet it doesn't show anything. If I put instead:

[wpv-conditional if="('[cust_count_chars text='[wpv-post-body]']' lt '1' ) "] Do this [/wpv-conditional]

Then "Do This" shows in every post ( I changed gt to lt and the number to 1). So it seems like it is not counting any actual characters. It's like it sees them all have 0 characters so if I use lt, it always shows that piece of text. If I use gt, it doesn't.

#2012645

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

Would you mind providing me with admin access to the site to check this further for you ?

I've enabled the private fields so that you can provide the credentials. Also please send the exact page that you are testing this on.

Thanks,
Shane

#2014103

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2021-04-07 at 3.06.06 PM.png

Hi Mike,

Thank you for the credentials,

However i'm having some trouble accessing the backend of the site. If you have a look at my screenshot i'm getting "too many redirects" error.

I've modified the shortcode to get the post content directly inside the code so you will need to replace the existing shortcode with

// Add Shortcode
function cust_count_chars( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'id' => '',
		),
		$atts
	);

	$my_post = get_post($atts['id']);
    $content = $my_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
  
	return strlen($content);

}
add_shortcode( 'cust_count_chars', 'cust_count_chars' );

Then it is used like this [cust_count_chars id='[wpv-post-id]'] and you will add it in the conditional like


[wpv-conditional if="('[cust_count_chars id='[wpv-post-id]']' gt '100' ) "] Do this [/wpv-conditional]

Thanks,
Shane

#2014939

Thanks for this. I fixed the login issue. I put in the new code but it doesn't seem accurate at all. For instance, It thinks this announcement:

"We will be hosting a blood drive! Time slots available from 12:00 until 3 pm. Register online today or email erin@placec.org if you have questions. "

is more than 300 characters. Yet the announcement below that is longer and it thinks it has less than 300 characters. You can see on that announcement page.

It also thinks content with JUST an image inserted is more than 300 characters (see last announcement on that page).

Can you take a look? I think we are closer but not quite there.

#2014967

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

There will be inaccuracies with this mainly because you are doing it on the post content which can have images, texts pretty much any kind of content and the function that we are using is counting the string length. For images i'm not sure how the strlen() php function process this.

Regarding the ones with just texts only I'm seeing that the counts are actually correct.

The code that you should be using is this one below.
[wpv-conditional if="('[cust_count_chars id='[wpv-post-id]']' lt '300' ) " ] Post ID: Less than 300 [/wpv-conditional]
[wpv-conditional if="('[cust_count_chars id='[wpv-post-id]']' gt '300' ) "] Post ID: Greater than 300 [/wpv-conditional]

Thanks,
Shane

#2015003

I assume then there is no way to count rendered characters instead? Ultimately I'm trying to show the actual body text as the announcement when it's shorter than 300 characters, since the body shows rendered html.

#2015031

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

The best thing that can be done here, is to perform a string length with the html content stripped from the text because html characters add to the length of the string.

There isn't an efficient way to count the character length when we have no control over the content that can be added to the field.

Given that this is custom code there isn't much else that I can do to resolve this.

Thanks,
Shane

#2015251

Would you be able to integrate this strip tag function into what you made? https://wordpress.stackexchange.com/questions/161740/count-title-and-post-characters

It talks about this:

You can use this function wp_strip_all_tags.

strlen( wp_strip_all_tags($post->post_content));

With that you get rid of all the HTML tags.

Just a little extra detail, you might want to strip the shortcodes, too. You can use this function strip_shortcodes

You will end up with something like this:

 strlen( wp_strip_all_tags(strip_shortcodes($post->post_content)));

To convert the encoded entities to the corresponding string value you could use the PHP function html_entity_decode.

#2015253

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Mike,

This is what i'm referring to.
I've added the modification to the code now.

Please let me know if the count appears accurate now.

Thanks,
Shane

#2015261

That seems to have done it! Looks to be working correct now. Thanks so much for your help!