Question

I want to allow my users to create a url link and label in a single field, like:

Register Now; www.abc123.com

or just

www.abc123.com

my code looks like this:

$cta_array = explode(";",  $field ) ;
if( count( $cta_array ) > 1 ){
  $cta = '<a href="'.esc_url( $cta_array[1] ).'" class="button '.$color.'">'.esc_html( $cta_array[0] ).'</a>';
}
else{
  $cta = '<a href="'.esc_url( $field ).'" class="button '.$color.'">Select</a>';
}
return $cta;

Only problem is if the url they enter has query strings with ampersands, the ampersands get treated as semicolons by the explode() function so if they enter

Book Now; http://www.abc123.com/scripts/WebObjects.dll/AAAOnline?association=CAA&club=272

$cta_array variable returns this:

Array

( [0] => Book Now [1] => http://www.aaa.com/scripts/WebObjects.dll/AAAOnline?association=CAA&amp; [2] => club=272 )

I think its converting the '&' to &amp; and that semicolon is used as the delimiter but i'm not sure how to fix it.

Was it helpful?

Solution

I replaced the &amp; with & before exploding

$cta_array = explode( ';', str_replace( '&amp;', '&', $cta_array[0]) );

That fixed the issue.

OTHER TIPS

Ask your users to use other delimiter such as comma. Or change delimiter programmatically.

$cta_array = explode(“|”,  str_replace(‘;’,’|’,$field));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top