1条回答 默认 最新
- douyulv6921 2018-05-16 02:08关注
Here it is a complete example that will add a custom metabox (with a wysiwyg text editor field) in product edit pages. Then it will be displayed under product meta:
## ---- 1. Backend ---- ## // 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', __( 'Additional Product text <em>(optional)</em>', 'woocommerce' ), 'add_custom_product_content_meta_box', 'product', 'normal', 'default' ); } } // Custom metabox content in admin product pages if ( ! function_exists( 'add_custom_product_content_meta_box' ) ){ function add_custom_product_content_meta_box( $post ){ $text_area = get_post_meta($post->ID, '_custom_text', true) ? get_post_meta($post->ID, '_custom_text', true) : ''; $args['textarea_rows'] = 6; echo '<p>'.__( 'Custom text label', 'woocommerce' ).'</p>'; wp_editor( $text_area, 'custom_text', $args ); echo '<input type="hidden" name="custom_text_field_nonce" value="' . wp_create_nonce() . '">'; } } //Save the data of the Meta field add_action( 'save_post', 'save_custom_product_content_meta_box', 20, 3 ); if ( ! function_exists( 'save_custom_product_content_meta_box' ) ){ function save_custom_product_content_meta_box( $post_id, $post, $update ) { if ( $post->post_type != 'product') return; // Only products // Check if our nonce is set. if ( ! isset( $_POST[ 'custom_text_field_nonce' ] ) ) return $post_id; //Verify that the nonce is valid. if ( ! wp_verify_nonce( $_POST[ 'custom_text_field_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 ( ! current_user_can( 'edit_product', $post_id ) ) return $post_id; // Sanitize user input and update the meta field in the database. if ( isset( $_POST[ 'custom_text' ] ) ) update_post_meta( $post_id, $prefix.'_custom_text', wp_kses_post($_POST[ 'custom_text' ]) ); } } ## ---- 2. Frontend ---- ## // Add custom text under single product meta add_action( 'woocommerce_single_product_summary', 'add_custom_product_text', 70 ); function add_custom_product_text() { global $product; $custom_text = get_post_meta( $product->get_id(), '_custom_text', true ); if( empty($custom_text) ) return; echo '<div class="product-extra-text" style="margin-top:30px;">'; echo '<h3>' . __( 'Product extras', 'woocommerce' ) . '</h3>'; // Updated to apply the_content filter to WYSIWYG content echo apply_filters( 'the_content', $custom_text ); echo '</div>'; }
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Backend:
Frontend:
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报