i created a plugin in WordPress , i created custom post type called "invoices" here it's the code
add_action('init', 'invoices_register');
function invoices_register() {
$labels = array(
'name' => _x('Invoices', 'post type general name'),
'singular_name' => _x('Invoice', 'post type singular name'),
'add_new' => _x('Add New Invoice ', 'Device'),
'add_new_item' => __('Add New Invoice'),
'edit_item' => __('Edit Invoice'),
'new_item' => __('New Invoice'),
'view_item' => __('View Invoices'),
'search_items' => __('Search Invoices'),
'not_found' => __('No Invoices'),
'not_found_in_trash' => __('No Invoices Here'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor')
);
register_post_type( 'invoices' , $args );
}
Then i created repeated meta box here it's the code
<?php
add_action( 'add_meta_boxes', 'mb_add_custom_box' );
/* Do something with the data entered */
add_action( 'save_post', 'mb_save_postdata' );
/* Adds a box to the main column on the Post and Page edit screens */
function mb_add_custom_box() {
add_meta_box(
'invocie_details',
__( 'Invocie Details', 'myplugin_textdomain' ),
'mb_inner_custom_box',
'invoices');
}
/* Prints the box content */
function mb_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'mbMeta_noncename' );
?>
<div id="meta_inner">
<?php
//get the saved meta as an array
$invocies = get_post_meta($post->ID,'invocies',false);
// print_r($invocies);
$c = 0;
if ( count( $invocies ) > 0 ) {
if(!empty($invocies)){
foreach( $invocies as $track_val ) {
foreach( $track_val as $track ) {
//var_dump($track);
if ( isset( $track['service'] ) || isset( $track['price'] ) || isset( $track['status']) || isset( $track['client']) ) {
printf( '<p>invocie service <input type="text" name="invocies[%1$s][service]" value="%2$s" /> invocie price : <input type="text" name="invocies[%1$s][price]" value="%3$s" /><br> invocie staus : <input type="text" name="invocies[%1$s][status]" value="%4$s" /> invocie client : <input type="text" name="invocies[%1$s][client]" value="%5$s" /><span class="remove">%6$s</span></p>', $c, $track['service'], $track['price'],$track['status'],$track['client'], __( 'Remove Invocie' ) );
$c = $c +1;
}
}
}
}
}
?>
<span id="here"></span>
<span class="add"><?php _e('Add Invoice'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".add").click(function() {
count = count + 1;
$('#here').append('<p>invocie service <input type="text" name="invocies['+count+'][service]" value="" /> invocie price : <input type="text" name="invocies['+count+'][price]" value="" /><br> invocie staus : <input type="text" name="invocies['+count+'][status]" value="" /> invocie client : <input type="text" name="invocies['+count+'][client]" value="" /><span class="remove">Remove Invocie</span></p>' );
return false;
});
$(".remove").live('click', function() {
$(this).parent().remove();
});
});
</script>
</div><?php
}
/* When the post is saved, saves our custom data */
function mb_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset( $_POST['mbMeta_noncename'] ) )
return;
if ( !wp_verify_nonce( $_POST['mbMeta_noncename'], plugin_basename( __FILE__ ) ) )
return;
// OK, we're authenticated: we need to find and save the data
$invocies = $_POST['invocies'];
update_post_meta($post_id,'invocies',$invocies);
}
then i created custom page template to insert new invoice from front end but the problem is when creating a new post the save not working with meta box here the code
if(isset($_POST['submit']) == '1') {
$post_title = $_POST['tile'];
//$post_category = $_POST['cat'];
//$post_content = $_POST['post_content'];
$new_post = array(
// 'ID' => '',
'post_type' => 'invoices',
//'post_author' => $user->ID,
//'post_category' => array($post_category),
// 'post_content' => $post_content,
// 'post_title' => $post_title,
'post_status' => 'publish',
'meta_input' => array(
'invociesp[service]' => $_POST['service'],
'invocies[price]' => $_POST['price'],
'invocies[status]' => $_POST['status'],
'invocies[client]' => $_POST['client'],
// '_flatsome_case_type' => $_POST['type'],
//'city' => $post['city']
)
);
$post_id = wp_insert_post($new_post,true);
if($post_id ==true){
echo '<script language="javascript">alert("You Create a New case successfully")</script>';
}
i created a plugin in WordPress , i created custom post type called "invoices" here it's the code
add_action('init', 'invoices_register');
function invoices_register() {
$labels = array(
'name' => _x('Invoices', 'post type general name'),
'singular_name' => _x('Invoice', 'post type singular name'),
'add_new' => _x('Add New Invoice ', 'Device'),
'add_new_item' => __('Add New Invoice'),
'edit_item' => __('Edit Invoice'),
'new_item' => __('New Invoice'),
'view_item' => __('View Invoices'),
'search_items' => __('Search Invoices'),
'not_found' => __('No Invoices'),
'not_found_in_trash' => __('No Invoices Here'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor')
);
register_post_type( 'invoices' , $args );
}
Then i created repeated meta box here it's the code
<?php
add_action( 'add_meta_boxes', 'mb_add_custom_box' );
/* Do something with the data entered */
add_action( 'save_post', 'mb_save_postdata' );
/* Adds a box to the main column on the Post and Page edit screens */
function mb_add_custom_box() {
add_meta_box(
'invocie_details',
__( 'Invocie Details', 'myplugin_textdomain' ),
'mb_inner_custom_box',
'invoices');
}
/* Prints the box content */
function mb_inner_custom_box() {
global $post;
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'mbMeta_noncename' );
?>
<div id="meta_inner">
<?php
//get the saved meta as an array
$invocies = get_post_meta($post->ID,'invocies',false);
// print_r($invocies);
$c = 0;
if ( count( $invocies ) > 0 ) {
if(!empty($invocies)){
foreach( $invocies as $track_val ) {
foreach( $track_val as $track ) {
//var_dump($track);
if ( isset( $track['service'] ) || isset( $track['price'] ) || isset( $track['status']) || isset( $track['client']) ) {
printf( '<p>invocie service <input type="text" name="invocies[%1$s][service]" value="%2$s" /> invocie price : <input type="text" name="invocies[%1$s][price]" value="%3$s" /><br> invocie staus : <input type="text" name="invocies[%1$s][status]" value="%4$s" /> invocie client : <input type="text" name="invocies[%1$s][client]" value="%5$s" /><span class="remove">%6$s</span></p>', $c, $track['service'], $track['price'],$track['status'],$track['client'], __( 'Remove Invocie' ) );
$c = $c +1;
}
}
}
}
}
?>
<span id="here"></span>
<span class="add"><?php _e('Add Invoice'); ?></span>
<script>
var $ =jQuery.noConflict();
$(document).ready(function() {
var count = <?php echo $c; ?>;
$(".add").click(function() {
count = count + 1;
$('#here').append('<p>invocie service <input type="text" name="invocies['+count+'][service]" value="" /> invocie price : <input type="text" name="invocies['+count+'][price]" value="" /><br> invocie staus : <input type="text" name="invocies['+count+'][status]" value="" /> invocie client : <input type="text" name="invocies['+count+'][client]" value="" /><span class="remove">Remove Invocie</span></p>' );
return false;
});
$(".remove").live('click', function() {
$(this).parent().remove();
});
});
</script>
</div><?php
}
/* When the post is saved, saves our custom data */
function mb_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !isset( $_POST['mbMeta_noncename'] ) )
return;
if ( !wp_verify_nonce( $_POST['mbMeta_noncename'], plugin_basename( __FILE__ ) ) )
return;
// OK, we're authenticated: we need to find and save the data
$invocies = $_POST['invocies'];
update_post_meta($post_id,'invocies',$invocies);
}
then i created custom page template to insert new invoice from front end but the problem is when creating a new post the save not working with meta box here the code
if(isset($_POST['submit']) == '1') {
$post_title = $_POST['tile'];
//$post_category = $_POST['cat'];
//$post_content = $_POST['post_content'];
$new_post = array(
// 'ID' => '',
'post_type' => 'invoices',
//'post_author' => $user->ID,
//'post_category' => array($post_category),
// 'post_content' => $post_content,
// 'post_title' => $post_title,
'post_status' => 'publish',
'meta_input' => array(
'invociesp[service]' => $_POST['service'],
'invocies[price]' => $_POST['price'],
'invocies[status]' => $_POST['status'],
'invocies[client]' => $_POST['client'],
// '_flatsome_case_type' => $_POST['type'],
//'city' => $post['city']
)
);
$post_id = wp_insert_post($new_post,true);
if($post_id ==true){
echo '<script language="javascript">alert("You Create a New case successfully")</script>';
}
wp_insert_uses (trac) the following loop to handle meta_input
,
if ( ! empty( $postarr['meta_input'] ) ) {
foreach ( $postarr['meta_input'] as $field => $value ) {
update_post_meta( $post_ID, $field, $value );
}
}
So if you change meta_input
in your new post args to be like this,
'meta_input' => array(
'invocies' => array(
'service' => $_POST['service'],
'price' => $_POST['price'],
'status' => $_POST['status'],
'client' => $_POST['client'],
),
)
I think the data would then get saved correctly.