Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce单个产品自定义选项卡上显示所见即所得字段的元框_Php_Wordpress_Woocommerce_Product_Wysiwyg - Fatal编程技术网

Php 在Woocommerce单个产品自定义选项卡上显示所见即所得字段的元框

Php 在Woocommerce单个产品自定义选项卡上显示所见即所得字段的元框,php,wordpress,woocommerce,product,wysiwyg,Php,Wordpress,Woocommerce,Product,Wysiwyg,需要在单个产品页面上的相关产品中显示自定义字段 我想在添加新产品字段中添加一个元框,并在“评论”下的单个产品页面的“自定义”选项卡上显示结果。我尝试使用代码,但页面上没有显示任何结果。“添加额外产品”选项卡可帮助我添加额外信息。所以,添加,保存和显示产品是我所寻找的 add_filter( 'add_meta_boxes', 'bhww_core_cpt_metaboxes' ); function bhww_core_cpt_metaboxes( $meta_boxes ) { /

需要在单个产品页面上的相关产品中显示自定义字段

我想在
添加新产品
字段中添加一个元框,并在“评论”下的单个产品页面的“自定义”选项卡上显示结果。我尝试使用代码,但页面上没有显示任何结果。“添加额外产品”选项卡可帮助我添加额外信息。所以,添加,保存和显示产品是我所寻找的

add_filter( 'add_meta_boxes', 'bhww_core_cpt_metaboxes' );

function bhww_core_cpt_metaboxes( $meta_boxes ) {

    //global $prefix;
    $prefix = '_bhww_'; // Prefix for all fields

    // Add metaboxes to the 'Product' CPT
    $meta_boxes[] = array(
        'id'         => 'bhww_woo_tabs_metabox',
        'title'      => 'Additional Product Information - <strong>Optional</strong>',
        'pages'      => array( 'product' ), // Which post type to associate with?
        'context'    => 'normal',
        'priority'   => 'default',
        'show_names' => true,                   
        'fields'     => array(
            array(
                'name'    => __( 'Ingredients', 'cmb' ),
                'desc'    => __( 'Anything you enter here will be displayed on the Ingredients tab.', 'cmb' ),
                'id'      => $prefix . 'ingredients_wysiwyg',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),
            ),
            array(
                'name'    => __( 'Benefits', 'cmb' ),
                'desc'    => __( 'Anything you enter here will be displayed on the Benefits tab.', 'cmb' ),
                'id'      => $prefix . 'benefits_wysiwyg',
                'type'    => 'wysiwyg',
                'options' => array( 'textarea_rows' => 5, ),
            ),
        ),
    );

    return $meta_boxes;

}

/////////////////////////////////////////

add_filter( 'woocommerce_product_data_tabs', 'bhww_woo_extra_tabs' );

function bhww_woo_extra_tabs( $tabs1 ) {

    global $post;
    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
    $product_benefits    = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_ingredients ) ) {

        $tabs1['ingredients_tab'] = array(
            'title'    => __( 'Ingredients', 'woocommerce' ),
            'priority' => 15,
            'callback' => 'bhww_woo_ingredients_tab_content'
        );

    }

    if ( ! empty( $product_benefits ) ) {

        $tabs1['benefits_tab'] = array(
            'title'    => __( 'Benefits', 'woocommerce' ),
            'priority' => 16,
            'callback' => 'bhww_woo_benefits_tab_content'
        );

    }

    return $tabs1;

}

function bhww_woo_ingredients_tab_content() {

    global $post;
    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );

    if ( ! empty( $product_ingredients ) ) {

        echo '<h2>' . esc_html__( 'Product Ingredients', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_ingredients );

    }

}

