Skip Navigation

[Resolved] Evaluate / match substring with wp-if

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.

This topic contains 5 replies, has 2 voices.

Last updated by Christian 7 years, 8 months ago.

Assigned support staff: Caridad.

Author
Posts
#27759

I'm using this statement to display a link if the field isn't empty:

[wpv-if url="wpcf-my-url" evaluate="!empty($url)"]
[types field="my-ur" title="Visit the page"][/types]
[/wpv-if]

Now we've noticed that some data is inccorect: the urls are missing a leading 'hidden link' - is it any way to $url to see if it contains 'hidden link' ?

For example (psudocode):

<cite>
I'm using this statement to display a link if the field isn't empty:

[wpv-if url="wpcf-my-url" evaluate="!empty($url)"]
[types field="my-ur" title="Visit the page"][/types]
[/wpv-if]

Now we've noticed that some data is inccorect: the urls are missing a leading 'hidden link' - is it any way to $url to see if it contains 'hidden link' ?

For example (psudocode):

[wpv-if url="wpcf-my-url" evaluate="!empty($url)"]
[wpv-if url="wpcf-my-url" evaluate="contains($url,'<em><u>hidden link</u></em>')"]
  [types field="my-ur" title="Visit the page"][/types]
[/wpv-if]
[wpv-if url="wpcf-my-url" evaluate="!contains($url,'<em><u>hidden link</u></em>)"]
  <a href="<em><u>hidden link</u></em> field="my-url" raw="true][/types]">Visit the page</a>
[/wpv-if]
[/wpv-if]
#27763

My psudeo-code got formatted strange: attaching a image here 🙂

#27818

Dear Christian,

A url field should always have the protocol in front (hidden link). What you can do is to enable URL validation in that field so that an error message will be displayed if http:// is missing.

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#27823

Caridad: thanks, but in this I don't have control of the source data. Sure it is possible to modify the database directly or using a meta field filter - but I was wondering if it is possible to solve it using a substring matching in the template.

If not, it's fine - but since the feature could also be useful in many other cases (not directly related to urls).

Just to make sure - there is no way to do a substring matching using wpv-if?

#27851

Dear Christian,

No, there isn't. I can only see the empty() function working in there. What you can do is to attach to a filter and do the evaluation yourself, something like this should work in functions.php of your theme:

add_filter('wpv-extra-condition-filters', 'wpv_add_time_functions');
function wpv_add_time_functions($value) {
$value = eval('return (' . $value . ');');
return $value?'1=1':'1=0';
}

You have to use normal PHP syntax this time:

[wpv-if url="wpcf-my-url" evaluate="substr($url,0,7)!='<em><u>hidden link</u></em>'"]<em><u>hidden link</u></em> field="my-url"][/types]

Please let me know if there is anything else that I can assist you with.

Regards,
Caridad

#27864

@caridadz - ah, a real sweet solution (even if I can't let the users use raw php code in this particular case). Thanks 🙂