SHARE
TWEET

Untitled

a guest Apr 10th, 2017 169 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Set custom attributes to use hyperlinked URLs
  3. * See for examples: https://nicola.blog/2016/03/11/make-product-attributes-linkable/
  4. *****************************************************************************************/
  5. /**
  6. * Register term fields
  7. */
  8. add_action( ‘init’, ‘register_attributes_url_meta’ );
  9. function register_attributes_url_meta() {
  10. $attributes = wc_get_attribute_taxonomies();
  11.  
  12. foreach ( $attributes as $tax ) {
  13. $name = wc_attribute_taxonomy_name( $tax->attribute_name );
  14.  
  15. add_action( $name . '_add_form_fields', 'add_attribute_url_meta_field' );
  16. add_action( $name . '_edit_form_fields', 'edit_attribute_url_meta_field', 10 );
  17. add_action( 'edit_' . $name, 'save_attribute_url' );
  18. add_action( 'create_' . $name, 'save_attribute_url' );
  19. }
  20.  
  21. }
  22.  
  23. /**
  24. * Add term fields form
  25. */
  26. function add_attribute_url_meta_field() {
  27.  
  28. wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
  29. ?>
  30.  
  31. <div class="form-field">
  32. <label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label>
  33. <input type="url" name="attribute_url" id="attribute_url" value="" target="_blank"/>
  34. </div>
  35. <?php
  36.  
  37. }
  38. /**
  39. * Edit term fields form
  40. */
  41. function edit_attribute_url_meta_field( $term ) {
  42. $url = get_term_meta( $term->term_id, ‘attribute_url’, true );
  43. wp_nonce_field( basename( FILE ), ‘attrbute_url_meta_nonce’ );
  44. ?>
  45.  
  46. <input type="url" name="attribute_url" id="attribute_url" target="_blank" value="” />
  47.  
  48. <?php
  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. $old_url = get_term_meta( $term_id, ‘attribute_url’, true );
  58. $new_url = esc_url( $_POST[‘attribute_url’] );
  59. if ( ! empty( $old_url ) && $new_url ===) {
  60. delete_term_meta( $term_id, ‘attribute_url’ );
  61. } else if ( $old_url !== $new_url ) {
  62. update_term_meta( $term_id, ‘attribute_url’, $new_url, $old_url );
  63. }
  64. }
  65. /**
  66. * Show term URL
  67. */
  68. add_filter( ‘woocommerce_attribute’, ‘make_product_atts_linkable’, 10, 3 );
  69. function make_product_atts_linkable( $text, $attribute, $values ) {
  70. $new_values = array();
  71. foreach ( $values as $value ) {
  72. if ( $attribute[‘is_taxonomy’] ) {
  73. $term = get_term_by( ‘name’, $value, $attribute[‘name’] );
  74. $url = get_term_meta( $term->term_id, ‘attribute_url’, true );
  75. if ( ! empty( $url ) ) {
  76. $val = ‘‘ . $value . ‘‘;
  77. array_push( $new_values, $val );
  78. } else {
  79. array_push( $new_values, $value );
  80. }
  81. } else {
  82. $matched = preg_match_all(/[([^]]+)](([^)]+))/, $value, $matches );
  83. if ( $matched && count( $matches ) == 3 ) {
  84. $val = ‘‘ . sanitize_text_field( $matches[1][0] ) . ‘‘;
  85. array_push( $new_values, $val );
  86. } else {
  87. array_push( $new_values, $value );
  88. }
  89. }
  90. }
  91. $text = implode(,, $new_values );
  92. return $text;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top