function bhww_woo_benefits_tab_content() {

    global $post;
    $product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_benefits ) ) {

        echo '<h2>' . esc_html__( 'Product Benefits', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_benefits );

    }

}
add_filter('add_meta_box','bhww_core_cpt_metaboxes');
功能bhww_核心cpt_元盒($meta_盒){
//全球$前缀;
$prefix='\u bhww';//所有字段的前缀
//将元盒添加到“产品”CPT
$meta_box[]=数组(
'id'=>'bhww\u woo\u tabs\u metabox',
“标题”=>“附加产品信息-可选”,
'pages'=>array('product'),//要与哪个帖子类型关联?
'上下文'=>'正常',
“优先级”=>“默认值”,
“显示名称”=>true,
“字段”=>数组(
排列(
“名称”=>u uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“desc'=>”(此处输入的任何内容都将显示在“配料”选项卡上。“”cmb“),
'id'=>$prefix.'配料所见即所得',
'类型'=>'所见即所得',
'options'=>array('textarea\u rows'=>5,),
),
排列(
“名称”=>u uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“desc'=>\(“您在此处输入的任何内容都将显示在“福利”选项卡上。”,“cmb”),
'id'=>$prefix.'benefits\u wysiwyg',
'类型'=>'所见即所得',
'options'=>array('textarea\u rows'=>5,),
),
),
);
退回$meta_盒;
}
/////////////////////////////////////////
添加过滤器(“woocommerce\u product\u data\u tabs”、“bhww\u woo\u extra\u tabs”);
功能bhww_-woo_额外选项卡($tabs1){
全球$员额;
$product\U METANCTIONS=get\U post\U meta($post->ID,“\u bhww\u METANCTIONS\u wysiwyg”,true);
$product\u benefits=get\u post\u meta($post->ID,''bhww\u benefits\u wysiwyg',true);
如果(!空($product_配料)){
$tabs1['Components_tab']=数组(
“title”=>uuuuuu(‘配料’、‘woocommerce’),
“优先级”=>15,
“回调”=>“bhww\u woo\u成分\u选项卡\u内容”
);
}
如果(!空($product_PROFECTIONS)){
$tabs1['benefits_tab']=数组(
“title”=>uuuuuuuu(‘福利’、‘商业’),
“优先级”=>16,
“回调”=>“bhww\u woo\u福利\u选项卡\u内容”
);
}
返回$tabs1;
}
功能bhww_woo_成分_选项卡_内容(){
全球$员额;
$product\U METANCTIONS=get\U post\U meta($post->ID,“\u bhww\u METANCTIONS\u wysiwyg”,true);
如果(!空($product_配料)){
echo'.esc_html_uuuuuu('productcomponents','woocommerce');
//更新以将_内容过滤器应用于所见即所得内容
echo apply_过滤器(‘_内容’,$product_成分);
}
}
功能bhww_woo_福利_选项卡_内容(){
全球$员额;
$product\u benefits=get\u post\u meta($post->ID,''bhww\u benefits\u wysiwyg',true);
如果(!空($product_PROFECTIONS)){
echo'.esc_html_uuuuu('Product Benefits','woocommerce');
//更新以将_内容过滤器应用于所见即所得内容
echo apply_过滤器(‘内容’,$product_优点);
}
}

以下是在管理编辑产品页面中添加两个自定义字段(编辑器wysiwyg)并在前端单个产品页面自定义选项卡中显示值的方法:

## ---- 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 Information <em>(optional)</em>', '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;

        $ingredients = get_post_meta($post->ID, $prefix.'ingredients_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'ingredients_wysiwyg', true) : '';
        $benefits = get_post_meta($post->ID, $prefix.'benefits_wysiwyg', true) ? get_post_meta($post->ID, $prefix.'benefits_wysiwyg', true) : '';
        $args['textarea_rows'] = 6;

        echo '<p>'.__( 'Ingredients', 'cmb' ).'</p>';
        wp_editor( $ingredients, 'ingredients_wysiwyg', $args );

        echo '<p>'.__( 'Benefits', 'cmb' ).'</p>';
        wp_editor( $benefits, 'benefits_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.'ingredients_wysiwyg', wp_kses_post($_POST[ 'ingredients_wysiwyg' ]) );
        update_post_meta( $post_id, $prefix.'benefits_wysiwyg', wp_kses_post($_POST[ 'benefits_wysiwyg' ]) );
    }
}

## ---- 2. Front-end ---- ##

// Create custom tabs in product single pages
add_filter( 'woocommerce_product_tabs', 'custom_product_tabs' );
function custom_product_tabs( $tabs ) {
    global $post;

    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );
    $product_benefits    = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_ingredients ) )
        $tabs['ingredients_tab'] = array(
            'title'    => __( 'Ingredients', 'woocommerce' ),
            'priority' => 45,
            'callback' => 'ingredients_product_tab_content'
        );

    if ( ! empty( $product_benefits ) )
        $tabs['benefits_tab'] = array(
            'title'    => __( 'Benefits', 'woocommerce' ),
            'priority' => 50,
            'callback' => 'benefits_product_tab_content'
        );

    return $tabs;
}

