Skip Navigation

[Résolu] Custom Shortcode not working inside conditional output

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
A custom shortcode is used to display an estimated read time for content in a custom field, but breaks inside conditional shortcodes.

Solution:
Rewriting the custom shortcode appears to fix the issue.

The updated code and an example of how to use it is shown below: https://toolset.com/forums/topic/custom-shortcode-not-working-inside-conditional-output/#post-1264961

This support ticket is created Il y a 5 années et 5 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Marqué : 

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par vimalS Il y a 5 années et 5 mois.

Assisté par: Nigel.

Auteur
Publications
#1264039

Custom Shortcode not working inside conditional output

#1264961

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Looking at this again, the code that you shared with me in the chat includes commented out lines and lines that don't do anything (related to images), so I'm guessing you got this code from somewhere and made some edits.

Can we start with a cleaner, re-written shortcode and test that?

/**
 * Register read_time shortcode
 */
add_shortcode('read_time', function ($atts = [], $content = null) {

	if ( isset( $content ) ){
		$content = apply_filters( 'the_content', $content );
		$content = wp_strip_all_tags( $content );
		$word_count = count( preg_split( '/\s+/', $content ) );
		return ceil( $word_count / 150 );
	}
});

I changed how you use the shortcode, so that the content to calculate the reading time of is passed as the shortcode content and not as an attribute, which I think is less likely to cause problems when the attribute value could include text with problem characters and tags.

You can supply the content to measure using a types field, or in my case I'm calculating the read time of the actual post content, like so:

[read_time] [wpv-post-body view_template="None"] [/read_time]

I tested it as is, and inside conditional shortcodes, if you could please confirm the same.

#1265071

My issue is resolved now. Thank you!