从我的自定义URL Meta字段和分类法中获取URL以获取简码

时间:2019-09-30 08:46:51

标签: php wordpress woocommerce hook-woocommerce

更新:感谢您可爱的人们的帮助,我设法为其中的短码部分争用了一个解决方案。进一步查看解决方案。

我正在努力以自己的属性以我的设置的短码版本输出URL。硒的最后一部分。我已经将用于实现的所有代码分成1个文件以供参考。查看产品时,它的效果很好,很完美,但是当我用简码输出list属性列表时,效果却不那么理想。似乎无法显示网址。

对于添加到WooCommerce属性中的URL字段,我具有以下代码设置。

<?php
/**
 * 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="" />
    </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' );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="attribute_url"><?php _e( 'URL', 'domain' ); ?></label></th>
        <td>
            <input type="url" name="attribute_url" id="attribute_url" value="<?php echo esc_url( $url ); ?>" />
        </td>
    </tr>
    <?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 = '<a target="_blank" href="' . esc_url( $url ) . '" title="' . esc_attr( $value ) . '">' . $value . '</a>';
                array_push( $new_values, $val );
            } else {
                array_push( $new_values, $value );
            }
        } else {
            $matched = preg_match_all( "/\[([^\]]+)\]\(([^)]+)\)/", $value, $matches );
            if ( $matched && count( $matches ) == 3 ) {
                $val = '<a target="_blank" href="' . esc_url( $matches[2][0] ) . '" title="' . esc_attr( $matches[1][0] ) . '">' . sanitize_text_field( $matches[1][0] ) . '</a>';
                array_push( $new_values, $val );
            } else {
                array_push( $new_values, $value );
            }
        }
    }
    $text = implode( ', ', $new_values );
    return $text;
}

// CUSTOM SHORTCODE FOR ATTR
add_shortcode( 'product_attributes', 'get_product_attributes' );
function get_product_attributes() {
    $url = get_term_meta( $term->term_id, 'attribute_url', true );

    $taxonomies = get_terms( array(
        'taxonomy' => 'pa_normerstandarder',
        'hide_empty' => false
    ) );

    if ( !empty($taxonomies) ) :
        $output = '<table class="standarder-tabell"><thead> <th>Norm/standard</th> <th>Beskrivelse</th> <th>Url</th> </thead><tbody>';

        foreach( $taxonomies as $category ) {
            if( $category->parent == 0 ) {
                $output.= '<tr class="norm-'. esc_attr( $category->name ) .'">';
                $output.= '<td>'.esc_attr( $category->name ). '</td>';
                $output.= '<td>'.esc_attr( $category->description ). '</td>';
                $output.= '<td>' . esc_url( $url ) . '</td>';
                $output.='</tr>';
            }
        }
        $output.='</tbody></table>';
    endif;


    return $output;
}

解决方案(更改为SHORTCODE部分)

// CUSTOM SHORTCODE FOR ATTR
add_shortcode( 'product_attributes', 'get_product_attributes' );

function get_product_attributes() {

    $terms = get_terms( array(
        'taxonomy' => 'pa_normerstandarder',
        'hide_empty' => false
    ) );


    if ( !empty($terms) ) :
        $output = '<table class="standarder-tabell"><thead> <th>Norm/standard</th> <th>Beskrivelse</th> <th>Url</th> </thead><tbody>';

        foreach( $terms as $term ) {
            $url = get_term_meta( $term->term_id, 'attribute_url', true );
            if( $category->parent == 0 ) {
                $output.= '<tr class="norm-'. esc_attr( $term->name ) .'">';
                $output.= '<td>'.esc_attr( $term->name ). '</td>';
                $output.= '<td>'.esc_attr( $term->description ). '</td>';
                $output.= '<td>'.esc_attr( $url ). '</td>';
                $output.='</tr>';
            }
        }
        $output.='</tbody></table>';
    endif;


    return $output;
}

0 个答案:

没有答案