【发布时间】:2018-03-10 16:13:46
【问题描述】:
我有一个不知道如何解决自己的小问题。我有这个工作功能:
// Custom Field Product
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Text Before Add to Cart:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the before add to cart button text here.',
'woocommerce' )
)
);
woocommerce_wp_textarea_input(
array(
'id' => '_textarea1',
'label' => __( 'Text After Add to Cart:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the after add to cart button text here.',
'woocommerce' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
// Starting Save text
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
// Textarea2
$woocommerce_textarea = $_POST['_textarea1'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea1', esc_html(
$woocommerce_textarea ) );
}
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
// Print content to product
add_action( 'woocommerce_before_add_to_cart_form',
'add_content_after_addtocart_button_func' );
function add_content_after_addtocart_button_func() {
// Echo content.
echo get_post_meta( get_the_ID(), '_textarea', true );
}
add_action( 'woocommerce_after_add_to_cart_form',
'add_content_after_addtocart_button_func1' );
function add_content_after_addtocart_button_func1() {
// Echo content.
echo get_post_meta( get_the_ID(), '_textarea1', true );
}
但是当插入这样的文本时:
<b>random text. bla bla</b>
进入 field 并保存,所有文本都丢失了,product 中什么也没有显示?所以我的问题是......如何在自定义字段中使用有效的 HTML 标签,如粗体、斜体、颜色等?为什么用HTML标签保存文本后所有文本都丢失了?
EDIT : 当替换这个函数时:
update_post_meta( $post_id, '_textarea1', esc_html($woocommerce_textarea ));
用这个:
update_post_meta( $post_id, '_textarea1', esc_textarea( $woocommerce_textarea ) );
打印:<b>blabla</b>
但仍不按应有的方式格式化标签。
【问题讨论】:
标签: php html wordpress woocommerce custom-fields