3

I have a custom post type 'service'. Each service will have items. For each item there would be 'name of item' and 'price'. Earlier i created meta boxes for 5 items i.e 10 meta boxes in total, which i think may be the wrong approach.

GOAL is to give the user the functionality that they can add as many items they want and not restricted to just 5 items. For eg: i am builting this for a saloon. And there is a service Face care . And under face care there are lot sub-services(items) like normal cleanup ,d tan,facial etc...... So it needs to be decided at real time that how many metaboxes needs to be created..

There are 40-50 services or more just under Face care service.There are other services like hair style, hair coloring etc. I have taxonomy terms as male,female and unisex. You are write textarea is not an good option but i dont understand why check box..

Now there are some more items. How can do this task by giving the user option in backend of adding new item that will generate meta boxes for 'name' & 'price'

Please suggest me something and following is the code i have been using.

  <?php

define( 'ROOT', plugins_url( '', __FILE__ ) );
define( 'STYLES', ROOT . '/css/' );
define( 'SCRIPTS', ROOT . '/js/' );
// Registers the new post type and taxonomy

function wpt_services_posttype() {
    register_post_type( 'services',
        array(
            'labels' => array(
                'name' => __( 'Services' ),
                'singular_name' => __( 'services' ),
                'add_new' => __( 'Add New services' ),
                'add_new_item' => __( 'Add New services' ),
                'edit_item' => __( 'Edit service' ),
                'new_item' => __( 'Add New service' ),
                'view_item' => __( 'View service' ),
                'search_items' => __( 'Search service' ),
                'not_found' => __( 'No services found' ),
                'not_found_in_trash' => __( 'No services found in trash' )
            ),
            'public' => true,
            'supports' => array( 'title','editor','thumbnail', 'excerpts','page-attributes' ),
            'capability_type' => 'post',
            'rewrite' => array("slug" => "services"), // Permalinks format
            'menu_position' => 6,             
            'register_meta_box_cb' => 'add_services_metaboxes',
            'show_ui'=>true,
            'query_var'=>true

            )
    );
}
add_action( 'init', 'wpt_services_posttype' );

/*Add custom taxonomy to the services post-type */



/*Now we add the meta boxes to the services*/
add_action('add_meta_boxes', 'add_services_metaboxes');
function add_services_metaboxes() {

    add_meta_box('wpt_services_men', __('Charges for Men'), 'wpt_services_men', 'services', 'normal', 'high');  

}

function wpt_services_men(){
    global $post;
    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="servicesmeta_noncename" id="servicesmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
    $serviceM1 = get_post_meta($post->ID, '_serviceM1', true);
    $serviceM2 = get_post_meta($post->ID, '_serviceM2', true);
    $serviceM3 = get_post_meta($post->ID, '_serviceM3', true);
    $serviceM4 = get_post_meta($post->ID, '_serviceM4', true);
    $serviceM5 = get_post_meta($post->ID, '_serviceM5', true);
    $priceM1 = get_post_meta($post->ID, '_priceM1', true);
    $priceM2 = get_post_meta($post->ID, '_priceM2', true);
    $priceM3 = get_post_meta($post->ID, '_priceM3', true);
    $priceM4 = get_post_meta($post->ID, '_priceM4', true);
    $priceM5 = get_post_meta($post->ID, '_priceM5', true);?>

    <!-- Echo out the field -->
    <?php echo '<label >';?><?php _e( 'Service1:' );?></label>
    <?php echo '<textarea name=_serviceM1 rows="2" cols="45">'.$serviceM1.'</textarea>';?>
    <?php echo '<label >';?><?php _e( 'Price:' );?></label>
    <?php echo '<textarea name=_priceM1 rows="2" cols="8">'.$priceM1.'</textarea>';?>
    <?php echo '<br><label >';?><?php _e( 'Service2:' );?></label>
    <?php echo '<textarea name=_serviceM2 rows="2" cols="45">'.$serviceM2.'</textarea>';?>
    <?php echo '<label >';?><?php _e( 'Price:' );?></label>
    <?php echo '<textarea name=_priceM2 rows="2" cols="8">'.$priceM2.'</textarea>';?>
    <?php echo '<br><label >';?><?php _e( 'Service3:' );?></label>
    <?php echo '<textarea name=_serviceM3 rows="2" cols="45">'.$serviceM3.'</textarea>';?>
    <?php echo '<label >';?><?php _e( 'Price:' );?></label>
    <?php echo '<textarea name=_priceM3 rows="2" cols="8">'.$priceM3.'</textarea>';?>
    <?php echo '<br><label >';?><?php _e( 'Service4:' );?></label>
    <?php echo '<textarea name=_serviceM4 rows="2" cols="45">'.$serviceM4.'</textarea>';?>
    <?php echo '<label >';?><?php _e( 'Price:' );?></label>
    <?php echo '<textarea name=_priceM4 rows="2" cols="8">'.$priceM4.'</textarea>';?>
    <?php echo '<br><label >';?><?php _e( 'Service5:' );?></label>
    <?php echo '<textarea name=_serviceM5 rows="2" cols="45">'.$serviceM5.'</textarea>';?>
    <?php echo '<label >';?><?php _e( 'Price:' );?></label>
    <?php echo '<textarea name=_priceM5 rows="2" cols="8">'.$priceM5.'</textarea>';

}


