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& [2] => club=272 )
I think its converting the '&' to &
and that semicolon is used as the delimiter but i'm not sure how to fix it.
I replaced the &
with & before exploding
$cta_array = explode( ';', str_replace( '&', '&', $cta_array[0]) );
That fixed the issue.