如何为woocommerce产品页面创建一个显示html的自定义字段Meta框。
我想把一个翻页书的iframe预览放进去。我试着把iframe放在Meta周围,只使用url,但这导致了其他问题。所以我只想用一种简单的方法把html插入到meta框中。
下面是我的代码:
// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_meta_box',
__( 'Custom Previews', 'cmb' ),
'add_custom_content_meta_box',
'product',
'normal',
'default'
);
}
}
// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_content_meta_box' ) ){
function add_custom_content_meta_box( $post ){
$prefix = '_bhww_'; // global $prefix;
$preview = get_post_meta($post->ID, $prefix.'preview_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'preview_wysiwyg', true) : '';
$args['textarea_rows'] = 6;
echo '<p>'.__( 'preview', 'cmb' ).'</p>';
wp_editor( $preview, 'preview_wysiwyg', $args );
echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'save_custom_content_meta_box', 10, 1 );
if ( ! function_exists( 'save_custom_content_meta_box' ) )
{
function save_custom_content_meta_box( $post_id ) {
$prefix = '_bhww_'; // global $prefix;
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_product_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'custom_product_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'product' == $_POST[ 'post_type' ] ){
if ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
// Sanitize user input and update the meta field in the database.
update_post_meta( $post_id, $prefix.'preview_wysiwyg', wp_kses_post($_POST[ 'preview_wysiwyg' ]) );
}
}
// Create custom tabs in product single pages
add_filter( 'woocommerce_after_single_product_summary', 'preview_product_content' );
// Add content to custom tab in product single pages (2)
function preview_product_content() {
global $post;
$product_preview = get_post_meta( $post->ID, '_bhww_preview_wysiwyg', true );
if ( ! empty( $product_preview ) ) {
echo '<h2>' . __( 'Product Preview', 'woocommerce' ) . '</h2>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $product_preview);
}
}
字符串
1条答案
按热度按时间vnzz0bqm1#
为了让它接受html,我不得不更新sanitize以允许html
字符串
改为
型
我添加了变量$allowed = wp_kses_allowed_html();
然而,这仍然不允许iframe
当我试图将iframe代码放在输出周围时,我得到了一个404服务器端访问不允许。