function wpt_save_services_meta($post_id, $post) {

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['servicesmeta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

    $services_meta['_serviceM1'] = $_POST['_serviceM1'];
    $services_meta['_serviceM2'] = $_POST['_serviceM2'];
    $services_meta['_serviceM3'] = $_POST['_serviceM3'];
    $services_meta['_serviceM4'] = $_POST['_serviceM4'];
    $services_meta['_serviceM5'] = $_POST['_serviceM5'];
    $services_meta['_priceM1'] = $_POST['_priceM1'];
    $services_meta['_priceM2'] = $_POST['_priceM2'];
    $services_meta['_priceM3'] = $_POST['_priceM3'];
    $services_meta['_priceM4'] = $_POST['_priceM4'];
    $services_meta['_priceM5'] = $_POST['_priceM5'];


    // Add values of $services_meta as custom fields

    foreach ($services_meta as $key => $value) { // Cycle through the $services_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
}

add_action('save_post', 'wpt_save_services_meta', 1, 2); // save the custom fields





?>
6
  • To me your code looks ok and should work (not tested). I'm also very sure that you don't need the add_action('add_meta_boxes', 'add_services_metaboxes'); call as you already registered the callback with register_post_type(). What I don't understand is "[...] How can do this task by giving the user option in backend of adding new item that will generate meta boxes for 'name' & 'price' [...]". Shall your users create the code? Please file an edit and explain the goal you want to achieve a bit more. Thanks.
    – kaiser
    Commented Jan 10, 2015 at 12:17
  • @kaiser i have edited above please review what i want to do. thanks
    – terminator
    Commented Jan 10, 2015 at 12:42
  • I guess I now understand what you want to do. But why did you use <textarea>s for that? Why not checkboxes? Or just use a custom taxonomy and terms like "tan", "facial", etc? Would imo haven been much easier (less code) and give you archives - which is built into the template hierarchy (taxonomy archives) - etc. Taxonomy terms can also have a description, which you then can output in your themes templates.
    – kaiser
    Commented Jan 10, 2015 at 12:48
  • @kaiser actually there are 40-50 services or more just under Face care service.There are other services like hair style, hair coloring etc. I have taxonomy terms as male,female and unisex. You are write textarea is not an good option but i dont understand why check box...
    – terminator
    Commented Jan 10, 2015 at 13:09
  • Answers (always) as update/edit to your question, please. Not everyone reads comments. About the question: You can add multiple (custom) taxonomies to a post type. And when you have a large, not really limited list of services, I'd say it's easier to use a CT because you get an easy to use UI as well. Also: Why do you need an textarea? A checkbox would simply say "service? yes/no". A taxonomy would make that even easier.
    – kaiser
    Commented Jan 10, 2015 at 13:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.