Home > OS >  WordPress Custom Post Type FrontEnd
WordPress Custom Post Type FrontEnd

Time:11-15

I have created a custom post type 'student', Now I want that student can submit their details via a frontend form. But I have used years and department as custom taxonomies, and I can show them in frontend as dropdown, but can not submit the field value to regarding taxonomy. My code below:

    <?php add_shortcode( 'singul_frontend_post', 'singul_frontend_post' );
function singul_frontend_post() {
    singul_save_post_if_submitted();
    
    ?>
<div id="postbox">
    <form id="new_post" name="new_post" method="post" >

    <div class="form-group"><label for="title">Student Name</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="title" class="form-control" />
</div>
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
        <label for="title">Year Of SSC</label><br />
    <select name="ssc" class="form-control" searchable="A">
    <?php
       $tax_terms = get_terms('ssc', array('hide_empty' => '0'));      
       foreach ( $tax_terms as $tax_term ):  
          echo '<option value="" selected disabled hidden>Choose Year</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
       endforeach;
    ?>
</select></div>
</div>
<div class="col-md-6">
    <div class="form-group">
    <label for="title">Group</label><br />
    <select name="group" class="form-control" searchable="A">
    <?php
       $tax_terms = get_terms('group', array('hide_empty' => '0'));      
       foreach ( $tax_terms as $tax_term ):  
          echo '<option value="" selected disabled hidden>Choose Group</option><option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
       endforeach;
    ?>
</select></div></div>
</div>

<div class="row">
    <div class="col-md-6">
    <div class="form-group"><label for="title">Current Position</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="cposition" class="form-control" />
    </div>
    </div>
    <div class="col-md-6"><div class="form-group"><label for="title">Current Institute</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="cinstitute" class="form-control" />
    </div></div>
</div>

    <div class="row">
        <div class="col-md-6">
        <div class="form-group"><label for="title">Contact Number</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="contact" class="form-control"  placeholder="01XXXXXXXXX"/>
    </div>
    </div>
    <div class="col-md-6">
    <div class="form-group"><label for="title">Email Address</label><br />
        <input type="email" id="title" value="" tabindex="1" size="20" name="email" class="form-control"  />
    </div>
    </div>
</div>

    <div class="row">
        <div class="col-md-6">
        <div class="form-group"><label for="title">Facebook ID</label><br />
        <input type="text" id="title" value="" tabindex="1" size="20" name="facebook" class="form-control"  />
    </div>
        </div>
        <div class="col-md-6">
        <div class="form-group"><label for="title">Photo</label><br />
    <input type="file" name="user-image-featured" id="user-image-featured" size="20" class="form-control-file" >
    <?php wp_nonce_field( 'user-image-featured', 'user-image-featured_nonce' ); ?>
</div>
        </div>
    </div>
   
    
    
    <?php wp_nonce_field( 'wps-frontend-post' ); ?>

    <div class="form-group"><button type="submit" value="Publish" tabindex="6" id="submit" name="submit" class="btn btn-primary">Submit</button></div>
    
    </form>
</div>
    <?php
}

    
    function singul_save_post_if_submitted() {
    // Stop running function if form wasn't submitted
    if ( !isset($_POST['title']) ) {
        return;
    }

    // Check that the nonce was set and valid
    if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
        echo 'Did not save because your form seemed to be invalid. Sorry';
        return;
    }

    // Do some minor form validation to make sure there is content
    if (strlen($_POST['title']) < 3) {
        echo 'Please enter a title. Titles must be at least three characters long.';
        return;
    }
    

    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $_POST['title'],
        'post_status'   => 'draft',   // Could be: publish
        'post_type'     => 'student' // Could be: `page` or your CPT
    );
    
    $cposition = $_POST['cposition'];
    $institute = $_POST['cinstitute'];
    $contact = $_POST['contact'];
    $email = $_POST['email'];
    $fbid = $_POST['facebook'];
    $ssc = $_POST['ssc'];
    $group = $_POST['group'];
    $post_id = wp_insert_post($post);
    update_post_meta( $post_id, 'snp_student_cposition', $cposition );
    update_post_meta( $post_id, 'snp_student_institute', $institute );
    update_post_meta( $post_id, 'snp_student_contact', $contact );
    update_post_meta( $post_id, 'snp_student_email', $email );
    update_post_meta( $post_id, 'snp_student_fbid', $fbid );
    wp_set_object_terms($post_id, $ssc, 'ssc');
    wp_set_object_terms($post_id, $group, 'group');
    
        // The nonce was valid and the user has the capabilities, it is safe to continue.
     
        // These files need to be included as dependencies when on the front end.
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
         
        // Let WordPress handle the upload.
        // Remember, 'user-image-featured' is the name of our file input in our form above.
        $attachment_id = media_handle_upload( 'user-image-featured', $post_id);
            set_post_thumbnail( $post_id, $attachment_id );
         
    echo 'Saved your post successfully! :)';
}

here singul_field_name name is meta key. Also thinking how to get the attachment_id for featured image.

**Update: All custom meta insert done, just can not insert post featured image.

CodePudding user response:

You can use the wp_set_object_terms function to save the custom taxonomy terms.

wp_set_object_terms( $post_id, $_POST['input_name_of_your_dropdown'], 'ssc', true );
wp_set_object_terms( $post_id, $_POST['input_name_of_your_dropdown'], 'group', true );

The above code should be placed after wp_insert_post so that $post_id can be found.

  • Related