_x( ‘Products’, ‘Post type general name’, ‘royal-knot’ ),
‘singular_name’ => _x( ‘Product’, ‘Post type singular name’, ‘royal-knot’ ),
‘menu_name’ => _x( ‘Products’, ‘Admin Menu text’, ‘royal-knot’ ),
‘plugin_name’ => _x( ‘Product’, ‘Added to plugin menu’, ‘royal-knot’ ),
‘add_new’ => __( ‘Add New’, ‘royal-knot’ ),
‘add_new_item’ => __( ‘Add New Product’, ‘royal-knot’ ),
‘edit_item’ => __( ‘Edit Product’, ‘royal-knot’ ),
‘new_item’ => __( ‘New Product’, ‘royal-knot’ ),
‘view_item’ => __( ‘View Product’, ‘royal-knot’ ),
‘search_items’ => __( ‘Search Products’, ‘royal-knot’ ),
‘not_found’ => __( ‘No Products found.’, ‘royal-knot’ ),
‘not_found_in_trash’ => __( ‘No Products found in Trash.’, ‘royal-knot’ ),
);
$args = array(
‘labels’ => $labels,
‘public’ => true,
‘publicly_queryable’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘product’ ),
‘capability_type’ => ‘post’,
‘has_archive’ => true,
‘hierarchical’ => false,
‘menu_position’ => null,
‘menu_icon’ => ‘dashicons-cart’,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’ ),
);
register_post_type( ‘rk_product’, $args );
}
add_action( ‘init’, ‘rk_register_product_cpt’ );
// 2. Register Meta Box for Custom Fields
function rk_register_product_meta_box() {
add_meta_box(
‘rk_product_details’, // ID
‘Product Details & Add-ons’, // Title
‘rk_render_product_meta_box’, // Callback
‘rk_product’, // Post Type
‘normal’, // Context
‘default’ // Priority
);
}
add_action( ‘add_meta_boxes’, ‘rk_register_product_meta_box’ );
// 3. Render the Meta Box
function rk_render_product_meta_box( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( ‘rk_save_product_meta_box_data’, ‘rk_product_meta_box_nonce’ );
$base_price = get_post_meta( $post->ID, ‘_rk_base_price’, true );
$addon_rsvp_price = get_post_meta( $post->ID, ‘_rk_addon_rsvp_price’, true );
$addon_wax_price = get_post_meta( $post->ID, ‘_rk_addon_wax_price’, true );
$addon_ribbon_price = get_post_meta( $post->ID, ‘_rk_addon_ribbon_price’, true );
// Default values if empty
$base_price = $base_price ? $base_price : ‘0.00’;
$addon_rsvp_price = $addon_rsvp_price ? $addon_rsvp_price : ‘0.00’;
$addon_wax_price = $addon_wax_price ? $addon_wax_price : ‘0.00’;
$addon_ribbon_price = $addon_ribbon_price ? $addon_ribbon_price : ‘0.00’;
echo ‘
‘;
echo ‘
‘;
echo ‘
Add-on Pricing (Set to 0 if not applicable)
‘;
echo ‘
‘;
echo ‘
‘;
echo ‘
‘;
echo ‘
‘;
echo ‘
‘;
echo ‘
‘;
}
// 4. Save the Meta Box data
function rk_save_product_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST[‘rk_product_meta_box_nonce’] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[‘rk_product_meta_box_nonce’], ‘rk_save_product_meta_box_data’ ) ) {
return;
}
// 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;
}
// Check the user’s permissions.
if ( isset( $_POST[‘post_type’] ) && ‘page’ == $_POST[‘post_type’] ) {
if ( ! current_user_can( ‘edit_page’, $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( ‘edit_post’, $post_id ) ) {
return;
}
}
// Sanitize user input.
$base_price = isset( $_POST[‘rk_base_price’] ) ? sanitize_text_field( $_POST[‘rk_base_price’] ) : ”;
$addon_rsvp_price = isset( $_POST[‘rk_addon_rsvp_price’] ) ? sanitize_text_field( $_POST[‘rk_addon_rsvp_price’] ) : ”;
$addon_wax_price = isset( $_POST[‘rk_addon_wax_price’] ) ? sanitize_text_field( $_POST[‘rk_addon_wax_price’] ) : ”;
$addon_ribbon_price = isset( $_POST[‘rk_addon_ribbon_price’] ) ? sanitize_text_field( $_POST[‘rk_addon_ribbon_price’] ) : ”;
// Update the meta field in the database.
update_post_meta( $post_id, ‘_rk_base_price’, $base_price );
update_post_meta( $post_id, ‘_rk_addon_rsvp_price’, $addon_rsvp_price );
update_post_meta( $post_id, ‘_rk_addon_wax_price’, $addon_wax_price );
update_post_meta( $post_id, ‘_rk_addon_ribbon_price’, $addon_ribbon_price );
}
add_action( ‘save_post’, ‘rk_save_product_meta_box_data’ );