class-acf-field-accordion.php
5 months ago
class-acf-field-button-group.php
5 months ago
class-acf-field-checkbox.php
2 months ago
class-acf-field-color_picker.php
5 months ago
class-acf-field-date_picker.php
5 months ago
class-acf-field-date_time_picker.php
5 months ago
class-acf-field-email.php
5 months ago
class-acf-field-file.php
5 months ago
class-acf-field-google-map.php
5 months ago
class-acf-field-group.php
5 months ago
class-acf-field-icon_picker.php
6 months ago
class-acf-field-image.php
1 month ago
class-acf-field-link.php
5 months ago
class-acf-field-message.php
6 months ago
class-acf-field-number.php
5 months ago
class-acf-field-oembed.php
2 months ago
class-acf-field-output.php
6 months ago
class-acf-field-page_link.php
1 month ago
class-acf-field-password.php
5 months ago
class-acf-field-post_object.php
1 month ago
class-acf-field-radio.php
2 months ago
class-acf-field-range.php
5 months ago
class-acf-field-relationship.php
1 month ago
class-acf-field-select.php
2 months ago
class-acf-field-separator.php
6 months ago
class-acf-field-tab.php
6 months ago
class-acf-field-taxonomy.php
2 months ago
class-acf-field-text.php
5 months ago
class-acf-field-textarea.php
5 months ago
class-acf-field-time_picker.php
5 months ago
class-acf-field-true_false.php
5 months ago
class-acf-field-url.php
5 months ago
class-acf-field-user.php
2 months ago
class-acf-field-wysiwyg.php
5 months ago
class-acf-field.php
5 months ago
index.php
2 years ago
class-acf-field-url.php
201 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | if ( ! class_exists( 'acf_field_url' ) ) : |
| 13 | |
| 14 | /** |
| 15 | * The URL field class. |
| 16 | */ |
| 17 | class acf_field_url extends acf_field { |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * This function will setup the field type data |
| 22 | * |
| 23 | * @since 5.0.0 |
| 24 | */ |
| 25 | public function initialize() { |
| 26 | // vars |
| 27 | $this->name = 'url'; |
| 28 | $this->label = __( 'URL', 'acf' ); |
| 29 | $this->description = __( 'A text input specifically designed for storing web addresses.', 'acf' ); |
| 30 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-url.png'; |
| 31 | $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/url/', 'docs', 'field-type-selection' ); |
| 32 | $this->defaults = array( |
| 33 | 'default_value' => '', |
| 34 | 'placeholder' => '', |
| 35 | ); |
| 36 | $this->supports = array( |
| 37 | 'escaping_html' => true, |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Create the HTML interface for your field |
| 44 | * |
| 45 | * @since 3.6 |
| 46 | * |
| 47 | * @param array $field An array holding all the field's data. |
| 48 | */ |
| 49 | public function render_field( $field ) { |
| 50 | $atts = array(); |
| 51 | $keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'pattern' ); |
| 52 | $keys2 = array( 'readonly', 'disabled', 'required' ); |
| 53 | |
| 54 | // atts (value="123") |
| 55 | foreach ( $keys as $k ) { |
| 56 | if ( isset( $field[ $k ] ) ) { |
| 57 | $atts[ $k ] = $field[ $k ]; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // atts2 (disabled="disabled") |
| 62 | foreach ( $keys2 as $k ) { |
| 63 | if ( ! empty( $field[ $k ] ) ) { |
| 64 | $atts[ $k ] = $k; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // remove empty atts |
| 69 | $atts = acf_clean_atts( $atts ); |
| 70 | |
| 71 | // render |
| 72 | $html = '<div class="acf-input-wrap acf-url">'; |
| 73 | $html .= '<i class="acf-icon -globe -small"></i>' . acf_get_text_input( $atts ); |
| 74 | $html .= '</div>'; |
| 75 | |
| 76 | // return |
| 77 | echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML, escaped by acf_get_text_input. |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Create extra options for your field. This is rendered when editing a field. |
| 83 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 84 | * |
| 85 | * @since 3.6 |
| 86 | * |
| 87 | * @param array $field An array holding all the field's data. |
| 88 | */ |
| 89 | public function render_field_settings( $field ) { |
| 90 | acf_render_field_setting( |
| 91 | $field, |
| 92 | array( |
| 93 | 'label' => __( 'Default Value', 'acf' ), |
| 94 | 'instructions' => __( 'Appears when creating a new post', 'acf' ), |
| 95 | 'type' => 'text', |
| 96 | 'name' => 'default_value', |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Renders the field settings used in the "Presentation" tab. |
| 103 | * |
| 104 | * @since 6.0 |
| 105 | * |
| 106 | * @param array $field The field settings array. |
| 107 | * @return void |
| 108 | */ |
| 109 | public function render_field_presentation_settings( $field ) { |
| 110 | acf_render_field_setting( |
| 111 | $field, |
| 112 | array( |
| 113 | 'label' => __( 'Placeholder Text', 'acf' ), |
| 114 | 'instructions' => __( 'Appears within the input', 'acf' ), |
| 115 | 'type' => 'text', |
| 116 | 'name' => 'placeholder', |
| 117 | ) |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * Validate the fields value is correctly formatted as a URL |
| 124 | * |
| 125 | * @since 5.0.0 |
| 126 | * |
| 127 | * @param mixed $valid The current validity of the field value. Boolean true if valid, a validation error message string if not. |
| 128 | * @param string $value The value of the field. |
| 129 | * @param array $field Field object array. |
| 130 | * @param string $input The form input name for this field. |
| 131 | * @return mixed Boolean true if valid, a validation error message string if not. |
| 132 | */ |
| 133 | public function validate_value( $valid, $value, $field, $input ) { |
| 134 | |
| 135 | // bail early if empty |
| 136 | if ( empty( $value ) ) { |
| 137 | return $valid; |
| 138 | } |
| 139 | |
| 140 | if ( strpos( $value, '://' ) !== false ) { |
| 141 | |
| 142 | // url |
| 143 | } elseif ( strpos( $value, '//' ) === 0 ) { |
| 144 | |
| 145 | // protocol relative url |
| 146 | } else { |
| 147 | $valid = __( 'Value must be a valid URL', 'acf' ); |
| 148 | } |
| 149 | |
| 150 | // return |
| 151 | return $valid; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * This filter is applied to the $value after it is loaded from the db, and before it is returned to the template |
| 156 | * |
| 157 | * @since 6.2.6 |
| 158 | * |
| 159 | * @param mixed $value The value which was loaded from the database. |
| 160 | * @param mixed $post_id The $post_id from which the value was loaded. |
| 161 | * @param array $field The field array holding all the field options. |
| 162 | * @param boolean $escape_html Should the field return a HTML safe formatted value. |
| 163 | * @return mixed $value The modified value |
| 164 | */ |
| 165 | public function format_value( $value, $post_id, $field, $escape_html ) { |
| 166 | if ( $escape_html ) { |
| 167 | return esc_url( $value ); |
| 168 | } |
| 169 | return $value; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Return the schema array for the REST API. |
| 174 | * |
| 175 | * @param array $field The field object. |
| 176 | * @return array |
| 177 | */ |
| 178 | public function get_rest_schema( array $field ) { |
| 179 | $schema = parent::get_rest_schema( $field ); |
| 180 | $schema['format'] = 'uri'; |
| 181 | |
| 182 | return $schema; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 187 | * |
| 188 | * @since 6.8 |
| 189 | * |
| 190 | * @return string[] |
| 191 | */ |
| 192 | public function get_jsonld_output_types(): array { |
| 193 | return array( 'URL' ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | |
| 198 | // initialize |
| 199 | acf_register_field_type( 'acf_field_url' ); |
| 200 | endif; // class_exists check |
| 201 |