Advertisement
BaapJaan

Clickable Attribute - wp

Apr 8th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. /**
  2. * Register term fields
  3. */
  4. add_action( 'init', 'register_attributes_url_meta' );
  5. function register_attributes_url_meta() {
  6. $attributes = wc_get_attribute_taxonomies();
  7.  
  8. foreach ( $attributes as $tax ) {
  9. $name = wc_attribute_taxonomy_name( $tax->attribute_name );
  10.  
  11. add_action( $name . '_add_form_fields', 'add_attribute_url_meta_field' );
  12. add_action( $name . '_edit_form_fields', 'edit_attribute_url_meta_field', 10 );
  13. add_action( 'edit_' . $name, 'save_attribute_url' );
  14. add_action( 'create_' . $name, 'save_attribute_url' );
  15. }
  16. }
  17.  
  18. /**
  19. * Add term fields form
  20. */
  21. function add_attribute_url_meta_field() {
  22.  
  23. wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
  24. ?>
  25.  
  26. <div class="form-field">
  27. <label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label>
  28. <input type="url" name="attribute_url" id="attribute_url" value="" />
  29. </div>
  30. <?php
  31. }
  32.  
  33. /**
  34. * Edit term fields form
  35. */
  36. function edit_attribute_url_meta_field( $term ) {
  37.  
  38. $url = get_term_meta( $term->term_id, 'attribute_url', true );
  39. wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
  40. ?>
  41. <tr class="form-field">
  42. <th scope="row" valign="top"><label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label></th>
  43. <td>
  44. <input type="url" name="attribute_url" id="attribute_url" value="<?php echo esc_url( $url ); ?>" />
  45. </td>
  46. </tr>
  47. <?php
  48. }
  49.  
  50. /**
  51. * Save term fields
  52. */
  53. function save_attribute_url( $term_id ) {
  54. if ( ! isset( $_POST['attribute_url'] ) || ! wp_verify_nonce( $_POST['attrbute_url_meta_nonce'], basename( __FILE__ ) ) ) {
  55. return;
  56. }
  57.  
  58. $old_url = get_term_meta( $term_id, 'attribute_url', true );
  59. $new_url = esc_url( $_POST['attribute_url'] );
  60.  
  61.  
  62. if ( ! empty( $old_url ) && $new_url === '' ) {
  63. delete_term_meta( $term_id, 'attribute_url' );
  64. } else if ( $old_url !== $new_url ) {
  65. update_term_meta( $term_id, 'attribute_url', $new_url, $old_url );
  66. }
  67. }
  68.  
  69. /**
  70. * Show term URL
  71. */
  72. add_filter( 'woocommerce_attribute', 'make_product_atts_linkable', 10, 3 );
  73. function make_product_atts_linkable( $text, $attribute, $values ) {
  74. $new_values = array();
  75. foreach ( $values as $value ) {
  76.  
  77. if ( $attribute['is_taxonomy'] ) {
  78. $term = get_term_by( 'name', $value, $attribute['name'] );
  79. $url = get_term_meta( $term->term_id, 'attribute_url', true );
  80.  
  81. if ( ! empty( $url ) ) {
  82. $val = '<a href="' . esc_url( $url ) . '" title="' . esc_attr( $value ) . '">' . $value . '</a>';
  83. array_push( $new_values, $val );
  84. } else {
  85. array_push( $new_values, $value );
  86. }
  87. } else {
  88. $matched = preg_match_all( "/\[([^\]]+)\]\(([^)]+)\)/", $value, $matches );
  89.  
  90. if ( $matched && count( $matches ) == 3 ) {
  91. $val = '<a href="' . esc_url( $matches[2][0] ) . '" title="' . esc_attr( $matches[1][0] ) . '">' . sanitize_text_field( $matches[1][0] ) . '</a>';
  92. array_push( $new_values, $val );
  93. } else {
  94. array_push( $new_values, $value );
  95. }
  96. }
  97. }
  98.  
  99. $text = implode( ', ', $new_values );
  100.  
  101. return $text;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement