0

I've been fighting with that for three days and I can't figure out, why my code doesn't work?

<?php
    function my_custom_post_product() {
    $labels = array(
        'name'               => _x( 'Produkty', 'post type general name' ),
        'singular_name'      => _x( 'Produkt', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'book' ),
        'add_new_item'       => __( 'Add New Product' ),
        'edit_item'          => __( 'Edit Product' ),
        'new_item'           => __( 'New Product' ),
        'all_items'          => __( 'All Products' ),
        'view_item'          => __( 'View Product' ),
        'search_items'       => __( 'Search Products' ),
        'not_found'          => __( 'No products found' ),
        'not_found_in_trash' => __( 'No products found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Products'
    );
    $args = array(
        'labels'        => $labels,
        'description'   => 'Holds our products and product specific data',
        'public'        => true,
        'publicly_queryable' => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
    );
    register_post_type( 'product', $args ); 
    flush_rewrite_rules();
}
add_action( 'init', 'my_custom_post_product' );
function my_taxonomies_product() {
    $labels = array(
        'name'              => _x( 'Product Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Product Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Product Categories' ),
        'all_items'         => __( 'All Product Categories' ),
        'parent_item'       => __( 'Parent Product Category' ),
        'parent_item_colon' => __( 'Parent Product Category:' ),
        'edit_item'         => __( 'Edit Product Category' ), 
        'update_item'       => __( 'Update Product Category' ),
        'add_new_item'      => __( 'Add New Product Category' ),
        'new_item_name'     => __( 'New Product Category' ),
        'menu_name'         => __( 'Product Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
    );
    register_taxonomy( 'product_category', 'product', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );
add_action( 'add_meta_boxes', 'product_price_box' );
function product_price_box() {
    add_meta_box( 
        'product_price_box',
        __( 'Product Price'),
        'product_price_box_content_callback',
        'product',
        'normal',
        'high'
    );
}

function product_price_box_content_callback( $post ) {
    wp_nonce_field( 'product_price_box_save' , 'product_price_box_content_nonce' );
    $value = get_post_meta($post->ID,  _product_price_value_key, true);
    echo '<label for="product_price_field"></label>';
    echo '<input type="text" id="product_price_field" name="product_price_field" 
    placeholder="enter a price" value="'. esc_attr( $value ) .'" />';
}

function product_price_box_save ( $post_id ) {
    if ( !isset( $_POST['product_price_box_content_nonce'])) {
        return;
    }
    if ( !wp_verify_nonce($_POST['product_price_box_content_nonce'], 'product_price_box_save')) {
        return;
    }
    if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        return;
    }
    if ( !current_user_can('edit_post', $post_id) ) {
        return;
    }
    if ( !isset($_POST['product_price_field']) ) {
        return;
    }
    $save_data = sanitize_text_field( $_POST['product_price_field'] );
    update_post_meta($post_id, '_product_price_value_key', $save_data);
}
add_action('save_post', 'product_price_box_save');
0

My code works perfectly. The second parameter of the value is a string $value = get_post_meta($post->ID, '_product_price_value_key', true);

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.