• vectyr

    (@vectyr)


    So I found this code that allows you to created a front-end form, however, there wasn’t a field for adding a date. Let’s say you wanted to publish something for 3 days prior or for 4 days in the future, having some kind of date field or picker would rock.

    So I added this: “<input type=”date” id=”title” value=”” name=”date” />” but have no idea how to get that date field to set the date for that particular post. I’ve looked high and low but to no avail. I’m not the best with php so this might be cake for someone out there.

    <?php
    			 if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
    
    			// Do some minor form validation to make sure there is content
    			if (isset ($_POST['title'])) {
    			    $title =  $_POST['title'];
    			} else {
    			    echo 'Please enter the wine name';
    			}
    			if (isset ($_POST['description'])) {
    			    $description = $_POST['description'];
    			} else {
    			    echo 'Please enter some notes';
    			}
    
    			// ADD THE FORM INPUT TO $new_post ARRAY
    			$new_post = array(
    			'post_title'    =>  $title,
    			'post_content'  =>  $description,
    			'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
    			'post_date'     =>  $post_date,
    			'post_type' 		=>  'post'  //'post',page' or use a custom post type if you want to
    			);
    
    			//SAVE THE POST
    			$pid = wp_insert_post($new_post);
    
    			         //KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
    			wp_set_post_tags($pid, $_POST['post_tags']);
    
    			//REDIRECT TO THE NEW POST ON SAVE
    			wp_redirect( '/visits' );
    
    			} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
    
    			//POST THE POST YO
    			 do_action('wp_insert_post', 'wp_insert_post');
    
    			?>
    
    	    <div class="gform_wrapper">
    		    <form id="new_post" name="new_post" method="post" action="" class="wpcf7-form" enctype="multipart/form-data">
    	        <fieldset name="name">
    	            <input type="text" id="title" value="" name="title" placeholder="Who was visited?"/>
    	        </fieldset>
    	        <fieldset name="date">
    	            <input type="date" id="title" value="" name="date" />
    	        </fieldset>
    	        <fieldset class="content">
    	            <textarea id="description" name="description" cols="80" rows="10" placeholder="Notes about the visit"></textarea>
    	        </fieldset>
    	        <fieldset class="submit gform_footer">
    	            <input type="submit" value="Post it!" tabindex="40" id="submit" name="submit" />
    	        </fieldset>
    
    	        <input type="hidden" name="action" value="new_post" />
    	        <?php wp_nonce_field( 'new-post' ); ?>
    		    </form>
    	    </div>
  • The topic ‘Frontend Form, Adding a Post Date’ is closed to new replies.