Home > Back-end >  Woocommerce Product Custom Field for iframe not work correctly
Woocommerce Product Custom Field for iframe not work correctly

Time:11-20

i create a custom field for woocommerce product by below code. i want use this custom field for store iframe code and display it in product single page. but stored data in database and output code hast wrong and different from entered code in xustom field text input

My Code :

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']) );
}

My entered code in input text 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>

My Custom field output & stored in db :

<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>

How i Change it to work with ifram or any Script ? Thanks

CodePudding user response:

You can solve this by using the following code,

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

htmlspecialchars_decode this function will treat the code as html.

  • Related