php - Function returns text instead of html

admin2025-06-02  1

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I use this function to add prices to my WooCommerce variation dropdown menu. I am trying to wrap the price with a span tag but it's displaying the tags as text.

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');
function display_price_in_variation_option_name( $term ) {
    global $product;
    if ( empty( $term ) ) {
        return $term;
    }
    if ( empty( $product->get_id() ) ) {
        return $term;
    }
    $variation_id = $product->get_children();
    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();
        foreach ( $variation_data as $key => $data ) {
            $display_price = '';
            switch ($id) {
                case 1:
                    $display_price = $_product->get_price() / 12;
                    break;
                case 2:
                    $display_price = $_product->get_price();
                    break;
                case 3:
                    $display_price = $_product->get_price() / 6;
                    break;
                default:
            }           
            if ( $data == $term ) {
                $html = $term;
                $html .= '<span>' . $display_price . '</span>';
                return $html;
            }
        }
    }
    return $term;
}
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I use this function to add prices to my WooCommerce variation dropdown menu. I am trying to wrap the price with a span tag but it's displaying the tags as text.

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');
function display_price_in_variation_option_name( $term ) {
    global $product;
    if ( empty( $term ) ) {
        return $term;
    }
    if ( empty( $product->get_id() ) ) {
        return $term;
    }
    $variation_id = $product->get_children();
    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();
        foreach ( $variation_data as $key => $data ) {
            $display_price = '';
            switch ($id) {
                case 1:
                    $display_price = $_product->get_price() / 12;
                    break;
                case 2:
                    $display_price = $_product->get_price();
                    break;
                case 3:
                    $display_price = $_product->get_price() / 6;
                    break;
                default:
            }           
            if ( $data == $term ) {
                $html = $term;
                $html .= '<span>' . $display_price . '</span>';
                return $html;
            }
        }
    }
    return $term;
}
Share Improve this question edited Mar 1, 2019 at 14:19 Jacob Peattie 44.3k10 gold badges50 silver badges64 bronze badges asked Mar 1, 2019 at 14:14 yourbudweiseryourbudweiser 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can't add HTML inside <option> tags, which the variation dropdowns use. It's invalid HTML.

Permitted content Text, possibly with escaped characters (like é).

https://developer.mozilla/en-US/docs/Web/HTML/Element/option

In addition to Jacob's answer, if you look at where the woocommerce_variation_option_name filter is applied in WooCommerce, you can see this:

<?php echo esc_html( apply_filters( 'woocommerce_variation_option_name', $option->name ) ); ?>

This means that any HTML is escaped via esc_html, so < becomes &lt;, etc.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748850668a314246.html

最新回复(0)