• Resolved sdcom

    (@sdcom)


    Hello, I’m sending a CPT through Frontend using the code below that is in my functions.php. On the frontend registration page, I have the form. It’s working, the only problem is that after sending the form, the success message appears on a white screen, and it ends. I need this message to somehow appear below the form. It is possible? Can someone help me?

    FUNCTIONS.PHP

    if(isset($_POST['name'])){
        
        $office_post = array(
                'post_type' => 'office',
                'post_title' => $_POST['name'],
                'post_content' => $_POST['address'],
                'post_status' => 'draft',
                
            );
        
        $post_id = wp_insert_post($office_post);
            
            if (!function_exists('wp_generate_attachment_metadata')){
                    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                    require_once(ABSPATH . "wp-admin" . '/includes/media.php');
                }
                 if ($_FILES) {
                    foreach ($_FILES as $file => $array) {
                        if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                            return "upload error : " . $_FILES[$file]['error'];
                        }
                        $attach_id = media_handle_upload( $file, $post_id );
                    }   
                }
                if ($attach_id > 0){
                    update_post_meta($post_id,'_thumbnail_id',$attach_id);
                }
        
        echo 'Message success!';          
        
        die;
    }

    PAGE-FORM.PHP

    <form method="post" class="form-wrap3 mb-30" data-bg-color="#f3f6f7" enctype="multipart/form-data">
                        <div class="form-title">
                            <h3 class="mt-n2 fls-n2 mb-0">Preencha seus dados</h3>
                            <p class="mb-4 mt-2">Depois do envio dos dados, faremos uma análise, e sendo aprovado, sua oficina estará disponível em nossa lista no prazo de 48h.</p>
                        </div>
                        <div class="form-group mb-15">
                            <input type="text" class="form-control style3" name="name" id="name" placeholder="Office Name" />
                            <i class="fal fa-user"></i>
                        </div>
                        <div class="form-group mb-15">
                            <textarea name="address" id="address" cols="30" rows="3" class="form-control style3" placeholder="Full Address"></textarea>
                            <i class="fal fa-pencil-alt"></i>
                        </div>
                        <div class="form-group mb-15">
                            <label>Insert your logo</label>
                            <input type="file" name="thumbnail" id="thumbnail" />
                        </div>
                        <div class="form-btn pt-15">
                            <button type="submit" class="vs-btn style2">Submit</button>
                        </div>
                        <p class="form-messages mb-0 mt-3"></p>
                    </form>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Just move that functions.php code to the page-form.php template ??

    Being on a template file, wp_generate_attachment_metadata() should always exist, so there’d never be any need to require other files. No harm in checking, but it’s unnecessary.

    Thread Starter sdcom

    (@sdcom)

    Ok, thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Submit a CPT from Frontend’ is closed to new replies.