Skip Navigation

[Closed] Display Shortcode conditionally based on URL

This support ticket is created 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 4 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1725675

I have this plugin for Weglot that auto-translate content on the go. We use PDF embedded to embed the PDF with a shortcode.

Is there was a way to conditional change the shortcode to display a different version of the PDF based on URL?

/fr/, /de/ - etc.

#1726015
Screen Shot 2020-08-03 at 2.26.48 PM.png

Hello, I believe you're looking for a conditional that will test the URL of the current page for a specific string. For example, if the current URL begins with https://yoursite.com/de/... then you would like to display one specific PDF using a shortcode. If the current URL begins with https://yoursite.com/fr/... then you would like to display a different PDF using a different shortcode. Is that correct? If so, then you can use the post URL shortcode to get the current URL, and the LEFT operator to test the beginning of the URL string in your conditional, like this:

[wpv-conditional if="( LEFT('[wpv-post-url item="$current_page"]',24) eq '<em><u>hidden link</u></em>' )" debug="true"]
The current URL begins with <em><u>hidden link</u></em>
[/wpv-conditional]
[wpv-conditional if="( LEFT('[wpv-post-url item="$current_page"]',24) eq '<em><u>hidden link</u></em>' )" debug="true"]
The current URL begins with <em><u>hidden link</u></em>
[/wpv-conditional]

Note that you must replace hidden link; and hidden link; with your actual site URLs, and the number 24 must be replaced with the number of characters you want to consider matching at the beginning of the URL. See the screenshot here - 24 is the number of characters to match at the beginning of the URL. Your number will be different, based on the actual URL.

Let me know if this doesn't work as expected and I can take a closer look.

#1727341

Hi Christian,

I think this might work but is there a way to be more specific?

If the URL contains a word that might not start right after the domain URL.

#1728559

The LEFT function tests a string from the beginning of the URL to a specific position in the string based on the number of characters. Unfortunately it's not flexible enough to skip over some characters and check for a specific matching word in any position in the string. Are you familiar with regular expressions? I'm not aware of your programming skill level, so I'm not sure if this would be helpful or not. We offer a function REGEX for evaluating custom regular expressions, and I think that's what you would need for more complex matching requirements. A few other functions are available besides LEFT and REGEX, but I don't think any of them provide a more useful solution based on your description: https://toolset.com/documentation/programmer-reference/forms/cred-conditional-display-engine/

Regular expressions are pretty complex and non-trivial, so I don't have a cut-and-paste solution available. You would have to create your own regular expression based on the matching requirements for your specific implementation.

The topic ‘[Closed] Display Shortcode conditionally based on URL’ is closed to new replies.