wordpress 如何在Woocommerce自定义Meta框中允许html

nuypyhwy  于 2023-11-17  发布在  WordPress
关注(0)|答案(1)|浏览(217)

如何为woocommerce产品页面创建一个显示html的自定义字段Meta框。
我想把一个翻页书的iframe预览放进去。我试着把iframe放在Meta周围,只使用url,但这导致了其他问题。所以我只想用一种简单的方法把html插入到meta框中。
下面是我的代码:

  1. // Adding a custom Meta container to admin products pages
  2. add_action( 'add_meta_boxes', 'create_custom_meta_box' );
  3. if ( ! function_exists( 'create_custom_meta_box' ) )
  4. {
  5. function create_custom_meta_box()
  6. {
  7. add_meta_box(
  8. 'custom_product_meta_box',
  9. __( 'Custom Previews', 'cmb' ),
  10. 'add_custom_content_meta_box',
  11. 'product',
  12. 'normal',
  13. 'default'
  14. );
  15. }
  16. }
  17. // Custom metabox content in admin product pages
  18. if ( ! function_exists( 'add_custom_content_meta_box' ) ){
  19. function add_custom_content_meta_box( $post ){
  20. $prefix = '_bhww_'; // global $prefix;
  21. $preview = get_post_meta($post->ID, $prefix.'preview_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'preview_wysiwyg', true) : '';
  22. $args['textarea_rows'] = 6;
  23. echo '<p>'.__( 'preview', 'cmb' ).'</p>';
  24. wp_editor( $preview, 'preview_wysiwyg', $args );
  25. echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">';
  26. }
  27. }
  28. //Save the data of the Meta field
  29. add_action( 'save_post', 'save_custom_content_meta_box', 10, 1 );
  30. if ( ! function_exists( 'save_custom_content_meta_box' ) )
  31. {
  32. function save_custom_content_meta_box( $post_id ) {
  33. $prefix = '_bhww_'; // global $prefix;
  34. // We need to verify this with the proper authorization (security stuff).
  35. // Check if our nonce is set.
  36. if ( ! isset( $_POST[ 'custom_product_field_nonce' ] ) ) {
  37. return $post_id;
  38. }
  39. $nonce = $_REQUEST[ 'custom_product_field_nonce' ];
  40. //Verify that the nonce is valid.
  41. if ( ! wp_verify_nonce( $nonce ) ) {
  42. return $post_id;
  43. }
  44. // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  45. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  46. return $post_id;
  47. }
  48. // Check the user's permissions.
  49. if ( 'product' == $_POST[ 'post_type' ] ){
  50. if ( ! current_user_can( 'edit_product', $post_id ) )
  51. return $post_id;
  52. } else {
  53. if ( ! current_user_can( 'edit_post', $post_id ) )
  54. return $post_id;
  55. }
  56. // Sanitize user input and update the meta field in the database.
  57. update_post_meta( $post_id, $prefix.'preview_wysiwyg', wp_kses_post($_POST[ 'preview_wysiwyg' ]) );
  58. }
  59. }
  60. // Create custom tabs in product single pages
  61. add_filter( 'woocommerce_after_single_product_summary', 'preview_product_content' );
  62. // Add content to custom tab in product single pages (2)
  63. function preview_product_content() {
  64. global $post;
  65. $product_preview = get_post_meta( $post->ID, '_bhww_preview_wysiwyg', true );
  66. if ( ! empty( $product_preview ) ) {
  67. echo '<h2>' . __( 'Product Preview', 'woocommerce' ) . '</h2>';
  68. // Updated to apply the_content filter to WYSIWYG content
  69. echo apply_filters( 'the_content', $product_preview);
  70. }
  71. }

字符串

vnzz0bqm

vnzz0bqm1#

为了让它接受html,我不得不更新sanitize以允许html

  1. update_post_meta( $post_id, $prefix.'preview_wysiwyg', wp_kses_post($_POST[ 'preview_wysiwyg' ]) );

字符串
改为

  1. update_post_meta( $post_id, $prefix.'bpreview_wysiwyg', wp_kses_post($_POST[ 'bpreview_wysiwyg' ],$allowed) );


我添加了变量$allowed = wp_kses_allowed_html();
然而,这仍然不允许iframe
当我试图将iframe代码放在输出周围时,我得到了一个404服务器端访问不允许。

相关问题