Friday, 18 May 2018

insert data using $_post method using foreach loop

if(isset( $_POST['employeesubmit'] ) ){   
    $post_title = wp_strip_all_tags($_POST['first_name']);
    $post_content = $_POST['address'];
   
    $form_type = $_POST['form_type'];
    if ($form_type == 'update') {
        $new_post = array('ID' => $eid, 'post_title' => $post_title, 'post_content' => $post_content, 'post_status' => 'publish', 'post_type' => 'employee', );
        $post_id = wp_update_post($new_post);
    }
    elseif($form_type == 'insert') {
        $new_post = array('post_title' => $post_title, 'post_content' => $post_content, 'post_status' => 'publish', 'post_type' => 'employee', );
        $post_id = wp_insert_post($new_post, true);
    }
   
    $removeKeys = array('employeesubmit', 'address', 'form_type');
    foreach($removeKeys as $rem) {
        unset($_POST[$rem]);
    }
    foreach($_POST as $key => $value) {
        $$key = $value;
       // update_post_meta($post_id, $key, $$key); /* to label acf*/
    }
    $url = home_url( $wp->request ).'/?success';
     echo '<script type="text/javascript">
           window.location = "'.$url.'";
                </script>';
    //header("Location: ".$url );
}
?>