简体   繁体   English

如何使用 esc_html 显示带有 css 的 html 标签?

[英]How to display html tag with css using esc_html?

I am editing the woocommerce orders.php template and ran into a problem.我正在编辑 woocommerce orders.php 模板并遇到了问题。 The template shows to user the orders he has placed.该模板向用户显示他所下的订单。 There are now several variables that I think need to be coded for security, such as $date_created or $view_order which contains the order link.现在有几个变量我认为需要对安全性进行编码,例如包含订单链接的 $date_created 或 $view_order。 So I'm trying to add esc_html to these and other variables but when I do it displays plain text on screen and not html tag with its css.因此,我尝试将 esc_html 添加到这些变量和其他变量中,但是当我这样做时,它会在屏幕上显示纯文本,而不是带有 css 的 html 标记。

Is there a way to use esc_html and keep the output clean so it displays html and css tags normally?有没有办法使用 esc_html 并保持输出干净,以便正常显示 html 和 css 标签? Sorry but I'm new to all this, I'm trying to learn step by step, I hope someone can show me a possible way / solution.对不起,但我对这一切都很陌生,我正在尝试一步一步地学习,我希望有人能告诉我一个可能的方法/解决方案。 I appreciate any help, thanks.我感谢任何帮助,谢谢。

Basically I use this to display variables and everything works fine, the variable is displayed with its css:基本上我用它来显示变量,一切正常,变量用它的css显示: 在此处输入图像描述

<td class="product_data">
  <span>'. $date_created .'</span>
</td>

If I try to do this, the variables is displayed without its css style如果我尝试这样做,则显示的变量没有其 css 样式在此处输入图像描述

<td class="product_data">
  <span><?php echo esc_html($date_created); ?></span>
</td>

Another example, if I have $example = esc_html( '<a href="http://www.example.com/">A link</a>' );另一个例子,如果我有$example = esc_html( '<a href="http://www.example.com/">A link</a>' ); this displayed as <a href="http://www.example.com/">A link</a> instead of A link .这显示为<a href="http://www.example.com/">A link</a>而不是A link Is there any way to solve this problem?有没有办法解决这个问题?

This is my orders.php template : I don't think it matters, but I have entered the complete template.这是我的 orders.php 模板:我认为这并不重要,但我已经输入了完整的模板。

<?php
//* echo do_shortcode('[elementor-template id="40136"]'); *//
?><div class="orders-container"><?php

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders );
                
?><table class="table_orders heading"><tr>
 <td class="product_number">Ordine</td>
 <td class="product_name">Prodotto</td>
 <td class="product_data">Data</td>
 <td class="product_price">Totale</td>
 <td class="product_status">Stato</td>
 <td class="product_action">File</td>
</tr></table><?php

if ( $has_orders ) {
    // Get Access $order variable Foreach
    foreach ( $customer_orders->orders as $customer_order ) {
     // Get $product object from $order / $order_id
     $order = wc_get_order( $customer_order );
     $items = $order->get_items();
     
     $orders_id = $order->get_id();
     $status =  wc_get_order_status_name( $order->get_status() );
     $date_created = $order->get_date_created()->date('d/m/Y');
     $payment_method = $order->get_payment_method_title();
     $order_total = $order->get_formatted_order_total();

        // Get Access Items & Product Variable Foreach
        foreach ( $items as $item ) {
         $product_name = $item->get_name();
         
         // Get product image - https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
         $product = $item->get_product();
            if( $product instanceof WC_Product ){
             $order_img = $product->get_image();
            }
    
         //Get product download button 
         $downloads = $order->get_downloadable_items();
            if(is_array($downloads)) {
                foreach($downloads as $product){
                 $download_button = '<a href="'. $product['download_url'] .'" target="_blank">Download</a>';
                } 
            } 
            
         $view_order = $order->get_view_order_url();
            
            //Start Prov Echo
            ?>
            <td class="product_data">
                <span class="mobile title">Data</span>
                <span><?php echo esc_html($date_created); ?></span>
            </td>
            <?php
            
            // Start echo
            echo '
                <table class="table_orders">
                <tr class="table_row_items">
                    <td class="product_number">
                     <span class="mobile title">Ordine</span>
                     <span>#'. $orders_id .'</span>
                    </td>
    
                    <td class="product_name">
                     <span class="mobile title">Prodotto</span>
                     <a href="'. $view_order .'">'. $product_name .'</a>
                    </td>
    
                    <td class="product_data">
                     <span class="mobile title">Data</span>
                     <span>'. $date_created .'</span>
                    </td>
    
                    <td class="product_price">
                     <span class="mobile title">Prezzo</span>
                     <span>'. $order_total .'</span>
                    </td>
    
                    <td class="product_status">
                     <span class="mobile title">Stato</span>
                     <span>'. $status .'</span>
                    </td>
 
                    <td class="product_action">
                     <span class="mobile title">File</span>
                     <a target=”_blank” href="'. $view_order .'">Visualizza<i class="fa-duotone fa-eye"></i></a>
                    </td>
                </tr>    
                </table> 
            '; //End Echo

             // Tasto download funzionante - if($downloads) { echo '<div class="container_orders_download"> '. $download_button .' </div>'; }
        }
    }
    
    // Pagination button - Responsabile dei bottoni e numerazione delle pagine della cronologia ordini
    ?><div class="container-pagination"><?php 
        $args = array(
         'base'          => esc_url( wc_get_endpoint_url( 'orders') ) . '%_%',
         'format'        => '%#%',
         'total'         => $customer_orders->max_num_pages,
         'current'       => $current_page,
         'show_all'      => false,
         'end_size'      => 3,
         'mid_size'      => 3,
         'prev_next'     => true,
         'prev_text' => __('<i class="fa-solid fa-angle-left"></i>'), 
         'next_text' => __('<i class="fa-solid fa-angle-right"></i>'),
         'type'          => 'plain',
         'add_args'      => false,
         'add_fragment'  => ''
        ); 
        echo paginate_links($args);
}       
        else {
         ?><div class="msg_orders">La tua cronologia ordini è vuota!</div><?php
        } 
    ?></div><?php

do_action( 'woocommerce_after_account_orders', $has_orders ); 

?>
</div>

您可以使用wp_kses_post ,它过滤文本内容并去除不允许的 HTML。

echo wp_kses_post( $date_created );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM