Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Set custom attributes to use hyperlinked URLs
- * See for examples: https://nicola.blog/2016/03/11/make-product-attributes-linkable/
- *****************************************************************************************/
- /**
- * Register term fields
- */
- add_action( ‘init’, ‘register_attributes_url_meta’ );
- function register_attributes_url_meta() {
- $attributes = wc_get_attribute_taxonomies();
- foreach ( $attributes as $tax ) {
- $name = wc_attribute_taxonomy_name( $tax->attribute_name );
- add_action( $name . '_add_form_fields', 'add_attribute_url_meta_field' );
- add_action( $name . '_edit_form_fields', 'edit_attribute_url_meta_field', 10 );
- add_action( 'edit_' . $name, 'save_attribute_url' );
- add_action( 'create_' . $name, 'save_attribute_url' );
- }
- }
- /**
- * Add term fields form
- */
- function add_attribute_url_meta_field() {
- wp_nonce_field( basename( __FILE__ ), 'attrbute_url_meta_nonce' );
- ?>
- <div class="form-field">
- <label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label>
- <input type="url" name="attribute_url" id="attribute_url" value="" target="_blank"/>
- </div>
- <?php
- }
- /**
- * Edit term fields form
- */
- function edit_attribute_url_meta_field( $term ) {
- $url = get_term_meta( $term->term_id, ‘attribute_url’, true );
- wp_nonce_field( basename( FILE ), ‘attrbute_url_meta_nonce’ );
- ?>
- <input type="url" name="attribute_url" id="attribute_url" target="_blank" value="” />
- <?php
- }
- /**
- * Save term fields
- */
- function save_attribute_url( $term_id ) {
- if ( ! isset( $_POST[‘attribute_url’] ) || ! wp_verify_nonce( $_POST[‘attrbute_url_meta_nonce’], basename( FILE ) ) ) {
- return;
- }
- $old_url = get_term_meta( $term_id, ‘attribute_url’, true );
- $new_url = esc_url( $_POST[‘attribute_url’] );
- if ( ! empty( $old_url ) && $new_url === ” ) {
- delete_term_meta( $term_id, ‘attribute_url’ );
- } else if ( $old_url !== $new_url ) {
- update_term_meta( $term_id, ‘attribute_url’, $new_url, $old_url );
- }
- }
- /**
- * Show term URL
- */
- add_filter( ‘woocommerce_attribute’, ‘make_product_atts_linkable’, 10, 3 );
- function make_product_atts_linkable( $text, $attribute, $values ) {
- $new_values = array();
- foreach ( $values as $value ) {
- if ( $attribute[‘is_taxonomy’] ) {
- $term = get_term_by( ‘name’, $value, $attribute[‘name’] );
- $url = get_term_meta( $term->term_id, ‘attribute_url’, true );
- if ( ! empty( $url ) ) {
- $val = ‘‘ . $value . ‘‘;
- array_push( $new_values, $val );
- } else {
- array_push( $new_values, $value );
- }
- } else {
- $matched = preg_match_all( “/[([^]]+)](([^)]+))/”, $value, $matches );
- if ( $matched && count( $matches ) == 3 ) {
- $val = ‘‘ . sanitize_text_field( $matches[1][0] ) . ‘‘;
- array_push( $new_values, $val );
- } else {
- array_push( $new_values, $value );
- }
- }
- }
- $text = implode( ‘, ‘, $new_values );
- return $text;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement