Home › Forums › General Issues › URL Field – tel: link rather than http: › Reply To: URL Field – tel: link rather than http:
The URL will only take a web page URL and there isn’t a way to alter this.
I use the following to add a setting to use text field an validate
<?php
add_action('acf/render_field_validation_settings/type=text', 'validate_text_as_href_setting');
add_action('acf/validate_value/type=text', 'validate_text_as_href', 10, 4);
function validate_text_as_href_setting($field) {
// this adds a setting to text fields
// setting it to true will require that the content of the fields
// meets requirements to be used as a link href value
$args = array(
'label' => 'Validate as HREF',
'instructions' => 'Require a valid href attribute. This includes values starting with: http://, https://, ftp://, mailto:, tel:, sms:, /, and #',
'type' => 'true_false',
'name' => 'validate_href',
'ui' => 1,
'class' => ''
);
acf_render_field_setting($field, $args);
}
function validate_text_as_href($valid, $value, $field, $input) {
// if the setting created by validate_text_as_href_setting
// is true than this filter will test input to ensure it can
// be used in as a link href value
// this allows links starting with
// / (site root relative), http://, https://, ftp://, # (anchor), mailto:, tel:, sms:
if (!$valid) {
return $valid;
}
// does setting exist and is it set
if (!empty($field['validate_href'])) {
// only validate if value submitted (use acf required setting to require value)
if (!empty($value)) {
// allow anything that looks like a valid href value
if (!preg_match('%^(https?\://|ftp\://|/|#|mailto\:|sms\:|tel\:)%', $value)) {
$valid = 'Enter a Valid Link HREF Value';
}
}
}
return $valid;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.