$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Function returns text instead of html|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Function returns text instead of html

matteradmin9PV0评论
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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far