【问题标题】:Woocommerce Product Custom Field for iframe not work correctlyiframe 的 Woocommerce 产品自定义字段无法正常工作
【发布时间】:2021-12-30 06:25:14
【问题描述】:

我通过以下代码为 woocommerce 产品创建了一个自定义字段。我想使用此自定义字段来存储 iframe 代码并将其显示在产品单页中。 但是存储在数据库中的数据和输出代码有错误,并且与 xustom 字段文本输入中输入的代码不同

我的代码:

add_action('woocommerce_product_options_sku', 'add_product_video_field' );
function add_product_video_field(){

    woocommerce_wp_text_input( array(
        'id'          => '_product_video_field',
        'label'       => __('video iframe code', 'woocommerce' ),
        'placeholder' => __('video iframe code', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('video iframe code.', 'woocommerce' ),
    ) );
}

add_action('woocommerce_admin_process_product_object', 'save_product_video_field', 10, 1 );
function save_product_video_field( $product ){
    if( isset($_POST['_product_video_field']) )
        $product->update_meta_data( '_product_video_field', esc_attr($_POST['_product_video_field']) );
}

我在输入文本字段中输入的代码:

<style>.h_iframe-aparat_embed_frame{position:relative;}.h_iframe-aparat_embed_frame .ratio{display:block;width:100%;height:auto;}.h_iframe-aparat_embed_frame iframe{position:absolute;top:0;left:0;width:100%;height:100%;}</style><div class="h_iframe-aparat_embed_frame"><span style="display: block;padding-top: 57%"></span><iframe src="https://www.aparat.com/video/video/embed/videohash/AtP2j/vt/frame" title="بِیبی میخواد آپاراتِ کودک" allowFullScreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe></div>

我的自定义字段输出并存储在数据库中:

<style>.h_iframe-aparat_embed_frame{position:relative;}.h_iframe-aparat_embed_frame .ratio{display:block;width:100%;height:auto;}.h_iframe-aparat_embed_frame iframe{position:absolute;top:0;left:0;width:100%;height:100%;}</style><div class=\"h_iframe-aparat_embed_frame\"><span style=\"display: block;padding-top: 57%\"></span><iframe src=\"https://www.aparat.com/video/video/embed/videohash/AtP2j/vt/frame\" title=\"بِیبی میخواد آپاراتِ کودک\" allowFullScreen=\"true\" webkitallowfullscreen=\"true\" mozallowfullscreen=\"true\"></iframe></div>

我如何更改它以使用 iram 或任何脚本? 谢谢

【问题讨论】:

    标签: wordpress woocommerce custom-fields


    【解决方案1】:

    您可以使用以下代码解决此问题,

    <?php $get_data_fromdb =  get_post_meta( YOUR_PRODUCT_ID_HERE, '_product_video_field' ); 
         echo htmlspecialchars_decode(stripslashes($get_data_fromdb[0])); 
    >
    

    htmlspecialchars_decode此函数会将代码视为html。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 2015-07-04
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 2016-04-24
      相关资源
      最近更新 更多