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-link.php
349 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_link' ) ) : |
| 13 | |
| 14 | class acf_field_link extends acf_field { |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * This function will setup the field type data |
| 19 | * |
| 20 | * @type function |
| 21 | * @date 5/03/2014 |
| 22 | * @since 5.0.0 |
| 23 | * |
| 24 | * @param n/a |
| 25 | * @return n/a |
| 26 | */ |
| 27 | function initialize() { |
| 28 | |
| 29 | // vars |
| 30 | $this->name = 'link'; |
| 31 | $this->label = __( 'Link', 'acf' ); |
| 32 | $this->category = 'relational'; |
| 33 | $this->description = __( 'Allows you to specify a link and its properties such as title and target using the WordPress native link picker.', 'acf' ); |
| 34 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-link.png'; |
| 35 | $this->doc_url = acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/resources/link/', 'docs', 'field-type-selection' ); |
| 36 | $this->defaults = array( |
| 37 | 'return_format' => 'array', |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * description |
| 44 | * |
| 45 | * @type function |
| 46 | * @date 16/5/17 |
| 47 | * @since 5.5.13 |
| 48 | * |
| 49 | * @param $post_id (int) |
| 50 | * @return $post_id (int) |
| 51 | */ |
| 52 | function get_link( $value = '' ) { |
| 53 | |
| 54 | // vars |
| 55 | $link = array( |
| 56 | 'title' => '', |
| 57 | 'url' => '', |
| 58 | 'target' => '', |
| 59 | ); |
| 60 | |
| 61 | // array (ACF 5.6.0) |
| 62 | if ( is_array( $value ) ) { |
| 63 | $link = array_merge( $link, $value ); |
| 64 | |
| 65 | // post id (ACF < 5.6.0) |
| 66 | } elseif ( is_numeric( $value ) ) { |
| 67 | $link['title'] = get_the_title( $value ); |
| 68 | $link['url'] = get_permalink( $value ); |
| 69 | |
| 70 | // string (ACF < 5.6.0) |
| 71 | } elseif ( is_string( $value ) ) { |
| 72 | $link['url'] = $value; |
| 73 | } |
| 74 | |
| 75 | // return |
| 76 | return $link; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Create the HTML interface for your field |
| 82 | * |
| 83 | * @param $field - an array holding all the field's data |
| 84 | * |
| 85 | * @type action |
| 86 | * @since 3.6 |
| 87 | */ |
| 88 | public function render_field( $field ) { |
| 89 | |
| 90 | // vars |
| 91 | $div = array( |
| 92 | 'id' => $field['id'], |
| 93 | 'class' => $field['class'] . ' acf-link', |
| 94 | ); |
| 95 | |
| 96 | // render scripts/styles |
| 97 | acf_enqueue_uploader(); |
| 98 | |
| 99 | // get link |
| 100 | $link = $this->get_link( $field['value'] ); |
| 101 | |
| 102 | // classes |
| 103 | if ( $link['url'] ) { |
| 104 | $div['class'] .= ' -value'; |
| 105 | } |
| 106 | |
| 107 | if ( $link['target'] === '_blank' ) { |
| 108 | $div['class'] .= ' -external'; |
| 109 | } |
| 110 | ?> |
| 111 | <div <?php echo acf_esc_attrs( $div ); ?>> |
| 112 | |
| 113 | <div class="acf-hidden"> |
| 114 | <a class="link-node" href="<?php echo esc_url( $link['url'] ); ?>" target="<?php echo esc_attr( $link['target'] ); ?>"><?php echo esc_html( $link['title'] ); ?></a> |
| 115 | <?php foreach ( $link as $k => $v ) : ?> |
| 116 | <?php |
| 117 | acf_hidden_input( |
| 118 | array( |
| 119 | 'class' => "input-$k", |
| 120 | 'name' => $field['name'] . "[$k]", |
| 121 | 'value' => $v, |
| 122 | ) |
| 123 | ); |
| 124 | ?> |
| 125 | <?php endforeach; ?> |
| 126 | </div> |
| 127 | |
| 128 | <a href="#" class="button" data-name="add" target=""><?php esc_html_e( 'Select Link', 'acf' ); ?></a> |
| 129 | |
| 130 | <div class="link-wrap"> |
| 131 | <span class="link-title"><?php echo esc_html( $link['title'] ); ?></span> |
| 132 | <a class="link-url" href="<?php echo esc_url( $link['url'] ); ?>" target="_blank"><?php echo esc_html( $link['url'] ); ?></a> |
| 133 | <i class="acf-icon -link-ext acf-js-tooltip" title="<?php esc_attr_e( 'Opens in a new window/tab', 'acf' ); ?>"></i><a class="acf-icon -pencil -clear acf-js-tooltip" data-name="edit" href="#" title="<?php esc_attr_e( 'Edit', 'acf' ); ?>"></a><a class="acf-icon -cancel -clear acf-js-tooltip" data-name="remove" href="#" title="<?php esc_attr_e( 'Remove', 'acf' ); ?>"></a> |
| 134 | </div> |
| 135 | |
| 136 | </div> |
| 137 | <?php |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * Create extra options for your field. This is rendered when editing a field. |
| 143 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 144 | * |
| 145 | * @type action |
| 146 | * @since 3.6 |
| 147 | * @date 23/01/13 |
| 148 | * |
| 149 | * @param $field - an array holding all the field's data |
| 150 | */ |
| 151 | function render_field_settings( $field ) { |
| 152 | acf_render_field_setting( |
| 153 | $field, |
| 154 | array( |
| 155 | 'label' => __( 'Return Value', 'acf' ), |
| 156 | 'instructions' => __( 'Specify the returned value on front end', 'acf' ), |
| 157 | 'type' => 'radio', |
| 158 | 'name' => 'return_format', |
| 159 | 'layout' => 'horizontal', |
| 160 | 'choices' => array( |
| 161 | 'array' => __( 'Link Array', 'acf' ), |
| 162 | 'url' => __( 'Link URL', 'acf' ), |
| 163 | ), |
| 164 | ) |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
| 170 | * |
| 171 | * @type filter |
| 172 | * @since 3.6 |
| 173 | * @date 23/01/13 |
| 174 | * |
| 175 | * @param $value (mixed) the value which was loaded from the database |
| 176 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 177 | * @param $field (array) the field array holding all the field options |
| 178 | * |
| 179 | * @return $value (mixed) the modified value |
| 180 | */ |
| 181 | function format_value( $value, $post_id, $field ) { |
| 182 | |
| 183 | // bail early if no value |
| 184 | if ( empty( $value ) ) { |
| 185 | return $value; |
| 186 | } |
| 187 | |
| 188 | // get link |
| 189 | $link = $this->get_link( $value ); |
| 190 | |
| 191 | // format value |
| 192 | if ( $field['return_format'] == 'url' ) { |
| 193 | return $link['url']; |
| 194 | } |
| 195 | |
| 196 | // return link |
| 197 | return $link; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /** |
| 202 | * description |
| 203 | * |
| 204 | * @type function |
| 205 | * @date 11/02/2014 |
| 206 | * @since 5.0.0 |
| 207 | * |
| 208 | * @param $post_id (int) |
| 209 | * @return $post_id (int) |
| 210 | */ |
| 211 | function validate_value( $valid, $value, $field, $input ) { |
| 212 | |
| 213 | // bail early if not required |
| 214 | if ( ! $field['required'] ) { |
| 215 | return $valid; |
| 216 | } |
| 217 | |
| 218 | // URL is required |
| 219 | if ( empty( $value ) || empty( $value['url'] ) ) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | // return |
| 224 | return $valid; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | /** |
| 229 | * This filter is appied to the $value before it is updated in the db |
| 230 | * |
| 231 | * @type filter |
| 232 | * @since 3.6 |
| 233 | * @date 23/01/13 |
| 234 | * |
| 235 | * @param $value - the value which will be saved in the database |
| 236 | * @param $post_id - the post_id of which the value will be saved |
| 237 | * @param $field - the field array holding all the field options |
| 238 | * |
| 239 | * @return $value - the modified value |
| 240 | */ |
| 241 | function update_value( $value, $post_id, $field ) { |
| 242 | |
| 243 | // Check if value is an empty array and convert to empty string. |
| 244 | if ( empty( $value ) || empty( $value['url'] ) ) { |
| 245 | $value = ''; |
| 246 | } |
| 247 | |
| 248 | // return |
| 249 | return $value; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Return the schema array for the REST API. |
| 254 | * |
| 255 | * @param array $field |
| 256 | * @return array |
| 257 | */ |
| 258 | public function get_rest_schema( array $field ) { |
| 259 | return array( |
| 260 | 'type' => array( 'object', 'null' ), |
| 261 | 'required' => ! empty( $field['required'] ), |
| 262 | 'properties' => array( |
| 263 | 'title' => array( |
| 264 | 'type' => 'string', |
| 265 | ), |
| 266 | 'url' => array( |
| 267 | 'type' => 'string', |
| 268 | 'required' => true, |
| 269 | 'format' => 'uri', |
| 270 | ), |
| 271 | 'target' => array( |
| 272 | 'type' => 'string', |
| 273 | ), |
| 274 | ), |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 280 | * |
| 281 | * @since 6.8 |
| 282 | * |
| 283 | * @return string[] |
| 284 | */ |
| 285 | public function get_jsonld_output_types(): array { |
| 286 | return array( 'URL', 'WebPage' ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Formats the field value for JSON-LD output. |
| 291 | * |
| 292 | * @since 6.8.0 |
| 293 | * |
| 294 | * @param mixed $value The value of the field. |
| 295 | * @param integer|string $post_id The ID of the post. |
| 296 | * @param array $field The field array. |
| 297 | * @return mixed |
| 298 | */ |
| 299 | public function format_value_for_jsonld( $value, $post_id, $field ) { |
| 300 | if ( empty( $value ) ) { |
| 301 | return null; |
| 302 | } |
| 303 | |
| 304 | // Get link data. |
| 305 | $link = $this->get_link( $value ); |
| 306 | |
| 307 | if ( empty( $link['url'] ) ) { |
| 308 | return null; |
| 309 | } |
| 310 | |
| 311 | // Get output format with fallback. |
| 312 | $output_format = $field['schema_output_format'] ?? ''; |
| 313 | if ( empty( $output_format ) ) { |
| 314 | $property = $field['schema_property'] ?? ''; |
| 315 | $output_format = \ACF\AI\GEO\Schema::get_default_output_format( $this->name, $property ); |
| 316 | } |
| 317 | |
| 318 | // Default to URL if no format determined. |
| 319 | if ( empty( $output_format ) ) { |
| 320 | $output_format = 'URL'; |
| 321 | } |
| 322 | |
| 323 | // URL format - just return the URL string. |
| 324 | if ( 'URL' === $output_format ) { |
| 325 | return $link['url']; |
| 326 | } |
| 327 | |
| 328 | // WebPage format - return structured object. |
| 329 | $webpage = array( |
| 330 | '@type' => 'WebPage', |
| 331 | 'url' => $link['url'], |
| 332 | ); |
| 333 | |
| 334 | // Add title as name if available. |
| 335 | if ( ! empty( $link['title'] ) ) { |
| 336 | $webpage['name'] = $link['title']; |
| 337 | } |
| 338 | |
| 339 | return $webpage; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | // initialize |
| 345 | acf_register_field_type( 'acf_field_link' ); |
| 346 | endif; // class_exists check |
| 347 | |
| 348 | ?> |
| 349 |