// Add content to custom tab in product single pages (1)
function ingredients_product_tab_content() {
    global $post;

    $product_ingredients = get_post_meta( $post->ID, '_bhww_ingredients_wysiwyg', true );

    if ( ! empty( $product_ingredients ) ) {
        echo '<h2>' . __( 'Product Ingredients', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_ingredients );
    }
}

// Add content to custom tab in product single pages (2)
function benefits_product_tab_content() {
    global $post;

    $product_benefits = get_post_meta( $post->ID, '_bhww_benefits_wysiwyg', true );

    if ( ! empty( $product_benefits ) ) {
        echo '<h2>' . __( 'Product Benefits', 'woocommerce' ) . '</h2>';

        // Updated to apply the_content filter to WYSIWYG content
        echo apply_filters( 'the_content', $product_benefits );
    }
}
##--1。后端--##
//向管理产品页面添加自定义元容器
添加动作(“添加元框”、“创建自定义元框”);
如果(!function_存在('create_custom_meta_box'))
{
函数create_custom_meta_box()
{
添加元框(
“定制产品元盒”,
__(“附加产品信息(可选)”,“cmb”),
“添加自定义内容元框”,
“产品”,
“正常”,
“默认”
);
}
}
//管理产品页面中的自定义元数据库内容
如果(!function_存在('add_custom_content_meta_box')){
函数添加自定义内容元框($post){
$prefix='\u bhww';//全局$prefix;
$Components=get_post_meta($post->ID,$prefix.'Components_wysiwyg',true)?get_post_meta($post->ID,$prefix.'Components_wysiwyg',true):“”;
$benefits=get_post_meta($post->ID,$prefix.benefits_wysiwyg',true)?get_post_meta($post->ID,$prefix.benefits_wysiwyg',true):“”;
$args['textarea_rows']=6;
回声“”。u uu('components','cmb')。

”; wp_编辑器($Components,'Components_wysiwyg',$args); 回音“”。_u('Benefits','cmb')。

”; wp_编辑器($benefits,'benefits_wysiwyg',$args); 回声'; } } //保存元字段的数据 添加操作(“保存帖子”、“保存自定义内容”元框“,10,1); 如果(!function_存在('save_custom_content_meta_box')) { 函数保存自定义内容元框($post\u id){ $prefix='\u bhww';//全局$prefix; //我们需要通过适当的授权(安全方面的东西)来验证这一点。 //检查当前值是否已设置。 如果(!isset($\u POST['自定义产品\字段\当前'])){ 返回$post_id; } $nonce=$\u请求['自定义产品\u字段\u nonce']; //验证nonce是否有效。 如果(!wp\u verify\u nonce($nonce)){ 返回$post_id; } //如果这是自动保存,则我们的表单尚未提交,因此我们不想执行任何操作。 if(已定义('DOING_AUTOSAVE')&&DOING_AUTOSAVE){ 返回$post_id; } //检查用户的权限。 如果('pro