Support

Account

Home Forums ACF PRO Extending the URL field.

Solved

Extending the URL field.

  • I’d like to extend the URL field to prepend the http:// protocol if it is not included in the input. I’m wondering what’s the best way to go about doing this.

  • The ACF field uses WP’s build in link feature that’s used in editors. Doing a quick search on modifying this turned up nothing useful. But if you want to modify the link field in any way you’d need to figure out if it’s at all possible to modify the built in link interface.

  • @hube2, Are you sure you’re not thinking of the Link field type?

    My question relates to the URL input field.

    Essentially I’d just like to add a default http:// protocol if one is not entered instead of failing validation.

    The aim is to adjust for users who assume http://www.whatever.com is a valid URL.

  • I’ve figured out that I need to use the following code…

    However, while the code runs on new fields added via repeaters, it does not run on fields during the initial load.

    I’ve opened a new thread more in line with the issue I am facing…

    https://support.advancedcustomfields.com/forums/topic/acf-javascript-api-initial-new_field-filters-not-working/

    acf.addAction('new_field/type=url', function(field){
          field.$el.find('input[type=url]').on('blur', function(e){
            var val = $(this).val();
            if((!(val.indexOf('://') !== -1)) && (val.length !== 0)){
              $(this).val('http://' + val);
            }
          });
        });
    
  • Yes, I was thinking about the wrong field. You can set the default using

    
    add_filter('acf/prepare_field/type=url', 'url_add_default_prot', 20);
    url_add_default_prot($field) {
      $field['default_value'] = 'http://';
      return $field;
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Extending the URL field.’ is closed to new replies.